Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +25 -24
ocr_engine.py
CHANGED
@@ -5,53 +5,54 @@ from PIL import Image
|
|
5 |
import easyocr
|
6 |
import os
|
7 |
|
8 |
-
# Initialize OCR
|
9 |
reader = easyocr.Reader(['en'], gpu=False)
|
10 |
|
11 |
def preprocess_image(image):
|
12 |
-
"""
|
13 |
-
|
|
|
|
|
14 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
return thresh
|
20 |
|
21 |
def extract_weight_from_image(pil_image):
|
22 |
try:
|
23 |
-
# Convert
|
24 |
image = np.array(pil_image.convert("RGB"))
|
25 |
|
26 |
-
#
|
27 |
-
print("Image shape:", image.shape)
|
28 |
-
|
29 |
-
# Preprocess for better OCR accuracy
|
30 |
processed = preprocess_image(image)
|
31 |
|
32 |
-
# Save debug image
|
33 |
-
debug_img = Image.fromarray(processed)
|
34 |
debug_path = "debug_processed_image.png"
|
35 |
-
|
36 |
-
print(f"
|
37 |
|
38 |
-
#
|
39 |
result = reader.readtext(processed)
|
40 |
|
41 |
-
print("
|
42 |
for r in result:
|
43 |
-
print(f"Text: '{r[1]}' | Confidence: {r[2]
|
44 |
|
45 |
-
#
|
46 |
weight = None
|
47 |
confidence = 0.0
|
48 |
|
49 |
for detection in result:
|
50 |
-
text = detection[1]
|
51 |
conf = detection[2]
|
52 |
|
53 |
-
#
|
54 |
-
match = re.search(r"\b\d
|
55 |
if match:
|
56 |
weight = match.group()
|
57 |
confidence = conf
|
@@ -63,5 +64,5 @@ def extract_weight_from_image(pil_image):
|
|
63 |
return "No weight detected", 0.0
|
64 |
|
65 |
except Exception as e:
|
66 |
-
print("β
|
67 |
return f"Error: {str(e)}", 0.0
|
|
|
5 |
import easyocr
|
6 |
import os
|
7 |
|
8 |
+
# β
Initialize OCR reader only once
|
9 |
reader = easyocr.Reader(['en'], gpu=False)
|
10 |
|
11 |
def preprocess_image(image):
|
12 |
+
"""
|
13 |
+
Preprocess the image to improve OCR detection.
|
14 |
+
Converts to grayscale and applies adaptive threshold.
|
15 |
+
"""
|
16 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
17 |
+
# Apply adaptive threshold to isolate digits better
|
18 |
+
thresh = cv2.adaptiveThreshold(
|
19 |
+
gray, 255,
|
20 |
+
cv2.ADAPTIVE_THRESH_MEAN_C,
|
21 |
+
cv2.THRESH_BINARY_INV,
|
22 |
+
11, 10
|
23 |
+
)
|
24 |
return thresh
|
25 |
|
26 |
def extract_weight_from_image(pil_image):
|
27 |
try:
|
28 |
+
# β
Step 1: Convert to OpenCV format
|
29 |
image = np.array(pil_image.convert("RGB"))
|
30 |
|
31 |
+
# β
Step 2: Preprocess image
|
|
|
|
|
|
|
32 |
processed = preprocess_image(image)
|
33 |
|
34 |
+
# β
Step 3: Optional - Save debug image
|
|
|
35 |
debug_path = "debug_processed_image.png"
|
36 |
+
Image.fromarray(processed).save(debug_path)
|
37 |
+
print(f"[DEBUG] Saved preprocessed image to {debug_path}")
|
38 |
|
39 |
+
# β
Step 4: Run EasyOCR
|
40 |
result = reader.readtext(processed)
|
41 |
|
42 |
+
print("π OCR Results:")
|
43 |
for r in result:
|
44 |
+
print(f" β’ Text: '{r[1]}' | Confidence: {r[2]*100:.2f}%")
|
45 |
|
46 |
+
# β
Step 5: Look for a decimal number like 53.25
|
47 |
weight = None
|
48 |
confidence = 0.0
|
49 |
|
50 |
for detection in result:
|
51 |
+
text = detection[1].replace(",", ".") # Handle comma decimal (if any)
|
52 |
conf = detection[2]
|
53 |
|
54 |
+
# Look for numbers like 53.25 or 100
|
55 |
+
match = re.search(r"\b\d{1,3}(\.\d{1,2})?\b", text)
|
56 |
if match:
|
57 |
weight = match.group()
|
58 |
confidence = conf
|
|
|
64 |
return "No weight detected", 0.0
|
65 |
|
66 |
except Exception as e:
|
67 |
+
print(f"β OCR Error: {e}")
|
68 |
return f"Error: {str(e)}", 0.0
|