Spaces:
Sleeping
Sleeping
Update ocr_engine.py
Browse files- ocr_engine.py +15 -12
ocr_engine.py
CHANGED
|
@@ -9,31 +9,34 @@ def extract_weight_from_image(pil_img):
|
|
| 9 |
try:
|
| 10 |
img = np.array(pil_img)
|
| 11 |
|
| 12 |
-
# Resize
|
| 13 |
if img.shape[1] > 1000:
|
| 14 |
img = cv2.resize(img, (1000, int(img.shape[0] * 1000 / img.shape[1])))
|
| 15 |
|
| 16 |
-
# Preprocessing
|
| 17 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 18 |
-
gray = cv2.resize(gray, None, fx=
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# OCR
|
| 26 |
result = reader.readtext(thresh, detail=0)
|
| 27 |
-
combined_text = " ".join(result)
|
| 28 |
print("OCR Text:", combined_text)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)
|
| 32 |
if match:
|
| 33 |
weight = match.group(1)
|
| 34 |
-
return f"{weight} kg",
|
| 35 |
else:
|
| 36 |
return "No weight detected kg", 0.0
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error: {str(e)}", 0.0
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
img = np.array(pil_img)
|
| 11 |
|
| 12 |
+
# Resize for consistency
|
| 13 |
if img.shape[1] > 1000:
|
| 14 |
img = cv2.resize(img, (1000, int(img.shape[0] * 1000 / img.shape[1])))
|
| 15 |
|
| 16 |
+
# Preprocessing for 7-segment digital font
|
| 17 |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 18 |
+
gray = cv2.resize(gray, None, fx=3, fy=3, interpolation=cv2.INTER_LINEAR)
|
| 19 |
+
blur = cv2.GaussianBlur(gray, (3, 3), 0)
|
| 20 |
+
_, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 21 |
+
|
| 22 |
+
# Optional inversion for black background with white digits
|
| 23 |
+
white_pct = np.mean(thresh > 127)
|
| 24 |
+
if white_pct < 0.5:
|
| 25 |
+
thresh = cv2.bitwise_not(thresh)
|
| 26 |
|
| 27 |
# OCR
|
| 28 |
result = reader.readtext(thresh, detail=0)
|
| 29 |
+
combined_text = " ".join(result).strip()
|
| 30 |
print("OCR Text:", combined_text)
|
| 31 |
|
| 32 |
+
# Match number like 25, 65.2, 18.89 etc.
|
| 33 |
+
match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)", combined_text)
|
| 34 |
if match:
|
| 35 |
weight = match.group(1)
|
| 36 |
+
return f"{weight} kg", 100.0
|
| 37 |
else:
|
| 38 |
return "No weight detected kg", 0.0
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error: {str(e)}", 0.0
|
| 42 |
+
|