Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +6 -9
ocr_engine.py
CHANGED
@@ -10,14 +10,14 @@ def extract_weight_from_image(pil_img):
|
|
10 |
try:
|
11 |
img = np.array(pil_img)
|
12 |
|
13 |
-
# Resize
|
14 |
max_dim = 1000
|
15 |
height, width = img.shape[:2]
|
16 |
if max(height, width) > max_dim:
|
17 |
scale = max_dim / max(height, width)
|
18 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
19 |
|
20 |
-
# OCR
|
21 |
results = reader.readtext(img)
|
22 |
print("DEBUG OCR RESULTS:", results)
|
23 |
|
@@ -26,11 +26,10 @@ def extract_weight_from_image(pil_img):
|
|
26 |
fallback_weight = None
|
27 |
fallback_conf = 0.0
|
28 |
|
29 |
-
for _, (text, conf) in results:
|
30 |
original = text
|
31 |
cleaned = text.lower().strip()
|
32 |
|
33 |
-
# Fix common OCR misreads
|
34 |
cleaned = cleaned.replace(",", ".")
|
35 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
36 |
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
@@ -40,24 +39,22 @@ def extract_weight_from_image(pil_img):
|
|
40 |
|
41 |
raw_texts.append(f"{original} → {cleaned} (conf: {round(conf, 2)})")
|
42 |
|
43 |
-
# Save fallback if no match later
|
44 |
if cleaned and cleaned.replace(".", "").isdigit() and not fallback_weight:
|
45 |
fallback_weight = cleaned
|
46 |
fallback_conf = conf
|
47 |
|
48 |
-
# Match proper weight format: 75.02, 97.2, 105
|
49 |
if cleaned.count(".") <= 1 and re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
|
50 |
weight_candidates.append((cleaned, conf))
|
51 |
|
52 |
-
# Choose
|
53 |
if weight_candidates:
|
54 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
55 |
elif fallback_weight:
|
56 |
best_weight, best_conf = fallback_weight, fallback_conf
|
57 |
else:
|
58 |
-
return "Not detected", 0.0, "
|
59 |
|
60 |
-
#
|
61 |
if "." in best_weight:
|
62 |
int_part, dec_part = best_weight.split(".")
|
63 |
int_part = int_part.lstrip("0") or "0"
|
|
|
10 |
try:
|
11 |
img = np.array(pil_img)
|
12 |
|
13 |
+
# Resize if large
|
14 |
max_dim = 1000
|
15 |
height, width = img.shape[:2]
|
16 |
if max(height, width) > max_dim:
|
17 |
scale = max_dim / max(height, width)
|
18 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
19 |
|
20 |
+
# OCR
|
21 |
results = reader.readtext(img)
|
22 |
print("DEBUG OCR RESULTS:", results)
|
23 |
|
|
|
26 |
fallback_weight = None
|
27 |
fallback_conf = 0.0
|
28 |
|
29 |
+
for _, (text, conf) in results: # ✅ Correct unpacking
|
30 |
original = text
|
31 |
cleaned = text.lower().strip()
|
32 |
|
|
|
33 |
cleaned = cleaned.replace(",", ".")
|
34 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
35 |
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
|
|
39 |
|
40 |
raw_texts.append(f"{original} → {cleaned} (conf: {round(conf, 2)})")
|
41 |
|
|
|
42 |
if cleaned and cleaned.replace(".", "").isdigit() and not fallback_weight:
|
43 |
fallback_weight = cleaned
|
44 |
fallback_conf = conf
|
45 |
|
|
|
46 |
if cleaned.count(".") <= 1 and re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
|
47 |
weight_candidates.append((cleaned, conf))
|
48 |
|
49 |
+
# Choose result
|
50 |
if weight_candidates:
|
51 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
52 |
elif fallback_weight:
|
53 |
best_weight, best_conf = fallback_weight, fallback_conf
|
54 |
else:
|
55 |
+
return "Not detected", 0.0, "OCR returned nothing useful"
|
56 |
|
57 |
+
# Clean leading zeros
|
58 |
if "." in best_weight:
|
59 |
int_part, dec_part = best_weight.split(".")
|
60 |
int_part = int_part.lstrip("0") or "0"
|