Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +14 -17
ocr_engine.py
CHANGED
@@ -5,30 +5,27 @@ import cv2
|
|
5 |
|
6 |
reader = easyocr.Reader(['en'], gpu=False)
|
7 |
|
8 |
-
def extract_weight_from_image(
|
9 |
try:
|
10 |
-
|
11 |
|
12 |
-
#
|
13 |
-
gray = cv2.cvtColor(
|
14 |
|
15 |
-
# Resize to make
|
16 |
-
resized = cv2.resize(gray, None, fx=
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
20 |
|
21 |
-
#
|
22 |
-
thresh = cv2.adaptiveThreshold(filtered, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
23 |
-
cv2.THRESH_BINARY, 11, 2)
|
24 |
-
|
25 |
-
# OCR
|
26 |
result = reader.readtext(thresh, detail=0)
|
27 |
-
|
28 |
-
print("OCR
|
29 |
|
30 |
-
#
|
31 |
-
match = re.search(r
|
32 |
if match:
|
33 |
return match.group(), 95.0
|
34 |
else:
|
|
|
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 |
+
# Convert to grayscale
|
13 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
14 |
|
15 |
+
# Resize to make characters larger
|
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 detection
|
|
|
|
|
|
|
|
|
23 |
result = reader.readtext(thresh, detail=0)
|
24 |
+
combined_text = " ".join(result)
|
25 |
+
print("OCR Text:", combined_text)
|
26 |
|
27 |
+
# Use regex to extract weight like 52.30, 002.50 etc.
|
28 |
+
match = re.search(r"\b\d{1,4}\.?\d{0,2}\b", combined_text)
|
29 |
if match:
|
30 |
return match.group(), 95.0
|
31 |
else:
|