Spaces:
Running
Running
Update ocr_engine.py
Browse files- ocr_engine.py +18 -12
ocr_engine.py
CHANGED
@@ -9,29 +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_LINEAR)
|
15 |
|
16 |
# Enhance contrast
|
17 |
gray = cv2.equalizeHist(gray)
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# OCR
|
22 |
result = reader.readtext(inverted, detail=0)
|
23 |
combined_text = " ".join(result)
|
24 |
-
print("OCR
|
25 |
|
26 |
-
#
|
27 |
-
match = re.search(r
|
28 |
if match:
|
29 |
return f"{match.group(1)} kg", 95.0
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
return "No weight detected kg", 0.0
|
37 |
|
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Convert to grayscale
|
13 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
14 |
|
15 |
# Enhance contrast
|
16 |
gray = cv2.equalizeHist(gray)
|
17 |
+
|
18 |
+
# Resize to enhance small text
|
19 |
+
gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
|
20 |
+
|
21 |
+
# Add light blur to reduce noise
|
22 |
+
gray = cv2.GaussianBlur(gray, (3, 3), 0)
|
23 |
+
|
24 |
+
# Invert for LCD screens with dark backgrounds
|
25 |
+
inverted = cv2.bitwise_not(gray)
|
26 |
|
27 |
# OCR
|
28 |
result = reader.readtext(inverted, detail=0)
|
29 |
combined_text = " ".join(result)
|
30 |
+
print("OCR Result:", combined_text)
|
31 |
|
32 |
+
# Try to detect weight pattern like "25kg" or "25.3kg"
|
33 |
+
match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)\s?(kg)", combined_text, re.IGNORECASE)
|
34 |
if match:
|
35 |
return f"{match.group(1)} kg", 95.0
|
36 |
+
|
37 |
+
# Fallback: detect numbers only
|
38 |
+
fallback = re.search(r"\d{1,4}(?:\.\d{1,2})?", combined_text)
|
39 |
+
if fallback:
|
40 |
+
return f"{fallback.group(0)} kg", 75.0
|
41 |
|
42 |
return "No weight detected kg", 0.0
|
43 |
|