import cv2 import pytesseract import numpy as np from PIL import Image def extract_weight_from_image(pil_img): try: img = pil_img.convert("L") # grayscale img = np.array(img) img = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) blur = cv2.GaussianBlur(img, (5, 5), 0) thresh = cv2.adaptiveThreshold( blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2 ) config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.' text = pytesseract.image_to_string(thresh, config=config) print("🔍 OCR RAW OUTPUT:", repr(text)) weight = ''.join(filter(lambda c: c in '0123456789.', text)).strip() confidence = 95 if weight else 0 return weight, confidence except Exception as e: print("❌ OCR Exception:", str(e)) return "", 0