Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +0 -7
ocr_engine.py
CHANGED
@@ -9,22 +9,15 @@ def extract_weight_from_image(pil_img):
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
12 |
-
# Convert to grayscale
|
13 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
14 |
-
|
15 |
-
# Resize to improve OCR accuracy
|
16 |
resized = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
|
17 |
-
|
18 |
-
# Blur and threshold
|
19 |
blurred = cv2.GaussianBlur(resized, (5, 5), 0)
|
20 |
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
21 |
|
22 |
-
# OCR
|
23 |
result = reader.readtext(thresh, detail=0)
|
24 |
text = " ".join(result)
|
25 |
print("OCR Text:", text)
|
26 |
|
27 |
-
# Regex to find weight like 002.50 or 55.3
|
28 |
match = re.search(r"\b\d{2,4}\.?\d{0,2}\b", text)
|
29 |
if match:
|
30 |
return match.group(), 95.0
|
|
|
9 |
try:
|
10 |
img = np.array(pil_img)
|
11 |
|
|
|
12 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
|
|
|
|
13 |
resized = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
|
|
|
|
|
14 |
blurred = cv2.GaussianBlur(resized, (5, 5), 0)
|
15 |
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
16 |
|
|
|
17 |
result = reader.readtext(thresh, detail=0)
|
18 |
text = " ".join(result)
|
19 |
print("OCR Text:", text)
|
20 |
|
|
|
21 |
match = re.search(r"\b\d{2,4}\.?\d{0,2}\b", text)
|
22 |
if match:
|
23 |
return match.group(), 95.0
|