venz commited on
Commit
1524fd1
β€’
1 Parent(s): 1d6134b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/chinese.jpg', 'chinese.jpg')
11
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/japanese.jpg', 'japanese.jpg')
12
+ torch.hub.download_url_to_file('https://i.imgur.com/mwQFd7G.jpeg', 'Hindi.jpeg')
13
+
14
+ def draw_boxes(image, bounds, color='yellow', width=2):
15
+ draw = ImageDraw.Draw(image)
16
+ for bound in bounds:
17
+ p0, p1, p2, p3 = bound[0]
18
+ draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
19
+ return image
20
+
21
+ def inference(img, lang):
22
+ reader = easyocr.Reader(lang)
23
+ bounds = reader.readtext(img.name)
24
+ im = PIL.Image.open(img.name)
25
+ draw_boxes(im, bounds)
26
+ im.save('result.jpg')
27
+ return ['result.jpg', pd.DataFrame(bounds).iloc[: , 1:]]
28
+
29
+ title = 'Image To Optical Character Recognition'
30
+ description = 'Multilingual OCR which works conveniently on all devices in multiple languages.'
31
+ article = "<p style='text-align: center'></p>"
32
+ examples = [['english.png',['en']],['chinese.jpg',['ch_sim', 'en']],['japanese.jpg',['ja', 'en']],['Hindi.jpeg',['hi', 'en']]]
33
+ css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
34
+ choices = [
35
+ "ch_sim",
36
+ "ch_tra",
37
+ "de",
38
+ "en",
39
+ "es",
40
+ "ja",
41
+ "hi",
42
+ "ru"
43
+ ]
44
+ gr.Interface(
45
+ inference,
46
+ [gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
47
+ [gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
48
+ title=title,
49
+ description=description,
50
+ article=article,
51
+ examples=examples,
52
+ css=css,
53
+ enable_queue=True
54
+ ).launch(debug=True)