import cv2 import pytesseract import numpy as np from PIL import Image def extract_weight_from_image(pil_img): try: # Convert to OpenCV image img = pil_img.convert("RGB") img = np.array(img) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Crop or resize if needed (optional based on display layout) # Convert to grayscale and enhance contrast gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV) # Optional: Resize for better OCR (helps with small digits) resized = cv2.resize(thresh, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR) # Apply OCR with proper config for digits custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.' text = pytesseract.image_to_string(resized, config=custom_config) # Filter out non-numeric parts weight = ''.join(filter(lambda c: c in '0123456789.', text)) confidence = 95 if weight else 0 return weight.strip(), confidence except Exception as e: print("OCR Exception:", str(e)) return "", 0