Spaces:
Running
Running
Update ocr_engine.py
Browse files- ocr_engine.py +7 -18
ocr_engine.py
CHANGED
@@ -3,34 +3,28 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
6 |
-
# Load EasyOCR reader
|
7 |
reader = easyocr.Reader(['en'], gpu=False)
|
8 |
|
9 |
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 |
|
24 |
-
raw_texts = []
|
25 |
weight_candidates = []
|
26 |
fallback_weight = None
|
27 |
fallback_conf = 0.0
|
28 |
|
29 |
-
for
|
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")
|
@@ -38,26 +32,21 @@ def extract_weight_from_image(pil_img):
|
|
38 |
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
39 |
cleaned = re.sub(r"[^0-9\.]", "", cleaned)
|
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 best candidate
|
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"
|
@@ -65,7 +54,7 @@ def extract_weight_from_image(pil_img):
|
|
65 |
else:
|
66 |
best_weight = best_weight.lstrip("0") or "0"
|
67 |
|
68 |
-
return best_weight, round(best_conf * 100, 2)
|
69 |
|
70 |
except Exception as e:
|
71 |
-
return f"Error: {str(e)}", 0.0
|
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
|
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
def extract_weight_from_image(pil_img):
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Resize large image if needed
|
13 |
max_dim = 1000
|
14 |
height, width = img.shape[:2]
|
15 |
if max(height, width) > max_dim:
|
16 |
scale = max_dim / max(height, width)
|
17 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
18 |
|
19 |
+
# OCR
|
20 |
results = reader.readtext(img)
|
|
|
21 |
|
|
|
22 |
weight_candidates = []
|
23 |
fallback_weight = None
|
24 |
fallback_conf = 0.0
|
25 |
|
26 |
+
for box, (text, conf) in results: # ✅ Correct unpacking
|
|
|
27 |
cleaned = text.lower().strip()
|
|
|
|
|
28 |
cleaned = cleaned.replace(",", ".")
|
29 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
30 |
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
|
|
32 |
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
33 |
cleaned = re.sub(r"[^0-9\.]", "", cleaned)
|
34 |
|
|
|
|
|
|
|
35 |
if cleaned and cleaned.replace(".", "").isdigit() and not fallback_weight:
|
36 |
fallback_weight = cleaned
|
37 |
fallback_conf = conf
|
38 |
|
|
|
39 |
if cleaned.count(".") <= 1 and re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
|
40 |
weight_candidates.append((cleaned, conf))
|
41 |
|
|
|
42 |
if weight_candidates:
|
43 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
44 |
elif fallback_weight:
|
45 |
best_weight, best_conf = fallback_weight, fallback_conf
|
46 |
else:
|
47 |
+
return "Not detected", 0.0
|
48 |
|
49 |
+
# Normalize
|
50 |
if "." in best_weight:
|
51 |
int_part, dec_part = best_weight.split(".")
|
52 |
int_part = int_part.lstrip("0") or "0"
|
|
|
54 |
else:
|
55 |
best_weight = best_weight.lstrip("0") or "0"
|
56 |
|
57 |
+
return best_weight, round(best_conf * 100, 2)
|
58 |
|
59 |
except Exception as e:
|
60 |
+
return f"Error: {str(e)}", 0.0
|