hantech commited on
Commit
e47538b
β€’
1 Parent(s): 33c0fae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +162 -35
app.py CHANGED
@@ -1,35 +1,162 @@
1
- import os
2
- import gradio as gr
3
- import omegaconf
4
- import torch
5
- from vietocr.model.transformerocr import VietOCR
6
- from vietocr.model.vocab import Vocab
7
- from vietocr.translate import translate, process_input
8
-
9
- examples_data = os.listdir('examples')
10
- examples_data = [os.path.join('examples', line.split('\t')[0]) for line in examples_data]
11
-
12
- config = omegaconf.OmegaConf.load("vgg-seq2seq.yaml")
13
- config = omegaconf.OmegaConf.to_container(config, resolve=True)
14
-
15
- vocab = Vocab(config['vocab'])
16
- model = VietOCR(len(vocab),
17
- config['backbone'],
18
- config['cnn'],
19
- config['transformer'],
20
- config['seq_modeling'])
21
- model.load_state_dict(torch.load('train_old.pth', map_location=torch.device('cpu')))
22
-
23
- def predict(inp):
24
- img = process_input(inp, config['dataset']['image_height'],
25
- config['dataset']['image_min_width'], config['dataset']['image_max_width'])
26
- out = translate(img, model)[0].tolist()
27
- out = vocab.decode(out)
28
- return out
29
-
30
- gr.Interface(fn=predict,
31
- title='Vietnamese Handwriting Recognition',
32
- inputs=gr.Image(type='pil'),
33
- outputs=gr.Text(),
34
- examples=examples_data,
35
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import omegaconf
9
+
10
+ from vietocr.model.transformerocr import VietOCR
11
+ from vietocr.model.vocab import Vocab
12
+ from vietocr.translate import translate, process_input
13
+
14
+ config = omegaconf.OmegaConf.load("vgg-seq2seq.yaml")
15
+ config = omegaconf.OmegaConf.to_container(config, resolve=True)
16
+
17
+ vocab = Vocab(config['vocab'])
18
+ model = VietOCR(len(vocab),
19
+ config['backbone'],
20
+ config['cnn'],
21
+ config['transformer'],
22
+ config['seq_modeling'])
23
+ model.load_state_dict(torch.load('train_old.pth', map_location=torch.device('cpu')))
24
+
25
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/english.png', 'english.png')
26
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/thai.jpg', 'thai.jpg')
27
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/french.jpg', 'french.jpg')
28
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/chinese.jpg', 'chinese.jpg')
29
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/japanese.jpg', 'japanese.jpg')
30
+ torch.hub.download_url_to_file('https://github.com/JaidedAI/EasyOCR/raw/master/examples/korean.png', 'korean.png')
31
+ torch.hub.download_url_to_file('https://i.imgur.com/mwQFd7G.jpeg', 'Hindi.jpeg')
32
+
33
+ def draw_boxes(image, bounds, color='yellow', width=2):
34
+ draw = ImageDraw.Draw(image)
35
+ for bound in bounds:
36
+ p0, p1, p2, p3 = bound[0]
37
+ draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
38
+ return image
39
+
40
+ def inference(img, lang):
41
+ reader = easyocr.Reader(lang)
42
+ bounds = reader.readtext(img.name)
43
+ new_bounds=[]
44
+ for (bbox, text, prob) in bounds:
45
+ y0 = bbox[0].min()
46
+ y1 = bbox[0].max()
47
+ x0 = bbox[1].min()
48
+ x1 = bbox[1].max()
49
+
50
+ # crop the region of interest (ROI)
51
+ img = img[y0:y1, x0:x1]
52
+ img = process_input(img, config['dataset']['image_height'],
53
+ config['dataset']['image_min_width'], config['dataset']['image_max_width'])
54
+ out = translate(img, model)[0].tolist()
55
+ out = vocab.decode(out)
56
+ new_bounds.append(bbox, out, prob)
57
+ im = PIL.Image.open(img.name)
58
+ draw_boxes(im, bounds)
59
+ im.save('result.jpg')
60
+ return ['result.jpg', pd.DataFrame(new_bounds).iloc[: , 1:]]
61
+
62
+ title = 'EasyOCR'
63
+ 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.'
64
+ 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>"
65
+ 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']]]
66
+ css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
67
+ choices = [
68
+ "abq",
69
+ "ady",
70
+ "af",
71
+ "ang",
72
+ "ar",
73
+ "as",
74
+ "ava",
75
+ "az",
76
+ "be",
77
+ "bg",
78
+ "bh",
79
+ "bho",
80
+ "bn",
81
+ "bs",
82
+ "ch_sim",
83
+ "ch_tra",
84
+ "che",
85
+ "cs",
86
+ "cy",
87
+ "da",
88
+ "dar",
89
+ "de",
90
+ "en",
91
+ "es",
92
+ "et",
93
+ "fa",
94
+ "fr",
95
+ "ga",
96
+ "gom",
97
+ "hi",
98
+ "hr",
99
+ "hu",
100
+ "id",
101
+ "inh",
102
+ "is",
103
+ "it",
104
+ "ja",
105
+ "kbd",
106
+ "kn",
107
+ "ko",
108
+ "ku",
109
+ "la",
110
+ "lbe",
111
+ "lez",
112
+ "lt",
113
+ "lv",
114
+ "mah",
115
+ "mai",
116
+ "mi",
117
+ "mn",
118
+ "mr",
119
+ "ms",
120
+ "mt",
121
+ "ne",
122
+ "new",
123
+ "nl",
124
+ "no",
125
+ "oc",
126
+ "pi",
127
+ "pl",
128
+ "pt",
129
+ "ro",
130
+ "ru",
131
+ "rs_cyrillic",
132
+ "rs_latin",
133
+ "sck",
134
+ "sk",
135
+ "sl",
136
+ "sq",
137
+ "sv",
138
+ "sw",
139
+ "ta",
140
+ "tab",
141
+ "te",
142
+ "th",
143
+ "tjk",
144
+ "tl",
145
+ "tr",
146
+ "ug",
147
+ "uk",
148
+ "ur",
149
+ "uz",
150
+ "vi"
151
+ ]
152
+ gr.Interface(
153
+ inference,
154
+ [gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
155
+ [gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
156
+ title=title,
157
+ description=description,
158
+ article=article,
159
+ examples=examples,
160
+ css=css,
161
+ enable_queue=True
162
+ ).launch(debug=True)