File size: 517 Bytes
07d4fed
 
1362c73
07d4fed
 
a8c66ae
07d4fed
 
c28abeb
07d4fed
 
c11fb3a
07d4fed
 
34234c5
07d4fed
 
 
c11fb3a
07d4fed
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import easyocr
import re

# Load OCR reader once
reader = easyocr.Reader(['en'])

def extract_weight_from_image(pil_image):
    results = reader.readtext(pil_image)

    weight = None
    confidence = 0.0

    for (bbox, text, conf) in results:
        match = re.search(r'(\d+(\.\d+)?)\s?g', text.lower())
        if match:
            weight = match.group(1) + " g"
            confidence = conf
            break

    if weight:
        return weight, confidence
    else:
        return "No weight detected", 0.0