Spaces:
Sleeping
Sleeping
from transformers import TrOCRProcessor, VisionEncoderDecoderModel | |
from PIL import Image | |
import re | |
# 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, max_length=20) | |
full_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
# For debugging (optional): | |
print("OCR Output:", full_text) | |
# Extract number (weight) | |
match = re.search(r"(\d+(\.\d+)?)", full_text) | |
if match: | |
weight = match.group(1) | |
else: | |
return "No valid weight detected" | |
# Detect unit β smarter match | |
text_lower = full_text.lower().replace(" ", "") | |
if any(unit in text_lower for unit in ["kg", "kgs", "kilogram", "kilo", "k.g"]): | |
unit = "kg" | |
elif any(unit in text_lower for unit in ["g", "gram", "grams"]): | |
unit = "grams" | |
else: | |
# Smart fallback: use value | |
if float(weight) >= 5: | |
unit = "kg" | |
else: | |
unit = "grams" | |
return f"{weight} {unit}" | |