Spaces:
Running
Running
File size: 1,547 Bytes
d754b04 5d38db5 d22d28e 65ef5f8 5d38db5 91ebd46 65ef5f8 513f893 5d38db5 d754b04 65ef5f8 5d38db5 d754b04 a4b646d 5217dbe fb27fac a4b646d fb27fac a4b646d fb27fac 5d38db5 fb27fac d754b04 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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 ""
|