from transformers import TrOCRProcessor, VisionEncoderDecoderModel from PIL import Image, ImageEnhance import re # Load processor + model processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten") model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten") def extract_weight(image: Image.Image) -> str: # Crop only display region (adjust based on your image format) width, height = image.size display_area = image.crop((width * 0.35, height * 0.1, width * 0.65, height * 0.25)) # crop display center # Enhance contrast & sharpness display_area = display_area.convert("L") # grayscale display_area = ImageEnhance.Contrast(display_area).enhance(2.0) display_area = ImageEnhance.Sharpness(display_area).enhance(2.5) display_area = display_area.convert("RGB") # OCR pixel_values = processor(images=display_area, return_tensors="pt").pixel_values generated_ids = model.generate(pixel_values, max_length=32) full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] # Clean & parse cleaned = full_text.lower().replace(" ", "") match = re.search(r"(\d+(\.\d+)?)", cleaned) weight = match.group(1) if match else None if any(u in cleaned for u in ["kg", "kgs", "kilogram", "kilo"]): unit = "kg" elif any(u in cleaned for u in ["g", "gram", "grams"]): unit = "grams" else: unit = "kg" if weight and float(weight) >= 20 else "grams" return f"{weight} {unit}" if weight else ""