File size: 1,293 Bytes
513f893
65ef5f8
d22d28e
65ef5f8
d22d28e
513f893
 
65ef5f8
513f893
 
 
fb27fac
d4534d1
65ef5f8
fb27fac
 
513f893
fb27fac
d22d28e
af7cef1
 
d4534d1
 
fb27fac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
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}"