import cv2 import pytesseract from PIL import Image import numpy as np def extract_weight(image: Image.Image) -> str: img = np.array(image.convert("RGB")) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) text = pytesseract.image_to_string(gray, config="--psm 7 digits") weight = ''.join(filter(lambda x: x in '0123456789.', text)) return weight if weight else "No valid weight detected"