import cv2 import pytesseract import numpy as np from PIL import Image def extract_weight_from_image(image): try: image = np.array(image.convert("RGB")) # Ensure RGB format gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) blur = cv2.GaussianBlur(gray, (3, 3), 0) # Optional: denoise text = pytesseract.image_to_string(blur, config='--psm 7 digits') weight = ''.join(filter(lambda x: x in '0123456789.', text)) confidence = 95 # Placeholder return weight.strip(), confidence except Exception as e: return "", 0