justineopuls commited on
Commit
7ebefe0
1 Parent(s): 3d9ed2c

edit app.py

Browse files
Files changed (8) hide show
  1. app.py +67 -118
  2. en_to_fr.png +0 -0
  3. german_to_en.jpg +0 -0
  4. gradio_queue.db +0 -0
  5. japan_to_en.png +0 -0
  6. requirements.txt +4 -2
  7. result.jpg +0 -0
  8. soccer.jpeg +0 -0
app.py CHANGED
@@ -1,132 +1,81 @@
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
- "abq",
39
- "ady",
40
- "af",
41
- "ang",
42
- "ar",
43
- "as",
44
- "ava",
45
- "az",
46
- "be",
47
- "bg",
48
- "bh",
49
- "bho",
50
- "bn",
51
- "bs",
52
- "ch_sim",
53
- "ch_tra",
54
- "che",
55
- "cs",
56
- "cy",
57
- "da",
58
- "dar",
59
- "de",
60
- "en",
61
- "es",
62
- "et",
63
- "fa",
64
- "fr",
65
- "ga",
66
- "gom",
67
- "hi",
68
- "hr",
69
- "hu",
70
- "id",
71
- "inh",
72
- "is",
73
- "it",
74
- "ja",
75
- "kbd",
76
- "kn",
77
- "ko",
78
- "ku",
79
- "la",
80
- "lbe",
81
- "lez",
82
- "lt",
83
- "lv",
84
- "mah",
85
- "mai",
86
- "mi",
87
- "mn",
88
- "mr",
89
- "ms",
90
- "mt",
91
- "ne",
92
- "new",
93
- "nl",
94
- "no",
95
- "oc",
96
- "pi",
97
- "pl",
98
- "pt",
99
- "ro",
100
- "ru",
101
- "rs_cyrillic",
102
- "rs_latin",
103
- "sck",
104
- "sk",
105
- "sl",
106
- "sq",
107
- "sv",
108
- "sw",
109
- "ta",
110
- "tab",
111
- "te",
112
- "th",
113
- "tjk",
114
- "tl",
115
- "tr",
116
- "ug",
117
- "uk",
118
- "ur",
119
- "uz",
120
- "vi"
121
- ]
122
  gr.Interface(
123
  inference,
124
- [gr.inputs.Image(type='file', label='Input'),gr.inputs.CheckboxGroup(choices, type="value", default=['en'], label='language')],
125
- [gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'confidence'])],
 
 
126
  title=title,
 
127
  description=description,
128
  article=article,
129
- examples=examples,
130
- css=css,
131
  enable_queue=True
132
  ).launch(debug=True)
 
1
+ import os
2
+ from matplotlib.pyplot import switch_backend, text
3
+ os.system('pip install paddlepaddle')
4
+ os.system('pip install paddleocr')
5
+ from paddleocr import PaddleOCR, draw_ocr
6
  from PIL import Image
 
7
  import gradio as gr
8
  import torch
 
9
 
10
+ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
 
 
 
 
 
 
11
 
12
+ model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_1.2B")
13
+ tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_1.2B")
 
 
 
 
14
 
15
+ title = 'OCR Translator'
16
+ description = 'This is a gradio demo for OCR and translating using the PaddleOCR and m2m100_418M model. It takes in an input of an image, the language to be read using OCR, and the language the result will be translated to. The PaddleOCR implementation is limited to English, Chinese, Japanese, German, and French, while the results can be translated to 100 languages.'
17
+ article = '<p>This is only a demo. The official repository can be found <a href="https://github.com/PaddlePaddle/PaddleOCR">here</a></p>'
18
+ examples = [['japan_to_en.png', 'Japanese', 'English'], ['en_to_fr.png', 'English', 'French'], ['german_to_en.jpg', 'German', 'English']]
19
+
20
+ def inference(img, src_lang, tgt_lang):
21
+ if src_lang == 'Chinese':
22
+ img_src_lang = 'ch'
23
+ tokenizer.src_lang = 'zh'
24
+ elif src_lang == 'English':
25
+ img_src_lang = 'en'
26
+ tokenizer.src_lang = 'en'
27
+ elif src_lang == 'French':
28
+ img_src_lang = 'fr'
29
+ tokenizer.src_lang = 'fr'
30
+ elif src_lang == 'Japanese':
31
+ img_src_lang = 'japan'
32
+ tokenizer.src_lang = "ja"
33
+ elif src_lang == 'German':
34
+ img_src_lang = 'german'
35
+ tokenizer.src_lang = "de"
36
+
37
+ if tgt_lang == 'Chinese':
38
+ tgt_lang = 'zh'
39
+ elif tgt_lang == 'English':
40
+ tgt_lang = 'en'
41
+ elif tgt_lang == 'French':
42
+ tgt_lang = 'fr'
43
+ elif src_lang == 'Japanese':
44
+ tgt_lang = 'ja'
45
+ elif src_lang == 'German':
46
+ tgt_lang = 'de'
47
+
48
+ # Use OCR Model
49
+ ocr = PaddleOCR(use_angle_cls = True, lang = img_src_lang, use_gpu = False)
50
+ img_path = img.name
51
+ result = ocr.ocr(img_path, cls = True)
52
+ image = Image.open(img_path).convert('RGB')
53
+ boxes = [line[0] for line in result]
54
+ txts = [line[1][0] for line in result]
55
+ im_show = draw_ocr(image, boxes, font_path = 'chinese.simfang.ttf')
56
+ im_show = Image.fromarray(im_show)
57
+ im_show.save('result.jpg')
58
+
59
+ # Parse OCR Text
60
+ input_text = ''
61
+ for txt in txts:
62
+ input_text = input_text + " " + txt
63
+
64
+ # Translate to Target Language
65
+ encoded_src = tokenizer(input_text, return_tensors="pt")
66
+ generated_tokens = model.generate(**encoded_src, forced_bos_token_id=tokenizer.get_lang_id(tgt_lang), use_cache=True)
67
+ result = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
68
+ return ['result.jpg', input_text, result]
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  gr.Interface(
71
  inference,
72
+ [gr.inputs.Image(type='file', label='Input'),
73
+ gr.inputs.Dropdown(choices=['Chinese', 'English', 'French', 'German', 'Japanese'], type="value", default='en', label='Source Language'),
74
+ gr.inputs.Dropdown(choices=['Chinese', 'English', 'French', 'German', 'Japanese'], type="value", default='en', label='Translate to')],
75
+ [gr.outputs.Image(type='file', label='Output'), gr.outputs.Textbox(label = 'Output Text'), gr.outputs.Textbox(label = 'Translated Text')],
76
  title=title,
77
+ examples=examples,
78
  description=description,
79
  article=article,
 
 
80
  enable_queue=True
81
  ).launch(debug=True)
en_to_fr.png ADDED
german_to_en.jpg ADDED
gradio_queue.db ADDED
Binary file (111 kB). View file
 
japan_to_en.png ADDED
requirements.txt CHANGED
@@ -1,4 +1,6 @@
1
  Pillow
2
- gradio
3
  torch
4
- easyocr
 
 
 
1
  Pillow
2
+ Gradio
3
  torch
4
+ transformers
5
+ numpy
6
+ sentencepiece
result.jpg ADDED
soccer.jpeg DELETED
Binary file (673 kB)