File size: 927 Bytes
5b73d24
65ed4c1
 
5b73d24
65ed4c1
363a646
65ed4c1
363a646
65ed4c1
5b73d24
363a646
5b73d24
 
 
65ed4c1
5b73d24
 
 
 
4a07e0e
5b73d24
4a07e0e
65ed4c1
 
 
 
 
 
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
import pytesseract
import numpy as np
import cv2
import re

def extract_weight_from_image(pil_img):
    try:
        img = np.array(pil_img)

        # Preprocessing
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        resized = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
        blur = cv2.GaussianBlur(resized, (5, 5), 0)
        _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

        # OCR using pytesseract
        config = "--psm 6"  # Assume a single uniform block of text
        text = pytesseract.image_to_string(thresh, config=config)
        print("OCR Output:", text)

        # Regex to find weight-like numbers
        match = re.search(r"\b\d{1,4}\.?\d{0,2}\b", text)
        if match:
            return match.group(), 95.0
        else:
            return "No weight detected", 0.0
    except Exception as e:
        return f"Error: {str(e)}", 0.0