Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +16 -13
ocr_engine.py
CHANGED
@@ -3,39 +3,42 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
6 |
-
# Initialize OCR reader once
|
7 |
reader = easyocr.Reader(['en'], gpu=False)
|
8 |
|
9 |
def extract_weight_from_image(pil_img):
|
10 |
try:
|
11 |
-
# Convert image to NumPy format
|
12 |
img = np.array(pil_img)
|
13 |
|
14 |
-
# Resize and
|
15 |
img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
|
16 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
17 |
gray = cv2.bilateralFilter(gray, 11, 17, 17)
|
18 |
-
|
|
|
19 |
|
20 |
-
# OCR
|
21 |
results = reader.readtext(thresh)
|
22 |
|
23 |
-
#
|
24 |
-
print("OCR Results:", results)
|
25 |
|
26 |
weight_candidates = []
|
27 |
for _, text, conf in results:
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
weight_candidates.append((clean, conf))
|
34 |
|
35 |
if not weight_candidates:
|
36 |
return "Not detected", 0.0
|
37 |
|
38 |
-
# Return best candidate
|
39 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
40 |
return best_weight, round(best_conf * 100, 2)
|
41 |
|
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
|
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
def extract_weight_from_image(pil_img):
|
9 |
try:
|
|
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Resize and grayscale
|
13 |
img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
|
14 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
15 |
+
|
16 |
+
# Denoise & Adaptive Threshold (better than OTSU for displays)
|
17 |
gray = cv2.bilateralFilter(gray, 11, 17, 17)
|
18 |
+
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
19 |
+
cv2.THRESH_BINARY_INV, 11, 2)
|
20 |
|
21 |
+
# Run OCR
|
22 |
results = reader.readtext(thresh)
|
23 |
|
24 |
+
print("OCR Results:", results) # For debugging
|
|
|
25 |
|
26 |
weight_candidates = []
|
27 |
for _, text, conf in results:
|
28 |
+
cleaned = text.lower()
|
29 |
+
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
30 |
+
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
31 |
+
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
32 |
+
cleaned = cleaned.replace("g", "9").replace("G", "6")
|
33 |
+
|
34 |
+
cleaned = re.sub(r"[^\d\.]", "", cleaned) # remove non-numeric
|
35 |
|
36 |
+
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned): # allow 2-4 digit weights
|
37 |
+
weight_candidates.append((cleaned, conf))
|
|
|
38 |
|
39 |
if not weight_candidates:
|
40 |
return "Not detected", 0.0
|
41 |
|
|
|
42 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
43 |
return best_weight, round(best_conf * 100, 2)
|
44 |
|