import easyocr import numpy as np import cv2 import re reader = easyocr.Reader(['en'], gpu=False) def enhance_image(img): # Resize large image down to avoid OCR failure max_dim = 1000 height, width = img.shape[:2] if max(height, width) > max_dim: scale = max_dim / max(height, width) img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA) # Convert to gray gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # Denoise gray = cv2.fastNlMeansDenoising(gray, h=15) # Sharpen kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]]) sharp = cv2.filter2D(gray, -1, kernel) # Contrast clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) contrast = clahe.apply(sharp) return contrast def extract_weight_from_image(pil_img): try: img = np.array(pil_img) enhanced = enhance_image(img) results = reader.readtext(enhanced) print("DEBUG OCR RESULTS:", results) ocr_texts = [text for _, text, _ in results] weight_candidates = [] for _, text, conf in results: cleaned = text.lower().replace("kg", "").replace("kgs", "") cleaned = cleaned.replace("o", "0").replace("s", "5").replace("g", "9") cleaned = re.sub(r"[^\d\.]", "", cleaned) if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned): weight_candidates.append((cleaned, conf)) if not weight_candidates: return "Not detected", 0.0, "\n".join(ocr_texts) best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0] return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts) except Exception as e: return f"Error: {str(e)}", 0.0, "OCR failed"