Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +15 -11
ocr_engine.py
CHANGED
@@ -1,10 +1,16 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
def extract_weight_from_image(image):
|
|
|
|
|
|
|
8 |
result = ocr.ocr(image, cls=True)
|
9 |
debug_texts = []
|
10 |
|
@@ -14,16 +20,14 @@ def extract_weight_from_image(image):
|
|
14 |
for line in result[0]:
|
15 |
text, confidence = line[1][0], line[1][1]
|
16 |
debug_texts.append(f"{text} (Conf: {confidence:.2f})")
|
17 |
-
|
18 |
-
# Find number with optional decimal + optional "kg", "g"
|
19 |
match = re.search(r'(\d+\.?\d*)\s*(kg|g)?', text.lower())
|
20 |
if match:
|
21 |
-
unit = match.group(2) if match.group(2) else "g"
|
22 |
weight = match.group(1)
|
|
|
23 |
return (f"{weight} {unit}", confidence)
|
24 |
|
25 |
-
print("
|
26 |
-
for
|
27 |
-
print(
|
28 |
|
29 |
return ("No weight detected", 0.0)
|
|
|
1 |
+
try:
|
2 |
+
from paddleocr import PaddleOCR
|
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 |
def extract_weight_from_image(image):
|
11 |
+
if ocr is None:
|
12 |
+
return ("OCR not initialized", 0.0)
|
13 |
+
|
14 |
result = ocr.ocr(image, cls=True)
|
15 |
debug_texts = []
|
16 |
|
|
|
20 |
for line in result[0]:
|
21 |
text, confidence = line[1][0], line[1][1]
|
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 |
+
unit = match.group(2) if match.group(2) else "g"
|
27 |
return (f"{weight} {unit}", confidence)
|
28 |
|
29 |
+
print("OCR Debug Output:")
|
30 |
+
for t in debug_texts:
|
31 |
+
print(t)
|
32 |
|
33 |
return ("No weight detected", 0.0)
|