logger2 / ocr_engine.py
Sanjayraju30's picture
Update ocr_engine.py
546e454 verified
raw
history blame
2.19 kB
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image, ImageFilter
import torch
import re
# Load TrOCR model and processor
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
def clean_ocr_text(text):
# Fix common OCR misreads
text = text.replace(",", ".").replace("s", "5").replace("o", "0").replace("O", "0")
return re.sub(r"[^\d.kg]", "", text.lower())
def restore_decimal(text):
if re.fullmatch(r"\d{5}", text):
return f"{text[:2]}.{text[2:]}"
elif re.fullmatch(r"\d{4}", text):
return f"{text[:2]}.{text[2:]}"
return text
def extract_unit_from_text(raw_text):
raw_text = raw_text.lower()
if "kg" in raw_text:
return "kg"
elif "g" in raw_text:
return "g"
return "g" # fallback if no unit
def extract_weight(image):
try:
# Resize & sharpen image
image = image.resize((image.width * 2, image.height * 2), Image.BICUBIC)
image = image.filter(ImageFilter.SHARPEN)
# OCR inference
pixel_values = processor(images=image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values)
raw_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
cleaned = clean_ocr_text(raw_text)
# Case 1: Match decimal with unit
match = re.search(r"(\d{1,3}\.\d{1,3})\s*(kg|g)?", cleaned)
if match:
return f"{match.group(1)} {match.group(2) or ''}".strip(), raw_text
# Case 2: Large number fallback like 53255 → 52.255
match = re.search(r"\d{4,5}", cleaned)
if match:
decimal_fixed = restore_decimal(match.group())
unit = extract_unit_from_text(raw_text)
return f"{decimal_fixed} {unit}", raw_text
# Final fallback: plain number
match = re.search(r"\d+", cleaned)
if match:
return f"{match.group()} g", raw_text
return "Error: No valid weight found", raw_text
except Exception as e:
return f"Error: {str(e)}", ""