Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
3 |
from PIL import Image
|
4 |
-
import
|
5 |
|
6 |
# Load the processor and model
|
7 |
-
processor = TrOCRProcessor.from_pretrained(
|
8 |
-
model = VisionEncoderDecoderModel.from_pretrained(
|
9 |
|
|
|
10 |
def recognize_handwriting(image):
|
11 |
-
|
12 |
-
image = image["image"]
|
13 |
-
pil_image = Image.fromarray(image).convert("RGB")
|
14 |
-
pixel_values = processor(images=pil_image, return_tensors="pt").pixel_values
|
15 |
generated_ids = model.generate(pixel_values)
|
16 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
17 |
return generated_text
|
18 |
|
19 |
-
# Gradio interface
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
)
|
31 |
|
32 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from PIL import Image
|
3 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
4 |
|
5 |
# Load the processor and model
|
6 |
+
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")
|
7 |
+
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-handwritten")
|
8 |
|
9 |
+
# Define the function to recognize handwriting
|
10 |
def recognize_handwriting(image):
|
11 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
|
|
|
|
|
|
12 |
generated_ids = model.generate(pixel_values)
|
13 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
14 |
return generated_text
|
15 |
|
16 |
+
# Create the Gradio interface
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
gr.Markdown("# Handwriting Recognition")
|
19 |
+
with gr.Row():
|
20 |
+
with gr.Column():
|
21 |
+
image_input = gr.Image(tool="editor", type="numpy", label="Draw or Upload an Image")
|
22 |
+
recognize_button = gr.Button("Recognize Handwriting")
|
23 |
+
with gr.Column():
|
24 |
+
output_text = gr.Textbox(label="Recognized Text")
|
25 |
+
|
26 |
+
recognize_button.click(fn=recognize_handwriting, inputs=image_input, outputs=output_text)
|
|
|
27 |
|
28 |
+
# Launch the Gradio app
|
29 |
+
demo.launch()
|