Spaces:
Running
Running
Update ocr_engine.py
Browse files- ocr_engine.py +28 -51
ocr_engine.py
CHANGED
@@ -6,80 +6,57 @@ import re
|
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
def enhance_image(img):
|
9 |
-
# Convert to grayscale
|
10 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
thresh = cv2.adaptiveThreshold(denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
25 |
-
cv2.THRESH_BINARY, 11, 2)
|
26 |
-
|
27 |
-
return thresh
|
28 |
|
29 |
def extract_weight_from_image(pil_img):
|
30 |
try:
|
31 |
img = np.array(pil_img)
|
|
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
height, width = img.shape[:2]
|
36 |
-
if max(height, width) > max_dim:
|
37 |
-
scale = max_dim / max(height, width)
|
38 |
-
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
39 |
-
elif max(height, width) < 400:
|
40 |
-
scale = 2.5 # Upscale very small images
|
41 |
-
img = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
|
42 |
-
|
43 |
-
# Enhance image for OCR
|
44 |
-
preprocessed = enhance_image(img)
|
45 |
|
46 |
-
results = reader.readtext(
|
47 |
|
48 |
best_weight = None
|
49 |
best_conf = 0.0
|
50 |
|
51 |
-
for
|
52 |
-
|
53 |
-
|
54 |
-
text, conf = item[1]
|
55 |
-
cleaned = text.lower().strip()
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
if re.fullmatch(r"\d{
|
65 |
if conf > best_conf:
|
66 |
-
best_weight =
|
67 |
-
best_conf = conf
|
68 |
-
|
69 |
-
if not best_weight:
|
70 |
-
for item in results:
|
71 |
-
if len(item) != 2 or not isinstance(item[1], tuple):
|
72 |
-
continue
|
73 |
-
text, conf = item[1]
|
74 |
-
fallback = re.sub(r"[^\d\.]", "", text)
|
75 |
-
if fallback and fallback.replace(".", "").isdigit():
|
76 |
-
best_weight = fallback
|
77 |
best_conf = conf
|
78 |
-
break
|
79 |
|
80 |
if not best_weight:
|
81 |
return "Not detected", 0.0
|
82 |
|
|
|
83 |
if "." in best_weight:
|
84 |
int_part, dec_part = best_weight.split(".")
|
85 |
int_part = int_part.lstrip("0") or "0"
|
|
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
def enhance_image(img):
|
|
|
9 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
10 |
|
11 |
+
# CLAHE (adaptive histogram equalization for better contrast)
|
12 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
13 |
+
contrast = clahe.apply(gray)
|
14 |
|
15 |
+
# Sharpen image
|
16 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
17 |
+
sharpened = cv2.filter2D(contrast, -1, kernel)
|
18 |
|
19 |
+
# Resize if very small
|
20 |
+
h, w = sharpened.shape
|
21 |
+
if max(h, w) < 500:
|
22 |
+
sharpened = cv2.resize(sharpened, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
|
23 |
|
24 |
+
return sharpened
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def extract_weight_from_image(pil_img):
|
27 |
try:
|
28 |
img = np.array(pil_img)
|
29 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # convert from PIL to OpenCV BGR
|
30 |
|
31 |
+
# Preprocess image
|
32 |
+
processed = enhance_image(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
results = reader.readtext(processed)
|
35 |
|
36 |
best_weight = None
|
37 |
best_conf = 0.0
|
38 |
|
39 |
+
for (bbox, text, conf) in results:
|
40 |
+
original_text = text
|
41 |
+
text = text.lower().strip()
|
|
|
|
|
42 |
|
43 |
+
# Fix common OCR errors
|
44 |
+
text = text.replace(",", ".")
|
45 |
+
text = text.replace("o", "0").replace("O", "0")
|
46 |
+
text = text.replace("s", "5").replace("S", "5")
|
47 |
+
text = text.replace("g", "9").replace("G", "6")
|
48 |
+
text = text.replace("kgs", "").replace("kg", "")
|
49 |
+
text = re.sub(r"[^\d\.]", "", text)
|
50 |
|
51 |
+
if re.fullmatch(r"\d{1,4}(\.\d{1,3})?", text):
|
52 |
if conf > best_conf:
|
53 |
+
best_weight = text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
best_conf = conf
|
|
|
55 |
|
56 |
if not best_weight:
|
57 |
return "Not detected", 0.0
|
58 |
|
59 |
+
# Format output
|
60 |
if "." in best_weight:
|
61 |
int_part, dec_part = best_weight.split(".")
|
62 |
int_part = int_part.lstrip("0") or "0"
|