Spaces:
Runtime error
Runtime error
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) | |
# Resize and grayscale | |
img = cv2.resize(img, None, fx=4, fy=4, interpolation=cv2.INTER_LINEAR) | |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
gray = cv2.bilateralFilter(gray, 11, 17, 17) | |
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) | |
results = reader.readtext(thresh) | |
ocr_raw_texts = [] | |
weight_candidates = [] | |
for _, text, conf in results: | |
ocr_raw_texts.append(text) | |
t = text.lower() | |
t = t.replace("kg", "").replace("kgs", "") | |
t = t.replace("o", "0").replace("O", "0") | |
t = t.replace("s", "5").replace("S", "5") | |
t = t.replace("g", "9").replace("G", "6") | |
t = re.sub(r"[^\d\.]", "", t) | |
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", t): | |
weight_candidates.append((t, conf)) | |
if not weight_candidates: | |
return "Not detected", 0.0, "\n".join(ocr_raw_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_raw_texts) | |
except Exception as e: | |
return f"Error: {str(e)}", 0.0, "OCR failed" | |