ChronoStellar commited on
Commit
a719225
·
verified ·
1 Parent(s): 5432576

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Load model directly
2
+ from transformers import AutoTokenizer, AutoModelForImageTextToText
3
+ from transformers import VisionEncoderDecoderModel, TrOCRProcessor
4
+ import torch
5
+ import torch
6
+ from PIL import Image
7
+
8
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
9
+ model = AutoModelForImageTextToText.from_pretrained("ChronoStellar/TrOCR_IndonesianLPR")
10
+
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ model.to(device)
13
+
14
+ import gradio as gr
15
+ from PIL import Image
16
+ import torch
17
+
18
+ # Assuming model, processor, and device are already defined
19
+
20
+ def OCR(pil_image, model=model, processor=processor, device=device):
21
+ # Prepare image for the model
22
+ pixel_values = processor(pil_image, return_tensors="pt").pixel_values
23
+
24
+ # Move the input to the appropriate device (CPU/GPU)
25
+ pixel_values = pixel_values.to(device)
26
+
27
+ # Generate prediction
28
+ model.eval() # Set the model to evaluation mode
29
+ with torch.no_grad(): # Disable gradient calculation for inference
30
+ generated_ids = model.generate(pixel_values)
31
+
32
+ # Decode the predicted IDs to get the text
33
+ predicted_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
34
+
35
+ return predicted_text
36
+
37
+ # Create Gradio interface
38
+ interface = gr.Interface(
39
+ fn=OCR,
40
+ inputs=gr.Image(type="pil", label="Upload License Plate Image"),
41
+ outputs=gr.Textbox(label="Predicted License Plate"),
42
+ title="Automatic License Plate Recognition",
43
+ description="Upload an image of a license plate, and the system will predict the text on it.",
44
+ )
45
+
46
+ # Launch the Gradio app
47
+ interface.launch()