Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +7 -11
ocr_engine.py
CHANGED
@@ -9,23 +9,19 @@ def extract_weight_from_image(pil_img):
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
-
#
|
13 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
14 |
-
|
15 |
-
|
16 |
-
resized = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
|
17 |
-
|
18 |
-
# Apply blur and threshold to improve text clarity
|
19 |
-
blurred = cv2.GaussianBlur(resized, (5, 5), 0)
|
20 |
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
21 |
|
22 |
-
# OCR
|
23 |
result = reader.readtext(thresh, detail=0)
|
24 |
combined_text = " ".join(result)
|
25 |
-
print("OCR
|
26 |
|
27 |
-
#
|
28 |
-
match = re.search(r"\b
|
29 |
if match:
|
30 |
return match.group(), 95.0
|
31 |
else:
|
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Preprocessing
|
13 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
14 |
+
resized = cv2.resize(gray, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_CUBIC)
|
15 |
+
blurred = cv2.GaussianBlur(resized, (3, 3), 0)
|
|
|
|
|
|
|
|
|
16 |
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
17 |
|
18 |
+
# OCR
|
19 |
result = reader.readtext(thresh, detail=0)
|
20 |
combined_text = " ".join(result)
|
21 |
+
print("OCR Result:", combined_text)
|
22 |
|
23 |
+
# Improve regex to only match numbers with optional decimal
|
24 |
+
match = re.search(r"\b(?:\d{1,3}\.?\d{1,2}|\d{1,4})\b", combined_text)
|
25 |
if match:
|
26 |
return match.group(), 95.0
|
27 |
else:
|