Spaces:
Runtime error
Runtime error
File size: 1,474 Bytes
6b14fa5 65ed4c1 8fe1b94 a71f519 6b14fa5 65ed4c1 363a646 65ed4c1 2469d8d 363a646 65ed4c1 2469d8d 363a646 e91f073 2469d8d e91f073 2469d8d 701d11a 2469d8d e91f073 2469d8d f901f58 2469d8d 103f82b f901f58 103f82b 2469d8d 8fe1b94 65ed4c1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import easyocr
import numpy as np
import cv2
import re
reader = easyocr.Reader(['en'], gpu=False)
def extract_weight_from_image(pil_img):
try:
# Convert PIL to NumPy
img = np.array(pil_img)
# Step 1: Preprocessing
img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Improve contrast & threshold
blur = cv2.GaussianBlur(gray, (5, 5), 0)
_, binary = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
binary = cv2.bitwise_not(binary)
# Step 2: OCR with bounding boxes
results = reader.readtext(binary, detail=1)
# Step 3: Filter for weight-like values
weight_candidates = []
for bbox, text, conf in results:
clean = text.lower().replace("kg", "").replace("kgs", "").strip()
clean = clean.replace("o", "0").replace("O", "0") # common OCR mistake
# Match like 2 digits or 3 digits or decimal numbers
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: Pick most confident
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
|