Spaces:
Running
Running
Update ocr_engine.py
Browse files- ocr_engine.py +10 -4
ocr_engine.py
CHANGED
@@ -9,21 +9,28 @@ def extract_weight_from_image(pil_img):
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
-
# Resize large
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
cleaned = text.lower().strip()
|
28 |
cleaned = cleaned.replace(",", ".")
|
29 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
@@ -46,7 +53,6 @@ def extract_weight_from_image(pil_img):
|
|
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"
|
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Resize large images
|
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 |
+
# Run OCR
|
20 |
results = reader.readtext(img)
|
21 |
|
22 |
weight_candidates = []
|
23 |
fallback_weight = None
|
24 |
fallback_conf = 0.0
|
25 |
|
26 |
+
for item in results:
|
27 |
+
if len(item) != 2:
|
28 |
+
continue
|
29 |
+
text_data = item[1]
|
30 |
+
if not isinstance(text_data, tuple) or len(text_data) != 2:
|
31 |
+
continue
|
32 |
+
|
33 |
+
text, conf = text_data
|
34 |
cleaned = text.lower().strip()
|
35 |
cleaned = cleaned.replace(",", ".")
|
36 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
|
|
53 |
else:
|
54 |
return "Not detected", 0.0
|
55 |
|
|
|
56 |
if "." in best_weight:
|
57 |
int_part, dec_part = best_weight.split(".")
|
58 |
int_part = int_part.lstrip("0") or "0"
|