Spaces:
Runtime error
Runtime error
import easyocr | |
import numpy as np | |
import cv2 | |
import re | |
# Initialize OCR reader once | |
reader = easyocr.Reader(['en'], gpu=False) | |
def extract_weight_from_image(pil_img): | |
try: | |
# Convert image to NumPy format | |
img = np.array(pil_img) | |
# Resize and preprocess | |
img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR) | |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
gray = cv2.bilateralFilter(gray, 11, 17, 17) | |
_, thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) | |
# OCR | |
results = reader.readtext(thresh) | |
# Debug | |
print("OCR Results:", results) | |
weight_candidates = [] | |
for _, text, conf in results: | |
clean = text.lower().replace("kg", "").strip() | |
clean = clean.replace("o", "0").replace("O", "0") # fix OCR misreads | |
# Match weights like 86, 85.5, 102.3 | |
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 | |
# Return best candidate | |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0] | |
return best_weight, round(best_conf * 100, 2) | |
except Exception as e: | |
return f"Error: {str(e)}", 0.0 | |