File size: 527 Bytes
2f21856
65ddb11
17d218a
65ef5f8
81b527b
65ddb11
65ef5f8
65ddb11
 
81b527b
17d218a
65ddb11
81b527b
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from PIL import Image
import pytesseract
import re

def extract_weight(img_path):
    img = Image.open(img_path).convert("L")  # Grayscale

    # OCR
    text = pytesseract.image_to_string(img, config='--psm 6')
    text = text.lower().replace('\n', ' ').strip()

    # Find weight + unit (e.g., 52.25 g, 75.8 kg)
    match = re.search(r'(\d+\.\d+|\d+)\s*(kg|g)', text)
    if match:
        number = match.group(1)
        unit = match.group(2)
        return f"{number} {unit}"
    else:
        return "Weight not detected"