Spaces:
Running
Running
Update ocr_engine.py
Browse files- ocr_engine.py +14 -14
ocr_engine.py
CHANGED
@@ -2,31 +2,31 @@ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
2 |
from PIL import Image, ImageEnhance
|
3 |
import re
|
4 |
|
5 |
-
# Load
|
6 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
7 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
8 |
|
9 |
def extract_weight(image: Image.Image) -> str:
|
10 |
-
#
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
display_area = ImageEnhance.Contrast(display_area).enhance(2.0)
|
17 |
-
display_area = ImageEnhance.Sharpness(display_area).enhance(2.5)
|
18 |
-
display_area = display_area.convert("RGB")
|
19 |
-
|
20 |
-
# OCR
|
21 |
-
pixel_values = processor(images=display_area, return_tensors="pt").pixel_values
|
22 |
generated_ids = model.generate(pixel_values, max_length=32)
|
23 |
full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
24 |
|
25 |
-
#
|
|
|
|
|
|
|
26 |
cleaned = full_text.lower().replace(" ", "")
|
27 |
match = re.search(r"(\d+(\.\d+)?)", cleaned)
|
28 |
weight = match.group(1) if match else None
|
29 |
|
|
|
30 |
if any(u in cleaned for u in ["kg", "kgs", "kilogram", "kilo"]):
|
31 |
unit = "kg"
|
32 |
elif any(u in cleaned for u in ["g", "gram", "grams"]):
|
@@ -34,4 +34,4 @@ def extract_weight(image: Image.Image) -> str:
|
|
34 |
else:
|
35 |
unit = "kg" if weight and float(weight) >= 20 else "grams"
|
36 |
|
37 |
-
return f"{weight} {unit}" if weight else ""
|
|
|
2 |
from PIL import Image, ImageEnhance
|
3 |
import re
|
4 |
|
5 |
+
# Load OCR model
|
6 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
7 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
8 |
|
9 |
def extract_weight(image: Image.Image) -> str:
|
10 |
+
# Step 1: Enhance image
|
11 |
+
image = image.convert("L") # grayscale
|
12 |
+
image = ImageEnhance.Contrast(image).enhance(2.0)
|
13 |
+
image = ImageEnhance.Sharpness(image).enhance(2.5)
|
14 |
+
image = image.convert("RGB")
|
15 |
|
16 |
+
# Step 2: Run OCR
|
17 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
generated_ids = model.generate(pixel_values, max_length=32)
|
19 |
full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
20 |
|
21 |
+
# Debug
|
22 |
+
print("OCR Output:", full_text)
|
23 |
+
|
24 |
+
# Step 3: Extract number
|
25 |
cleaned = full_text.lower().replace(" ", "")
|
26 |
match = re.search(r"(\d+(\.\d+)?)", cleaned)
|
27 |
weight = match.group(1) if match else None
|
28 |
|
29 |
+
# Step 4: Detect unit based on actual OCR text
|
30 |
if any(u in cleaned for u in ["kg", "kgs", "kilogram", "kilo"]):
|
31 |
unit = "kg"
|
32 |
elif any(u in cleaned for u in ["g", "gram", "grams"]):
|
|
|
34 |
else:
|
35 |
unit = "kg" if weight and float(weight) >= 20 else "grams"
|
36 |
|
37 |
+
return f"{weight} {unit}" if weight else "No valid weight detected"
|