Spaces:
Sleeping
Sleeping
from transformers import TrOCRProcessor, VisionEncoderDecoderModel | |
from PIL import Image | |
# Load model + processor | |
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1") | |
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1") | |
def extract_weight(image: Image.Image) -> str: | |
image = image.convert("RGB") | |
pixel_values = processor(images=image, return_tensors="pt").pixel_values | |
generated_ids = model.generate(pixel_values) | |
full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
# Extract digits | |
weight = ''.join(filter(lambda x: x in '0123456789.', full_text)) | |
# Check for unit in original OCR text | |
unit = "grams" # default | |
if "kg" in full_text.lower(): | |
unit = "kg" | |
elif "g" in full_text.lower(): | |
unit = "grams" | |
if weight: | |
return f"{weight} {unit}" | |
else: | |
return "No valid weight detected" | |