|
|
|
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 |
|
|
|
|
|
|
|
def OCR(pil_image, model=model, processor=processor, device=device): |
|
|
|
pixel_values = processor(pil_image, return_tensors="pt").pixel_values |
|
|
|
|
|
pixel_values = pixel_values.to(device) |
|
|
|
|
|
model.eval() |
|
with torch.no_grad(): |
|
generated_ids = model.generate(pixel_values) |
|
|
|
|
|
predicted_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] |
|
|
|
return predicted_text |
|
|
|
|
|
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.", |
|
) |
|
|
|
|
|
interface.launch() |
|
|