Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +19 -14
ocr_engine.py
CHANGED
@@ -3,35 +3,40 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
import re
|
5 |
|
6 |
-
# Load the OCR engine
|
7 |
reader = easyocr.Reader(['en'], gpu=False)
|
8 |
|
9 |
def extract_weight_from_image(pil_img):
|
10 |
try:
|
11 |
-
# Convert PIL to OpenCV image (numpy array)
|
12 |
img = np.array(pil_img)
|
13 |
|
14 |
-
#
|
15 |
-
img = cv2.resize(img, None, fx=
|
16 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
17 |
-
|
|
|
|
|
18 |
_, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
19 |
-
thresh = cv2.bitwise_not(thresh) # Invert for dark digits
|
20 |
|
21 |
-
#
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
weight_candidates = []
|
26 |
-
for
|
27 |
-
|
28 |
-
if re.
|
29 |
-
weight_candidates.append((
|
30 |
|
31 |
if not weight_candidates:
|
32 |
return "Not detected", 0.0
|
33 |
|
34 |
-
#
|
35 |
weight, confidence = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
36 |
return weight, round(confidence * 100, 2)
|
37 |
|
|
|
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 |
+
# STEP 1: Resize and convert to grayscale
|
13 |
+
img = cv2.resize(img, None, fx=4, fy=4, interpolation=cv2.INTER_CUBIC)
|
14 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
15 |
+
|
16 |
+
# STEP 2: Denoise + Threshold
|
17 |
+
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
18 |
_, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
|
|
19 |
|
20 |
+
# Invert to get black text on white background
|
21 |
+
inverted = cv2.bitwise_not(thresh)
|
22 |
+
|
23 |
+
# STEP 3: OCR
|
24 |
+
results = reader.readtext(inverted)
|
25 |
+
|
26 |
+
# Debug print
|
27 |
+
print("OCR Results:", results)
|
28 |
|
29 |
+
# STEP 4: Extract weight values using regex
|
30 |
weight_candidates = []
|
31 |
+
for _, text, conf in results:
|
32 |
+
text = text.replace("kg", "").replace("KG", "").strip()
|
33 |
+
if re.match(r"^\d{2,4}(\.\d{1,2})?$", text):
|
34 |
+
weight_candidates.append((text, conf))
|
35 |
|
36 |
if not weight_candidates:
|
37 |
return "Not detected", 0.0
|
38 |
|
39 |
+
# STEP 5: Highest confidence
|
40 |
weight, confidence = sorted(weight_candidates, key=lambda x: -x[1])[0]
|
41 |
return weight, round(confidence * 100, 2)
|
42 |
|