Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +26 -13
ocr_engine.py
CHANGED
@@ -9,24 +9,37 @@ 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 |
-
|
17 |
-
cv2.THRESH_BINARY_INV, 11, 2)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
dilated = cv2.dilate(thresh, kernel, iterations=1)
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
text = " ".join(result).strip()
|
26 |
-
print("OCR Text:", text)
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
if match:
|
31 |
return match.group(), 95.0
|
32 |
else:
|
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
+
# Convert to grayscale
|
13 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
14 |
|
15 |
+
# Apply bilateral filter to reduce noise while keeping edges
|
16 |
+
filtered = cv2.bilateralFilter(gray, 11, 17, 17)
|
|
|
17 |
|
18 |
+
# Apply binary threshold
|
19 |
+
_, thresh = cv2.threshold(filtered, 150, 255, cv2.THRESH_BINARY_INV)
|
|
|
20 |
|
21 |
+
# Find contours
|
22 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
|
23 |
|
24 |
+
if not contours:
|
25 |
+
return "No weight detected", 0.0
|
26 |
+
|
27 |
+
# Get the largest contour assuming it's the display area
|
28 |
+
largest_contour = max(contours, key=cv2.contourArea)
|
29 |
+
x, y, w, h = cv2.boundingRect(largest_contour)
|
30 |
+
|
31 |
+
# Add padding
|
32 |
+
pad = 10
|
33 |
+
x, y = max(x - pad, 0), max(y - pad, 0)
|
34 |
+
cropped = gray[y:y+h+pad, x:x+w+pad]
|
35 |
+
|
36 |
+
# OCR on cropped area
|
37 |
+
result = reader.readtext(cropped, detail=0)
|
38 |
+
combined = " ".join(result)
|
39 |
+
print("Detected Text:", combined)
|
40 |
+
|
41 |
+
# Match weight patterns like 52.30 or 003.25
|
42 |
+
match = re.search(r"\b\d{2,4}\.?\d{0,2}\b", combined)
|
43 |
if match:
|
44 |
return match.group(), 95.0
|
45 |
else:
|