Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +16 -27
ocr_engine.py
CHANGED
@@ -3,18 +3,16 @@ import re
|
|
3 |
import cv2
|
4 |
from PIL import Image
|
5 |
import easyocr
|
6 |
-
import os
|
7 |
|
8 |
-
# β
Initialize
|
9 |
reader = easyocr.Reader(['en'], gpu=False)
|
10 |
|
11 |
def preprocess_image(image):
|
12 |
"""
|
13 |
-
|
14 |
-
|
15 |
"""
|
16 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
17 |
-
# Apply adaptive threshold to isolate digits better
|
18 |
thresh = cv2.adaptiveThreshold(
|
19 |
gray, 255,
|
20 |
cv2.ADAPTIVE_THRESH_MEAN_C,
|
@@ -25,43 +23,34 @@ def preprocess_image(image):
|
|
25 |
|
26 |
def extract_weight_from_image(pil_image):
|
27 |
try:
|
28 |
-
# β
|
29 |
image = np.array(pil_image.convert("RGB"))
|
30 |
|
31 |
-
# β
|
32 |
processed = preprocess_image(image)
|
33 |
|
34 |
-
# β
|
35 |
debug_path = "debug_processed_image.png"
|
36 |
Image.fromarray(processed).save(debug_path)
|
37 |
-
print(f"[DEBUG]
|
38 |
|
39 |
-
# β
|
40 |
result = reader.readtext(processed)
|
41 |
|
42 |
print("π OCR Results:")
|
43 |
-
for
|
44 |
-
print(f" β’ Text: '{
|
45 |
-
|
46 |
-
# β
Step 5: Look for a decimal number like 53.25
|
47 |
-
weight = None
|
48 |
-
confidence = 0.0
|
49 |
|
|
|
50 |
for detection in result:
|
51 |
-
text = detection[1].replace(",", ".") #
|
52 |
conf = detection[2]
|
53 |
-
|
54 |
-
# Look for numbers like 53.25 or 100
|
55 |
-
match = re.search(r"\b\d{1,3}(\.\d{1,2})?\b", text)
|
56 |
if match:
|
57 |
-
|
58 |
-
confidence = conf
|
59 |
-
break
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
else:
|
64 |
-
return "No weight detected", 0.0
|
65 |
|
66 |
except Exception as e:
|
67 |
print(f"β OCR Error: {e}")
|
|
|
3 |
import cv2
|
4 |
from PIL import Image
|
5 |
import easyocr
|
|
|
6 |
|
7 |
+
# β
Initialize EasyOCR Reader once
|
8 |
reader = easyocr.Reader(['en'], gpu=False)
|
9 |
|
10 |
def preprocess_image(image):
|
11 |
"""
|
12 |
+
Convert to grayscale and apply adaptive thresholding
|
13 |
+
to enhance contrast for digital scale OCR.
|
14 |
"""
|
15 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
|
|
16 |
thresh = cv2.adaptiveThreshold(
|
17 |
gray, 255,
|
18 |
cv2.ADAPTIVE_THRESH_MEAN_C,
|
|
|
23 |
|
24 |
def extract_weight_from_image(pil_image):
|
25 |
try:
|
26 |
+
# β
Convert PIL image to OpenCV format
|
27 |
image = np.array(pil_image.convert("RGB"))
|
28 |
|
29 |
+
# β
Preprocess image
|
30 |
processed = preprocess_image(image)
|
31 |
|
32 |
+
# β
Optional: Save debug image for troubleshooting
|
33 |
debug_path = "debug_processed_image.png"
|
34 |
Image.fromarray(processed).save(debug_path)
|
35 |
+
print(f"[DEBUG] Preprocessed image saved to: {debug_path}")
|
36 |
|
37 |
+
# β
Perform OCR using EasyOCR
|
38 |
result = reader.readtext(processed)
|
39 |
|
40 |
print("π OCR Results:")
|
41 |
+
for detection in result:
|
42 |
+
print(f" β’ Text: '{detection[1]}' | Confidence: {detection[2]*100:.2f}%")
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# β
Extract first matching numeric value
|
45 |
for detection in result:
|
46 |
+
text = detection[1].replace(",", ".") # normalize decimal
|
47 |
conf = detection[2]
|
48 |
+
match = re.search(r"\b\d{1,4}(\.\d{1,2})?\b", text)
|
|
|
|
|
49 |
if match:
|
50 |
+
return match.group(), round(conf * 100, 2)
|
|
|
|
|
51 |
|
52 |
+
# β No weight found
|
53 |
+
return "No weight detected", 0.0
|
|
|
|
|
54 |
|
55 |
except Exception as e:
|
56 |
print(f"β OCR Error: {e}")
|