Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +33 -37
ocr_engine.py
CHANGED
@@ -3,60 +3,56 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
6 |
-
# Load OCR engine
|
7 |
reader = easyocr.Reader(['en'], gpu=False)
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def extract_weight_from_image(pil_img):
|
10 |
try:
|
11 |
img = np.array(pil_img)
|
|
|
12 |
|
13 |
-
|
14 |
-
max_dim = 1000
|
15 |
-
height, width = img.shape[:2]
|
16 |
-
if max(height, width) > max_dim:
|
17 |
-
scale = max_dim / max(height, width)
|
18 |
-
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
19 |
-
|
20 |
-
# OCR without heavy preprocessing
|
21 |
-
results = reader.readtext(img)
|
22 |
print("DEBUG OCR RESULTS:", results)
|
23 |
|
24 |
-
|
25 |
weight_candidates = []
|
26 |
|
27 |
for _, text, conf in results:
|
28 |
-
|
29 |
-
cleaned =
|
30 |
-
|
31 |
-
# Fix common OCR mistakes
|
32 |
-
cleaned = cleaned.replace(",", ".")
|
33 |
-
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
34 |
-
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
35 |
-
cleaned = cleaned.replace("g", "9").replace("G", "6")
|
36 |
-
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
37 |
-
cleaned = re.sub(r"[^0-9\.]", "", cleaned)
|
38 |
-
|
39 |
-
raw_texts.append(f"{original} → {cleaned} (conf: {round(conf, 2)})")
|
40 |
|
41 |
-
|
42 |
-
if cleaned.count(".") <= 1 and re.match(r"^\d{2,4}(\.\d{1,3})?$", cleaned):
|
43 |
weight_candidates.append((cleaned, conf))
|
44 |
|
45 |
if not weight_candidates:
|
46 |
-
return "Not detected", 0.0, "\n".join(
|
47 |
|
48 |
-
# Get best weight
|
49 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
50 |
-
|
51 |
-
# Strip unnecessary leading zeros
|
52 |
-
if "." in best_weight:
|
53 |
-
int_part, dec_part = best_weight.split(".")
|
54 |
-
int_part = int_part.lstrip("0") or "0"
|
55 |
-
best_weight = f"{int_part}.{dec_part}"
|
56 |
-
else:
|
57 |
-
best_weight = best_weight.lstrip("0") or "0"
|
58 |
-
|
59 |
-
return best_weight, round(best_conf * 100, 2), "\n".join(raw_texts)
|
60 |
|
61 |
except Exception as e:
|
62 |
return f"Error: {str(e)}", 0.0, "OCR failed"
|
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
|
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
+
def enhance_image(img):
|
9 |
+
# Resize large image down to avoid OCR failure
|
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 |
+
# Convert to gray
|
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,-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 |
+
|
30 |
+
return contrast
|
31 |
+
|
32 |
def extract_weight_from_image(pil_img):
|
33 |
try:
|
34 |
img = np.array(pil_img)
|
35 |
+
enhanced = enhance_image(img)
|
36 |
|
37 |
+
results = reader.readtext(enhanced)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
print("DEBUG OCR RESULTS:", results)
|
39 |
|
40 |
+
ocr_texts = [text for _, text, _ in results]
|
41 |
weight_candidates = []
|
42 |
|
43 |
for _, text, conf in results:
|
44 |
+
cleaned = text.lower().replace("kg", "").replace("kgs", "")
|
45 |
+
cleaned = cleaned.replace("o", "0").replace("s", "5").replace("g", "9")
|
46 |
+
cleaned = re.sub(r"[^\d\.]", "", cleaned)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
|
|
|
49 |
weight_candidates.append((cleaned, conf))
|
50 |
|
51 |
if not weight_candidates:
|
52 |
+
return "Not detected", 0.0, "\n".join(ocr_texts)
|
53 |
|
|
|
54 |
best_weight, best_conf = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
55 |
+
return best_weight, round(best_conf * 100, 2), "\n".join(ocr_texts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
except Exception as e:
|
58 |
return f"Error: {str(e)}", 0.0, "OCR failed"
|