Spaces:
Build error
Build error
Update ocr_engine.py
Browse files- ocr_engine.py +13 -25
ocr_engine.py
CHANGED
@@ -5,37 +5,25 @@ from PIL import Image
|
|
5 |
|
6 |
def extract_weight_from_image(pil_img):
|
7 |
try:
|
8 |
-
|
9 |
-
img =
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
# Adaptive Thresholding for 7-segment LCD
|
17 |
-
processed = cv2.adaptiveThreshold(
|
18 |
-
gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 15, 10
|
19 |
)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# OCR config tuned for digit blocks
|
25 |
-
config = r'--oem 3 --psm 7 -c tessedit_char_whitelist=0123456789.'
|
26 |
-
|
27 |
-
# Run OCR
|
28 |
-
text = pytesseract.image_to_string(resized, config=config)
|
29 |
-
|
30 |
-
print("π RAW OCR OUTPUT:", repr(text))
|
31 |
|
32 |
-
|
33 |
-
weight = ''.join(c for c in text if c in '0123456789.')
|
34 |
-
weight = weight.strip()
|
35 |
|
|
|
36 |
confidence = 95 if weight else 0
|
37 |
return weight, confidence
|
38 |
|
39 |
except Exception as e:
|
40 |
-
print("β OCR
|
41 |
return "", 0
|
|
|
5 |
|
6 |
def extract_weight_from_image(pil_img):
|
7 |
try:
|
8 |
+
img = pil_img.convert("L") # grayscale
|
9 |
+
img = np.array(img)
|
10 |
+
|
11 |
+
img = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
|
12 |
+
blur = cv2.GaussianBlur(img, (5, 5), 0)
|
13 |
+
thresh = cv2.adaptiveThreshold(
|
14 |
+
blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
15 |
+
cv2.THRESH_BINARY_INV, 11, 2
|
|
|
|
|
|
|
16 |
)
|
17 |
|
18 |
+
config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
|
19 |
+
text = pytesseract.image_to_string(thresh, config=config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
print("π OCR RAW OUTPUT:", repr(text))
|
|
|
|
|
22 |
|
23 |
+
weight = ''.join(filter(lambda c: c in '0123456789.', text)).strip()
|
24 |
confidence = 95 if weight else 0
|
25 |
return weight, confidence
|
26 |
|
27 |
except Exception as e:
|
28 |
+
print("β OCR Exception:", str(e))
|
29 |
return "", 0
|