File size: 966 Bytes
f47b893
 
 
 
 
 
 
1362c73
f47b893
 
 
9c7d97d
f47b893
 
c11fb3a
f47b893
 
7e1096c
f47b893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
try:
    from paddleocr import PaddleOCR
    import re
    ocr = PaddleOCR(use_angle_cls=True, lang='en')
except Exception as e:
    print(f"❌ PaddleOCR failed to load: {e}")
    ocr = None

def extract_weight_from_image(image):
    if ocr is None:
        return ("OCR not initialized", 0.0)

    result = ocr.ocr(image, cls=True)
    debug_texts = []

    if not result or not result[0]:
        return ("No weight detected", 0.0)

    for line in result[0]:
        text, confidence = line[1][0], line[1][1]
        debug_texts.append(f"{text} (Conf: {confidence:.2f})")

        # Regex: number with optional kg/g
        match = re.search(r'(\d+\.?\d*)\s*(kg|g)?', text.lower())
        if match:
            weight = match.group(1)
            unit = match.group(2) if match.group(2) else "g"
            return (f"{weight} {unit}", confidence)

    print("🧪 OCR DEBUG:")
    for t in debug_texts:
        print(t)

    return ("No weight detected", 0.0)