Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +21 -19
ocr_engine.py
CHANGED
@@ -9,33 +9,35 @@ 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 |
-
gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
19 |
-
cv2.THRESH_BINARY, 11, 2)
|
20 |
-
thresh = cv2.bitwise_not(thresh)
|
21 |
|
22 |
-
#
|
|
|
|
|
|
|
|
|
23 |
results = reader.readtext(thresh)
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
if not
|
34 |
return "Not detected", 0.0
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
return
|
39 |
|
40 |
except Exception as e:
|
41 |
return f"Error: {str(e)}", 0.0
|
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Resize and convert to grayscale
|
13 |
+
img = cv2.resize(img, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_LINEAR)
|
14 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
15 |
|
16 |
+
# Apply Gaussian blur to remove noise
|
17 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
|
|
|
|
|
|
18 |
|
19 |
+
# Apply adaptive threshold
|
20 |
+
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
21 |
+
cv2.THRESH_BINARY_INV, 15, 6)
|
22 |
+
|
23 |
+
# OCR
|
24 |
results = reader.readtext(thresh)
|
25 |
|
26 |
+
# Debug: Print all detected text
|
27 |
+
print("OCR Results:", results)
|
28 |
+
|
29 |
+
weight_candidates = []
|
30 |
+
for _, text, conf in results:
|
31 |
+
text = text.lower().replace('kg', '').replace('kgs', '').strip()
|
32 |
+
if re.match(r'^\d{2,4}(\.\d{1,2})?$', text):
|
33 |
+
weight_candidates.append((text, conf))
|
34 |
|
35 |
+
if not weight_candidates:
|
36 |
return "Not detected", 0.0
|
37 |
|
38 |
+
# Return the one with highest confidence
|
39 |
+
weight, confidence = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
40 |
+
return weight, round(confidence * 100, 2)
|
41 |
|
42 |
except Exception as e:
|
43 |
return f"Error: {str(e)}", 0.0
|