Spaces:
Running
Running
Update ocr_engine.py
Browse files- ocr_engine.py +24 -32
ocr_engine.py
CHANGED
@@ -1,36 +1,28 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
def extract_weight(
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
weight = match.group(1) if match else None
|
27 |
-
|
28 |
-
# Step 4: Infer unit
|
29 |
-
if any(u in cleaned for u in ["kg", "kgs", "kilo"]):
|
30 |
-
unit = "kg"
|
31 |
-
elif any(u in cleaned for u in ["g", "gram", "grams"]):
|
32 |
-
unit = "grams"
|
33 |
-
else:
|
34 |
-
unit = "kg" if weight and float(weight) >= 20 else "grams"
|
35 |
-
|
36 |
-
return f"{weight} {unit}" if weight else "No valid weight detected"
|
|
|
1 |
+
import cv2
|
2 |
+
import pytesseract
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
|
6 |
+
def preprocess_image(pil_image):
|
7 |
+
image = np.array(pil_image.convert("RGB"))
|
8 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
9 |
+
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
|
10 |
+
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
11 |
+
return thresh
|
12 |
|
13 |
+
def extract_weight(pil_image):
|
14 |
+
try:
|
15 |
+
processed_img = preprocess_image(pil_image)
|
16 |
+
config = "--psm 7 -c tessedit_char_whitelist=0123456789."
|
17 |
+
text = pytesseract.image_to_string(processed_img, config=config)
|
18 |
+
print("OCR Raw:", text)
|
19 |
|
20 |
+
numbers = ''.join(filter(lambda x: x in "0123456789.", text))
|
21 |
+
if not numbers:
|
22 |
+
return "No valid weight detected"
|
|
|
23 |
|
24 |
+
weight_val = float(numbers)
|
25 |
+
unit = "kg" if weight_val >= 20 else "grams"
|
26 |
+
return f"{weight_val} {unit}"
|
27 |
+
except Exception as e:
|
28 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|