Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +42 -16
ocr_engine.py
CHANGED
@@ -1,25 +1,51 @@
|
|
|
|
|
|
1 |
import cv2
|
2 |
-
import pytesseract
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# OCR
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
confidence = 95 # Replace with real confidence logic if needed
|
22 |
-
return weight.strip(), confidence
|
23 |
except Exception as e:
|
24 |
-
|
25 |
-
return "", 0
|
|
|
1 |
+
import easyocr
|
2 |
+
import re
|
3 |
import cv2
|
|
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
|
7 |
+
# Initialize EasyOCR reader (only once)
|
8 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
9 |
+
|
10 |
+
def preprocess_image(image):
|
11 |
+
# Convert to grayscale
|
12 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
13 |
+
|
14 |
+
# Apply thresholding (adaptive works well for 7-seg)
|
15 |
+
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
|
16 |
+
cv2.THRESH_BINARY_INV, 15, 10)
|
17 |
+
|
18 |
+
# Dilation to strengthen numbers
|
19 |
+
kernel = np.ones((2, 2), np.uint8)
|
20 |
+
dilated = cv2.dilate(thresh, kernel, iterations=1)
|
21 |
|
22 |
+
return dilated
|
23 |
+
|
24 |
+
def extract_weight_from_image(pil_image):
|
25 |
+
try:
|
26 |
+
# Convert PIL to OpenCV
|
27 |
+
image = np.array(pil_image.convert("RGB"))
|
28 |
+
processed = preprocess_image(image)
|
29 |
|
30 |
# OCR
|
31 |
+
result = reader.readtext(processed)
|
32 |
+
|
33 |
+
# Filter and extract digits like weight (e.g., 75.5)
|
34 |
+
weight = None
|
35 |
+
confidence = 0.0
|
36 |
+
for detection in result:
|
37 |
+
text = detection[1]
|
38 |
+
conf = detection[2]
|
39 |
+
match = re.search(r"\d{2,4}(\.\d{1,2})?", text) # match 2-4 digit decimal
|
40 |
+
if match:
|
41 |
+
weight = match.group()
|
42 |
+
confidence = conf
|
43 |
+
break
|
44 |
+
|
45 |
+
if weight:
|
46 |
+
return weight, round(confidence * 100, 2)
|
47 |
+
else:
|
48 |
+
return "No weight detected", 0.0
|
49 |
|
|
|
|
|
50 |
except Exception as e:
|
51 |
+
return f"Error: {str(e)}", 0.0
|
|