Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +15 -9
ocr_engine.py
CHANGED
@@ -13,17 +13,12 @@ def enhance_image(img):
|
|
13 |
scale = max_dim / max(height, width)
|
14 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
15 |
|
16 |
-
# Grayscale
|
17 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
18 |
-
|
19 |
-
# Denoise
|
20 |
gray = cv2.fastNlMeansDenoising(gray, h=15)
|
21 |
|
22 |
-
|
23 |
-
kernel = np.array([[0, -1, 0], [-1, 5,-1], [0, -1, 0]])
|
24 |
sharp = cv2.filter2D(gray, -1, kernel)
|
25 |
|
26 |
-
# Contrast
|
27 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
28 |
contrast = clahe.apply(sharp)
|
29 |
|
@@ -41,18 +36,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 =
|
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:
|
|
|
13 |
scale = max_dim / max(height, width)
|
14 |
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
15 |
|
|
|
16 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
17 |
gray = cv2.fastNlMeansDenoising(gray, h=15)
|
18 |
|
19 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
|
|
20 |
sharp = cv2.filter2D(gray, -1, kernel)
|
21 |
|
|
|
22 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
23 |
contrast = clahe.apply(sharp)
|
24 |
|
|
|
36 |
weight_candidates = []
|
37 |
|
38 |
for _, text, conf in results:
|
39 |
+
cleaned = text.lower().strip()
|
40 |
+
|
41 |
+
# Fix common misreads
|
42 |
+
cleaned = cleaned.replace(",", ".") # comma → dot
|
43 |
+
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
44 |
+
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
45 |
+
cleaned = cleaned.replace("g", "9").replace("G", "6")
|
46 |
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
47 |
+
cleaned = re.sub(r"[^\d\.]", "", cleaned) # Keep only digits + dot
|
|
|
48 |
|
49 |
+
# Match: 2 to 4 digits, optional .digit
|
50 |
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
|
51 |
weight_candidates.append((cleaned, conf))
|
52 |
|
53 |
if not weight_candidates:
|
54 |
return "Not detected", 0.0, "\n".join(ocr_texts)
|
55 |
|
56 |
+
# Get highest confidence result
|
57 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
58 |
+
|
59 |
+
# Remove leading zeros
|
60 |
+
best_weight = best_weight.lstrip('0') or '0'
|
61 |
+
|
62 |
return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
|
63 |
|
64 |
except Exception as e:
|