Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +20 -23
ocr_engine.py
CHANGED
@@ -1,55 +1,52 @@
|
|
1 |
-
import numpy as np
|
2 |
import re
|
3 |
import cv2
|
|
|
4 |
from PIL import Image
|
5 |
-
import
|
6 |
|
7 |
-
# β
Initialize
|
8 |
-
|
9 |
|
10 |
def preprocess_image(image):
|
11 |
"""
|
12 |
-
Convert to grayscale and
|
13 |
-
to enhance contrast for digital scale OCR.
|
14 |
"""
|
15 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
16 |
-
|
17 |
gray, 255,
|
18 |
-
cv2.
|
19 |
cv2.THRESH_BINARY_INV,
|
20 |
-
11,
|
21 |
)
|
22 |
-
return
|
23 |
|
24 |
def extract_weight_from_image(pil_image):
|
25 |
try:
|
26 |
-
# β
Convert
|
27 |
image = np.array(pil_image.convert("RGB"))
|
28 |
|
29 |
# β
Preprocess image
|
30 |
processed = preprocess_image(image)
|
31 |
|
32 |
-
#
|
33 |
debug_path = "debug_processed_image.png"
|
34 |
Image.fromarray(processed).save(debug_path)
|
35 |
-
print(f"[DEBUG]
|
36 |
|
37 |
-
# β
|
38 |
-
result =
|
39 |
|
40 |
-
print("π
|
41 |
-
for
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
for detection in result:
|
46 |
-
text = detection[1].replace(",", ".") # normalize decimal
|
47 |
-
conf = detection[2]
|
48 |
match = re.search(r"\b\d{1,4}(\.\d{1,2})?\b", text)
|
49 |
if match:
|
50 |
return match.group(), round(conf * 100, 2)
|
51 |
|
52 |
-
# β No weight found
|
53 |
return "No weight detected", 0.0
|
54 |
|
55 |
except Exception as e:
|
|
|
|
|
1 |
import re
|
2 |
import cv2
|
3 |
+
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
from paddleocr import PaddleOCR
|
6 |
|
7 |
+
# β
Initialize PaddleOCR once
|
8 |
+
ocr = PaddleOCR(use_angle_cls=False, lang='en', show_log=False)
|
9 |
|
10 |
def preprocess_image(image):
|
11 |
"""
|
12 |
+
Convert to grayscale and enhance contrast to help OCR.
|
|
|
13 |
"""
|
14 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
15 |
+
processed = cv2.adaptiveThreshold(
|
16 |
gray, 255,
|
17 |
+
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
18 |
cv2.THRESH_BINARY_INV,
|
19 |
+
11, 2
|
20 |
)
|
21 |
+
return processed
|
22 |
|
23 |
def extract_weight_from_image(pil_image):
|
24 |
try:
|
25 |
+
# β
Convert to OpenCV format
|
26 |
image = np.array(pil_image.convert("RGB"))
|
27 |
|
28 |
# β
Preprocess image
|
29 |
processed = preprocess_image(image)
|
30 |
|
31 |
+
# Optional: Save debug image
|
32 |
debug_path = "debug_processed_image.png"
|
33 |
Image.fromarray(processed).save(debug_path)
|
34 |
+
print(f"[DEBUG] Saved preprocessed image to {debug_path}")
|
35 |
|
36 |
+
# β
Run OCR on original (RGB) image (not preprocessed)
|
37 |
+
result = ocr.ocr(image, cls=False)
|
38 |
|
39 |
+
print("π PaddleOCR Results:")
|
40 |
+
for line in result[0]:
|
41 |
+
text = line[1][0]
|
42 |
+
conf = line[1][1]
|
43 |
+
print(f" β’ Text: '{text}' | Confidence: {conf*100:.2f}%")
|
44 |
|
45 |
+
# Try to extract number like 53.25 or 100
|
|
|
|
|
|
|
46 |
match = re.search(r"\b\d{1,4}(\.\d{1,2})?\b", text)
|
47 |
if match:
|
48 |
return match.group(), round(conf * 100, 2)
|
49 |
|
|
|
50 |
return "No weight detected", 0.0
|
51 |
|
52 |
except Exception as e:
|