Spaces:
Runtime error
Runtime error
import easyocr | |
import numpy as np | |
import cv2 | |
import re | |
# Load the OCR engine | |
reader = easyocr.Reader(['en'], gpu=False) | |
def extract_weight_from_image(pil_img): | |
try: | |
# Convert PIL to OpenCV image (numpy array) | |
img = np.array(pil_img) | |
# Step 1: Preprocess image for better OCR | |
img = cv2.resize(img, None, fx=3, fy=3, interpolation=cv2.INTER_LINEAR) | |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
blur = cv2.GaussianBlur(gray, (3, 3), 0) | |
_, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) | |
thresh = cv2.bitwise_not(thresh) # Invert for dark digits | |
# Step 2: Run OCR | |
results = reader.readtext(thresh, detail=1) | |
# Step 3: Extract numbers like 65.20 or 50 | |
weight_candidates = [] | |
for bbox, text, conf in results: | |
clean = text.lower().replace("kg", "").replace("kgs", "").strip() | |
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", clean): | |
weight_candidates.append((clean, conf)) | |
if not weight_candidates: | |
return "Not detected", 0.0 | |
# Step 4: Choose highest confidence number | |
weight, confidence = sorted(weight_candidates, key=lambda x: -x[1])[0] | |
return weight, round(confidence * 100, 2) | |
except Exception as e: | |
return f"Error: {str(e)}", 0.0 | |