Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +17 -27
ocr_engine.py
CHANGED
@@ -1,33 +1,23 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import re
|
4 |
-
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
5 |
-
except Exception as e:
|
6 |
-
import streamlit as st
|
7 |
-
st.error(f"❌ OCR Engine Load Failed: {e}")
|
8 |
-
ocr = None
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
return ("OCR not initialized", 0.0)
|
13 |
|
14 |
-
|
15 |
-
|
16 |
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
for
|
21 |
-
|
22 |
-
debug_texts.append(f"{text} (Conf: {confidence:.2f})")
|
23 |
-
match = re.search(r'(\d+\.?\d*)\s*(kg|g)?', text.lower())
|
24 |
if match:
|
25 |
-
weight = match.group(1)
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return ("No weight detected", 0.0)
|
|
|
1 |
+
import easyocr
|
2 |
+
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load OCR reader once
|
5 |
+
reader = easyocr.Reader(['en'])
|
|
|
6 |
|
7 |
+
def extract_weight_from_image(pil_image):
|
8 |
+
results = reader.readtext(pil_image)
|
9 |
|
10 |
+
weight = None
|
11 |
+
confidence = 0.0
|
12 |
|
13 |
+
for (bbox, text, conf) in results:
|
14 |
+
match = re.search(r'(\d+(\.\d+)?)\s?g', text.lower())
|
|
|
|
|
15 |
if match:
|
16 |
+
weight = match.group(1) + " g"
|
17 |
+
confidence = conf
|
18 |
+
break
|
19 |
|
20 |
+
if weight:
|
21 |
+
return weight, confidence
|
22 |
+
else:
|
23 |
+
return "No weight detected", 0.0
|
|