File size: 1,532 Bytes
a719225
c88e0f4
a719225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
# Load model directly
from transformers import AutoModelForImageTextToText, TrOCRProcessor
import torch
from PIL import Image

processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
model = AutoModelForImageTextToText.from_pretrained("ChronoStellar/TrOCR_IndonesianLPR")

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

import gradio as gr
from PIL import Image
import torch

# Assuming model, processor, and device are already defined

def OCR(pil_image, model=model, processor=processor, device=device):
    # Prepare image for the model
    pixel_values = processor(pil_image, return_tensors="pt").pixel_values

    # Move the input to the appropriate device (CPU/GPU)
    pixel_values = pixel_values.to(device)

    # Generate prediction
    model.eval()  # Set the model to evaluation mode
    with torch.no_grad():  # Disable gradient calculation for inference
        generated_ids = model.generate(pixel_values)

    # Decode the predicted IDs to get the text
    predicted_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    
    return predicted_text

# Create Gradio interface
interface = gr.Interface(
    fn=OCR,
    inputs=gr.Image(type="pil", label="Upload License Plate Image"),
    outputs=gr.Textbox(label="Predicted License Plate"),
    title="Automatic License Plate Recognition",
    description="Upload an image of a license plate, and the system will predict the text on it.",
)

# Launch the Gradio app
interface.launch()