File size: 851 Bytes
b902588
34234c5
8f557ef
34234c5
 
1362c73
34234c5
 
 
c28abeb
34234c5
 
c11fb3a
34234c5
 
 
cfcd3d5
34234c5
 
 
 
 
 
c11fb3a
34234c5
 
 
42b463a
34234c5
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
from paddleocr import PaddleOCR
import re

# Initialize PaddleOCR once
ocr = PaddleOCR(use_angle_cls=True, lang='en')

def extract_weight_from_image(image):
    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})")

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

    print("DEBUG OCR OUTPUT:")
    for d in debug_texts:
        print(d)

    return ("No weight detected", 0.0)