3v324v23 commited on
Commit
9bfe77c
1 Parent(s): 20b503c

Add application file

Browse files
Files changed (2) hide show
  1. app.py +48 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ import requests
4
+ from PIL import Image
5
+
6
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-small-printed")
7
+ model = VisionEncoderDecoderModel.from_pretrained("tomofi/trocr-captcha")
8
+
9
+ # load image examples
10
+ urls = [
11
+ 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nfcb5.png',
12
+ 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/p57fn.png',
13
+ 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w2yp7.png',
14
+ 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/pme86.png',
15
+ 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w4nfx.png',
16
+ 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nf8b8.png'
17
+ ]
18
+ for idx, url in enumerate(urls):
19
+ image = Image.open(requests.get(url, stream=True).raw)
20
+ image.save(f"image_{idx}.png")
21
+
22
+ def process_image(image):
23
+ # prepare image
24
+ pixel_values = processor(image, return_tensors="pt").pixel_values
25
+
26
+ # generate (no beam search)
27
+ generated_ids = model.generate(pixel_values)
28
+
29
+ # decode
30
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
31
+
32
+ return generated_text
33
+
34
+ title = "TrOCR for Captcha"
35
+ 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. To use it, simply upload a (single-text line) image or use one of the example images below and click 'submit'. Results will show up in a few seconds."
36
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.10282'>TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models</a> | <a href='https://github.com/microsoft/unilm/tree/master/trocr'>Github Repo</a></p>"
37
+ examples =[["image_0.png"], ["image_1.png"], ["image_2.png"], ["image_3.png"], ["image_4.png"], ["image_5.png"]]
38
+
39
+ #css = """.output_image, .input_image {height: 600px !important}"""
40
+
41
+ iface = gr.Interface(fn=process_image,
42
+ inputs=gr.inputs.Image(type="pil"),
43
+ outputs=gr.outputs.Textbox(),
44
+ title=title,
45
+ description=description,
46
+ article=article,
47
+ examples=examples)
48
+ iface.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ Pillow
4
+ git+https://github.com/huggingface/transformers.git