awacke1 commited on
Commit
d75bd2b
1 Parent(s): c4af414

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import PIL
3
+ from PIL import Image
4
+ from PIL import ImageDraw
5
+ import gradio as gr
6
+ import torch
7
+ import easyocr
8
+
9
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png', 'english.png')
10
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/thai.jpg', 'thai.jpg')
11
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/french.jpg', 'french.jpg')
12
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/chinese.jpg', 'chinese.jpg')
13
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/japanese.jpg', 'japanese.jpg')
14
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/korean.png', 'korean.png')
15
+ torch.hub.download_url_to_file('https://i.imgur.com/mwQFd7G.jpeg', 'Hindi.jpeg')
16
+
17
+ def draw_boxes(image, bounds, color='yellow', width=2):
18
+ draw = ImageDraw.Draw(image)
19
+ for bound in bounds:
20
+ p0, p1, p2, p3 = bound[0]
21
+ draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
22
+ return image
23
+
24
+ def inference(img, lang):
25
+ reader = easyocr.Reader(lang)
26
+ bounds = reader.readtext(img.name)
27
+ im = PIL.Image.open(img.name)
28
+ draw_boxes(im, bounds)
29
+ im.save('result.jpg')
30
+ return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]
31
+
32
+ title = 'EasyOCR'
33
+ description = 'Gradio demo for EasyOCR. EasyOCR demo supports 80+ languages.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
34
+ article = "<p style='text-align: center'><a href='https://www.jaided.ai/easyocr/'>Ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic and etc.</a> | <a href='https://github.com/JaidedAI/EasyOCR'>Github Repo</a></p>"
35
+ examples = [['english.png',['en']],['thai.jpg',['th']],['french.jpg',['fr', 'en']],['chinese.jpg',['ch_sim', 'en']],['japanese.jpg',['ja', 'en']],['korean.png',['ko', 'en']],['Hindi.jpeg',['hi', 'en']]]
36
+ css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
37
+ choices = [
38
+ "ch_sim",
39
+ "ch_tra",
40
+ "de",
41
+ "en",
42
+ "es",
43
+ "ja",
44
+ "ru"
45
+ ]
46
+ gr.Interface(
47
+ inference,
48
+ [gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
49
+ [gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
50
+ title=title,
51
+ description=description,
52
+ article=article,
53
+ examples=examples,
54
+ css=css,
55
+ enable_queue=True
56
+ ).launch(debug=True)