File size: 939 Bytes
513f893
65ef5f8
 
d4534d1
513f893
 
65ef5f8
513f893
 
 
 
d4534d1
65ef5f8
d4534d1
 
513f893
d4534d1
 
 
 
 
 
1cb8b90
d4534d1
 
 
 
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
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"