import numpy as np import re import cv2 from PIL import Image import easyocr import os # Initialize OCR Reader reader = easyocr.Reader(['en'], gpu=False) def preprocess_image(image): """Preprocess the image to improve OCR accuracy""" # Convert to grayscale gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Apply threshold to isolate digits _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) return thresh def extract_weight_from_image(pil_image): try: # Convert PIL to OpenCV format image = np.array(pil_image.convert("RGB")) # Print image shape for debugging print("Image shape:", image.shape) # Preprocess for better OCR accuracy processed = preprocess_image(image) # Save debug image debug_img = Image.fromarray(processed) debug_path = "debug_processed_image.png" debug_img.save(debug_path) print(f"✅ Processed image saved to: {debug_path}") # Run OCR on processed image result = reader.readtext(processed) print("✅ OCR Results:") for r in result: print(f"Text: '{r[1]}' | Confidence: {r[2] * 100:.2f}%") # Try to find numeric weight weight = None confidence = 0.0 for detection in result: text = detection[1] conf = detection[2] # Match numbers like 53.25 or 45 match = re.search(r"\b\d+(\.\d+)?\b", text) if match: weight = match.group() confidence = conf break if weight: return weight, round(confidence * 100, 2) else: return "No weight detected", 0.0 except Exception as e: print("❌ Exception during OCR:", str(e)) return f"Error: {str(e)}", 0.0