File size: 1,548 Bytes
4659c11
 
 
 
 
f8e2b27
 
212a6ae
4659c11
 
 
 
 
 
212a6ae
 
 
a45cd99
212a6ae
a45cd99
c76156d
4659c11
 
 
 
 
 
6077eff
4b71bcf
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
import gradio as gr
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
import requests
from PIL import Image

processor = TrOCRProcessor.from_pretrained("microsoft/trocr-small-handwritten")
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-small-handwritten")
# 2 cpu and 16gib ram
def process_image(image):
    pixel_values = processor(image, return_tensors="pt").pixel_values
    generated_ids = model.generate(pixel_values)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return generated_text

title = "Transforme(encoder-decoder) based Text OCR"
description = "Demo for Microsoft's TrOCR, an encoder-decoder model \
        consisting of an image Transformer encoder and a text Transformer \
        decoder for state-of-the-art optical character recognition (OCR) on \
        single-text line images. This particular model is fine-tuned on IAM, \
        a dataset of annotated handwritten images."
article = "<p style='text-align: center'><a target='_blank' href='https://arxiv.org/abs/2109.10282'>Transformer Optical Character Recognition with Pre-trained Models</a> | <a target='_blank' href='https://github.com/microsoft/unilm/tree/master/trocr'>Github Repo</a></p>"

iface = gr.Interface(fn=process_image, 
                     inputs=gr.inputs.Image(type="pil"), 
                     outputs=gr.outputs.Textbox(),
                     title=title,
                     description=description,
                     article=article)
iface.launch(debug=False)