Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +16 -26
ocr_engine.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import easyocr
|
2 |
import numpy as np
|
3 |
-
import re
|
4 |
import cv2
|
|
|
5 |
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
@@ -9,37 +9,27 @@ def extract_weight_from_image(pil_img):
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
-
#
|
13 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
# Apply binary threshold
|
19 |
-
_, thresh = cv2.threshold(filtered, 150, 255, cv2.THRESH_BINARY_INV)
|
20 |
-
|
21 |
-
# Find contours
|
22 |
-
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
23 |
-
|
24 |
-
if not contours:
|
25 |
-
return "No weight detected", 0.0
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
x, y = max(x - pad, 0), max(y - pad, 0)
|
34 |
-
cropped = gray[y:y+h+pad, x:x+w+pad]
|
35 |
|
36 |
-
# OCR
|
37 |
-
result = reader.readtext(
|
38 |
-
|
39 |
-
print("
|
40 |
|
41 |
-
# Match weight
|
42 |
-
match = re.search(r"\b\d{2,4}\.?\d{0,2}\b",
|
43 |
if match:
|
44 |
return match.group(), 95.0
|
45 |
else:
|
|
|
1 |
import easyocr
|
2 |
import numpy as np
|
|
|
3 |
import cv2
|
4 |
+
import re
|
5 |
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Grayscale + resize
|
13 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
14 |
+
gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
|
15 |
|
16 |
+
# Histogram equalization
|
17 |
+
gray = cv2.equalizeHist(gray)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Adaptive threshold
|
20 |
+
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
21 |
+
cv2.THRESH_BINARY, 11, 2)
|
22 |
|
23 |
+
# Invert colors (LCD digits are usually dark on light)
|
24 |
+
thresh = cv2.bitwise_not(thresh)
|
|
|
|
|
25 |
|
26 |
+
# OCR
|
27 |
+
result = reader.readtext(thresh, detail=0)
|
28 |
+
combined_text = " ".join(result)
|
29 |
+
print("OCR Text:", combined_text)
|
30 |
|
31 |
+
# Match weight pattern (e.g. 52.30, 003.2, 250)
|
32 |
+
match = re.search(r"\b\d{2,4}\.?\d{0,2}\b", combined_text)
|
33 |
if match:
|
34 |
return match.group(), 95.0
|
35 |
else:
|