Sohag1 commited on
Commit
2ccef20
1 Parent(s): 4543ad8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from PIL import Image
3
+
4
+ url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg"
5
+ image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
6
+
7
+ from transformers import TrOCRProcessor
8
+
9
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
10
+ # calling the processor is equivalent to calling the feature extractor
11
+ pixel_values = processor(image, return_tensors="pt").pixel_values
12
+ print(pixel_values.shape)
13
+
14
+ from transformers import VisionEncoderDecoderModel
15
+
16
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
17
+ generated_ids = model.generate(pixel_values)
18
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
19
+ #print(generated_text)
20
+
21
+ import gradio as gr
22
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
23
+ import requests
24
+ from PIL import Image
25
+
26
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
27
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
28
+ # load image examples from the IAM database
29
+ urls = ['https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSoolxi9yWGAT5SLZShv8vVd0bz47UWRzQC19fDTeE8GmGv_Rn-PCF1pP1rrUx8kOjA4gg&usqp=CAU',
30
+ 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRNYtTuSBpZPV_nkBYPMFwVVD9asZOPgHww4epu9EqWgDmXW--sE2o8og40ZfDGo87j5w&usqp=CAU']
31
+ for idx, url in enumerate(urls):
32
+ image = Image.open(requests.get(url, stream=True).raw)
33
+ image.save(f"image_{idx}.png")
34
+
35
+ def process_image(image):
36
+ # prepare image
37
+ pixel_values = processor(image, return_tensors="pt").pixel_values
38
+
39
+ # generate (no beam search)
40
+ generated_ids = model.generate(pixel_values)
41
+
42
+ # decode
43
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
44
+
45
+ return generated_text
46
+ title = "Handwritten text Recognition Using TrOCR"
47
+ 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 an image or use the example image below and click 'submit'. Results will show up in a few seconds."
48
+ #article = "TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models | Github Repo"
49
+ examples =[["image_0.png"], ["image_1.png"], ["image_2.png"]]
50
+
51
+ iface = gr.Interface(fn=process_image,
52
+ inputs=gr.inputs.Image(type="pil"),
53
+ outputs=gr.outputs.Textbox(),
54
+ title=title,
55
+ description=description,
56
+ examples=examples)
57
+ iface.launch(inline=False)