Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +19 -53
ocr_engine.py
CHANGED
@@ -1,55 +1,21 @@
|
|
1 |
from mmocr.utils.ocr import MMOCR
|
2 |
-
import numpy as np
|
3 |
-
import cv2
|
4 |
-
import re
|
5 |
-
from PIL import Image
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
27 |
-
cleaned = re.sub(r"[^0-9\.]", "", cleaned)
|
28 |
-
|
29 |
-
raw_texts.append(f"{original} → {cleaned} (conf: {round(conf, 2)})")
|
30 |
-
|
31 |
-
if cleaned and cleaned.replace(".", "").isdigit() and not fallback_weight:
|
32 |
-
fallback_weight = cleaned
|
33 |
-
fallback_conf = conf
|
34 |
-
|
35 |
-
if cleaned.count(".") <= 1 and re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
|
36 |
-
weight_candidates.append((cleaned, conf))
|
37 |
-
|
38 |
-
if weight_candidates:
|
39 |
-
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
40 |
-
elif fallback_weight:
|
41 |
-
best_weight, best_conf = fallback_weight, fallback_conf
|
42 |
-
else:
|
43 |
-
return "Not detected", 0.0, "\n".join(raw_texts)
|
44 |
-
|
45 |
-
if "." in best_weight:
|
46 |
-
int_part, dec_part = best_weight.split(".")
|
47 |
-
int_part = int_part.lstrip("0") or "0"
|
48 |
-
best_weight = f"{int_part}.{dec_part}"
|
49 |
-
else:
|
50 |
-
best_weight = best_weight.lstrip("0") or "0"
|
51 |
-
|
52 |
-
return best_weight, round(best_conf * 100, 2), "\n".join(raw_texts)
|
53 |
-
|
54 |
-
except Exception as e:
|
55 |
-
return f"Error: {str(e)}", 0.0, "OCR failed"
|
|
|
1 |
from mmocr.utils.ocr import MMOCR
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
# Load MMOCR model
|
4 |
+
mmocr = MMOCR(det='DB_r18', recog='CRNN', det_config=None, recog_config=None)
|
5 |
+
|
6 |
+
def extract_weight_from_image(image):
|
7 |
+
result = mmocr.readtext(image, output=None)
|
8 |
+
texts = [item['text'] for item in result]
|
9 |
+
debug_text = "\n".join(texts)
|
10 |
+
|
11 |
+
# Find the first valid float that could represent a weight
|
12 |
+
for text in texts:
|
13 |
+
if any(char.isdigit() for char in text):
|
14 |
+
cleaned = text.replace('kg', '').replace('KG', '').strip()
|
15 |
+
try:
|
16 |
+
weight = float(cleaned)
|
17 |
+
return str(weight), debug_text
|
18 |
+
except:
|
19 |
+
continue
|
20 |
+
|
21 |
+
return None, debug_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|