File size: 681 Bytes
3165179
 
 
65ef5f8
 
 
 
 
3165179
65ef5f8
3165179
65ef5f8
 
 
 
 
 
 
 
 
3165179
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import cv2
import pytesseract
import numpy as np
from PIL import Image

def extract_weight(pil_image: Image.Image) -> str:
    # Convert PIL to OpenCV image
    img = np.array(pil_image.convert("RGB"))

    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    # Optional: Resize to improve accuracy
    gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)

    # OCR config for digits only
    config = "--psm 7 digits"
    text = pytesseract.image_to_string(gray, config=config)

    # Keep digits and decimal
    weight = ''.join(filter(lambda x: x in '0123456789.', text))
    return weight if weight else "No valid weight detected"