import easyocr import numpy as np import cv2 import re reader = easyocr.Reader(['en'], gpu=False) def extract_weight_from_image(pil_img): try: img = np.array(pil_img) # Convert to grayscale and resize gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) # Histogram equalization and adaptive threshold gray = cv2.equalizeHist(gray) thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) thresh = cv2.bitwise_not(thresh) # OCR with bounding boxes results = reader.readtext(thresh) # Filter potential weight values candidates = [] for (bbox, text, confidence) in results: # Clean text clean_text = text.replace('kg', '').strip() if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", clean_text): candidates.append((clean_text, confidence)) if not candidates: return "Not detected", 0.0 # Choose the highest confidence match best_weight, conf = sorted(candidates, key=lambda x: -x[1])[0] return best_weight, round(conf, 2) except Exception as e: return f"Error: {str(e)}", 0.0