import cv2 import easyocr import numpy as np import re # Initialize EasyOCR reader reader = easyocr.Reader(['en'], gpu=False) def sharpen_image(img_cv): kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]]) return cv2.filter2D(img_cv, -1, kernel) def extract_weight_from_image(pil_img): # Convert PIL to OpenCV img_cv = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR) # Resize and sharpen img_cv = cv2.resize(img_cv, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC) img_cv = sharpen_image(img_cv) # Convert to grayscale img_gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY) # Apply adaptive threshold and morphology img_thresh = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 3) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) img_clean = cv2.morphologyEx(img_thresh, cv2.MORPH_CLOSE, kernel) # OCR results = reader.readtext(img_clean) filtered = [text for _, text, conf in results if len(text.strip()) <= 5 and conf > 0.4] raw_text = " ".join(filtered) matches = re.findall(r"\d+\.\d+|\d+", raw_text) weight = matches[0] if matches else "0.0" confidence = int(results[0][2] * 100) if results else 0 return weight, confidence, raw_text, img_cv # Return processed image