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"