Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +20 -8
ocr_engine.py
CHANGED
@@ -3,31 +3,32 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
|
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
def enhance_image(img):
|
9 |
-
#
|
10 |
max_dim = 1000
|
11 |
height, width = img.shape[:2]
|
12 |
if max(height, width) > max_dim:
|
13 |
scale = max_dim / max(height, width)
|
14 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
15 |
|
16 |
-
#
|
17 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
18 |
|
19 |
# Denoise
|
20 |
gray = cv2.fastNlMeansDenoising(gray, h=15)
|
21 |
|
22 |
# Sharpen
|
23 |
-
kernel = np.array([[0, -1, 0], [-1, 5
|
24 |
sharp = cv2.filter2D(gray, -1, kernel)
|
25 |
|
26 |
-
#
|
27 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
28 |
-
|
29 |
|
30 |
-
return
|
31 |
|
32 |
def extract_weight_from_image(pil_img):
|
33 |
try:
|
@@ -41,18 +42,29 @@ def extract_weight_from_image(pil_img):
|
|
41 |
weight_candidates = []
|
42 |
|
43 |
for _, text, conf in results:
|
44 |
-
cleaned = text.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
46 |
-
cleaned = cleaned.replace("o", "0").replace("s", "5").replace("g", "9")
|
47 |
cleaned = re.sub(r"[^\d\.]", "", cleaned)
|
48 |
|
|
|
49 |
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", 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 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
|
|
|
|
|
|
|
|
56 |
return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
|
57 |
|
58 |
except Exception as e:
|
|
|
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 |
|
31 |
+
return enhanced
|
32 |
|
33 |
def extract_weight_from_image(pil_img):
|
34 |
try:
|
|
|
42 |
weight_candidates = []
|
43 |
|
44 |
for _, text, conf in results:
|
45 |
+
cleaned = text.lower().strip()
|
46 |
+
|
47 |
+
# Replace common OCR misreads
|
48 |
+
cleaned = cleaned.replace(",", ".")
|
49 |
+
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
50 |
+
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
51 |
+
cleaned = cleaned.replace("g", "9").replace("G", "6")
|
52 |
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
|
|
53 |
cleaned = re.sub(r"[^\d\.]", "", cleaned)
|
54 |
|
55 |
+
# Match numbers like 84.5, 102.3, 99.9
|
56 |
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
|
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 match
|
63 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
64 |
+
|
65 |
+
# Strip leading zeros if any
|
66 |
+
best_weight = best_weight.lstrip('0') or '0'
|
67 |
+
|
68 |
return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
|
69 |
|
70 |
except Exception as e:
|