Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +11 -13
ocr_engine.py
CHANGED
@@ -3,28 +3,21 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
6 |
-
# Initialize EasyOCR
|
7 |
reader = easyocr.Reader(['en'], gpu=False)
|
8 |
|
9 |
def enhance_image(img):
|
10 |
-
# Downscale large images
|
11 |
max_dim = 1000
|
12 |
height, width = img.shape[:2]
|
13 |
if max(height, width) > max_dim:
|
14 |
scale = max_dim / max(height, width)
|
15 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
16 |
|
17 |
-
# Convert to grayscale
|
18 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
19 |
-
|
20 |
-
# Denoise
|
21 |
gray = cv2.fastNlMeansDenoising(gray, h=15)
|
22 |
|
23 |
-
# Sharpen
|
24 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
25 |
sharp = cv2.filter2D(gray, -1, kernel)
|
26 |
|
27 |
-
# Enhance contrast
|
28 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
29 |
enhanced = clahe.apply(sharp)
|
30 |
|
@@ -44,7 +37,7 @@ def extract_weight_from_image(pil_img):
|
|
44 |
for _, text, conf in results:
|
45 |
cleaned = text.lower().strip()
|
46 |
|
47 |
-
#
|
48 |
cleaned = cleaned.replace(",", ".")
|
49 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
50 |
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
@@ -52,18 +45,23 @@ def extract_weight_from_image(pil_img):
|
|
52 |
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
53 |
cleaned = re.sub(r"[^\d\.]", "", cleaned)
|
54 |
|
55 |
-
# Match
|
56 |
-
if re.fullmatch(r"\d{2,4}(\.\d{1,
|
57 |
weight_candidates.append((cleaned, conf))
|
58 |
|
59 |
if not weight_candidates:
|
60 |
return "Not detected", 0.0, "\n".join(ocr_texts)
|
61 |
|
62 |
-
# Pick highest confidence
|
63 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
64 |
|
65 |
-
#
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
|
69 |
|
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
|
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
def enhance_image(img):
|
|
|
9 |
max_dim = 1000
|
10 |
height, width = img.shape[:2]
|
11 |
if max(height, width) > max_dim:
|
12 |
scale = max_dim / max(height, width)
|
13 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
14 |
|
|
|
15 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
16 |
gray = cv2.fastNlMeansDenoising(gray, h=15)
|
17 |
|
|
|
18 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
19 |
sharp = cv2.filter2D(gray, -1, kernel)
|
20 |
|
|
|
21 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
22 |
enhanced = clahe.apply(sharp)
|
23 |
|
|
|
37 |
for _, text, conf in results:
|
38 |
cleaned = text.lower().strip()
|
39 |
|
40 |
+
# Fix common OCR errors
|
41 |
cleaned = cleaned.replace(",", ".")
|
42 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
43 |
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
|
|
45 |
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
46 |
cleaned = re.sub(r"[^\d\.]", "", cleaned)
|
47 |
|
48 |
+
# Match weights like: 58.8, 75.02, 97.2, 102.34, etc.
|
49 |
+
if re.fullmatch(r"\d{2,4}(\.\d{1,3})?", cleaned):
|
50 |
weight_candidates.append((cleaned, conf))
|
51 |
|
52 |
if not weight_candidates:
|
53 |
return "Not detected", 0.0, "\n".join(ocr_texts)
|
54 |
|
55 |
+
# Pick the highest confidence result
|
56 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
57 |
|
58 |
+
# Remove unnecessary leading zeros
|
59 |
+
if "." in best_weight:
|
60 |
+
parts = best_weight.split(".")
|
61 |
+
parts[0] = parts[0].lstrip("0") or "0"
|
62 |
+
best_weight = ".".join(parts)
|
63 |
+
else:
|
64 |
+
best_weight = best_weight.lstrip("0") or "0"
|
65 |
|
66 |
return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
|
67 |
|