Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +14 -1
ocr_engine.py
CHANGED
@@ -1,8 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def extract_weight_from_image(pil_img):
|
2 |
try:
|
3 |
img = np.array(pil_img)
|
|
|
|
|
4 |
img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
|
5 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
6 |
gray = cv2.bilateralFilter(gray, 11, 17, 17)
|
7 |
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
8 |
cv2.THRESH_BINARY_INV, 11, 2)
|
@@ -12,11 +23,13 @@ def extract_weight_from_image(pil_img):
|
|
12 |
|
13 |
weight_candidates = []
|
14 |
for _, text, conf in results:
|
15 |
-
cleaned = text.lower()
|
|
|
16 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
17 |
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
18 |
cleaned = cleaned.replace("g", "9").replace("G", "6")
|
19 |
cleaned = re.sub(r"[^\d\.]", "", cleaned)
|
|
|
20 |
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
|
21 |
weight_candidates.append((cleaned, conf))
|
22 |
|
|
|
1 |
+
import easyocr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import re
|
5 |
+
|
6 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
+
|
8 |
def extract_weight_from_image(pil_img):
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
+
|
12 |
+
# Resize and grayscale
|
13 |
img = cv2.resize(img, None, fx=3.5, fy=3.5, interpolation=cv2.INTER_LINEAR)
|
14 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
15 |
+
|
16 |
+
# Denoise and threshold
|
17 |
gray = cv2.bilateralFilter(gray, 11, 17, 17)
|
18 |
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
19 |
cv2.THRESH_BINARY_INV, 11, 2)
|
|
|
23 |
|
24 |
weight_candidates = []
|
25 |
for _, text, conf in results:
|
26 |
+
cleaned = text.lower()
|
27 |
+
cleaned = cleaned.replace("kg", "").replace("kgs", "")
|
28 |
cleaned = cleaned.replace("o", "0").replace("O", "0")
|
29 |
cleaned = cleaned.replace("s", "5").replace("S", "5")
|
30 |
cleaned = cleaned.replace("g", "9").replace("G", "6")
|
31 |
cleaned = re.sub(r"[^\d\.]", "", cleaned)
|
32 |
+
|
33 |
if re.fullmatch(r"\d{2,4}(\.\d{1,2})?", cleaned):
|
34 |
weight_candidates.append((cleaned, conf))
|
35 |
|