Spaces:
Runtime error
Runtime error
Update ocr_engine.py
Browse files- ocr_engine.py +21 -15
ocr_engine.py
CHANGED
|
@@ -3,42 +3,48 @@ from PIL import Image, ImageFilter
|
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
| 8 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
| 9 |
|
| 10 |
def clean_ocr_text(text):
|
| 11 |
print("[RAW OCR]", text)
|
| 12 |
-
# Fix common misreads
|
| 13 |
text = text.replace(",", ".").replace("s", "5").replace("o", "0").replace("O", "0")
|
| 14 |
-
text = re.sub(r"[^\d
|
| 15 |
print("[CLEANED OCR]", text)
|
| 16 |
return text
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
def extract_weight(image):
|
| 25 |
try:
|
| 26 |
-
image =
|
|
|
|
|
|
|
| 27 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 28 |
generated_ids = model.generate(pixel_values)
|
| 29 |
raw_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
| 30 |
|
| 31 |
cleaned = clean_ocr_text(raw_text)
|
| 32 |
|
| 33 |
-
# Try
|
| 34 |
-
match = re.search(r
|
| 35 |
if match:
|
| 36 |
return f"{match.group(1)} {match.group(2)}"
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
if
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
return f"No valid weight found | OCR: {cleaned}"
|
| 44 |
except Exception as e:
|
|
|
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
| 8 |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
| 9 |
|
| 10 |
def clean_ocr_text(text):
|
| 11 |
print("[RAW OCR]", text)
|
|
|
|
| 12 |
text = text.replace(",", ".").replace("s", "5").replace("o", "0").replace("O", "0")
|
| 13 |
+
text = re.sub(r"[^\d.kg]", "", text.lower()) # Keep digits, dots, and kg
|
| 14 |
print("[CLEANED OCR]", text)
|
| 15 |
return text
|
| 16 |
|
| 17 |
+
def restore_decimal(text):
|
| 18 |
+
if re.fullmatch(r"\d{5}", text):
|
| 19 |
+
return f"{text[:2]}.{text[2:]}"
|
| 20 |
+
elif re.fullmatch(r"\d{4}", text):
|
| 21 |
+
return f"{text[:2]}.{text[2:]}"
|
| 22 |
+
return text
|
| 23 |
|
| 24 |
def extract_weight(image):
|
| 25 |
try:
|
| 26 |
+
image = image.resize((image.width * 2, image.height * 2), Image.BICUBIC)
|
| 27 |
+
image = image.filter(ImageFilter.SHARPEN)
|
| 28 |
+
|
| 29 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 30 |
generated_ids = model.generate(pixel_values)
|
| 31 |
raw_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
| 32 |
|
| 33 |
cleaned = clean_ocr_text(raw_text)
|
| 34 |
|
| 35 |
+
# Try direct match: e.g., 52.25 kg or 75.0 g
|
| 36 |
+
match = re.search(r"(\d{1,3}\.\d{1,3})\s*(kg|g)", cleaned)
|
| 37 |
if match:
|
| 38 |
return f"{match.group(1)} {match.group(2)}"
|
| 39 |
|
| 40 |
+
# Try fallback: extract digits and manually guess decimal
|
| 41 |
+
fallback_match = re.search(r"(\d{4,5})", cleaned)
|
| 42 |
+
if fallback_match:
|
| 43 |
+
fallback_value = restore_decimal(fallback_match.group(1))
|
| 44 |
+
|
| 45 |
+
# Check for presence of unit hints in raw_text
|
| 46 |
+
unit = "kg" if "kg" in raw_text.lower() else "g"
|
| 47 |
+
return f"{fallback_value} {unit}"
|
| 48 |
|
| 49 |
return f"No valid weight found | OCR: {cleaned}"
|
| 50 |
except Exception as e:
|