File size: 5,553 Bytes
0445b72
3f03254
c60cfa4
0445b72
 
6465660
0445b72
4f34618
0445b72
7b85041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6628404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
734bc82
6628404
 
 
 
 
 
 
 
 
 
 
 
 
2099764
a367ae6
215d546
2099764
 
72ab8e7
 
 
 
8d09312
4b01ddd
94abf45
f04efc7
 
4f34618
16bc2e0
 
 
4f34618
f04efc7
 
 
 
 
 
 
4f34618
16bc2e0
4f34618
a367ae6
2099764
3f03254
0445b72
 
 
 
 
 
e37ccb3
44402e2
86d74a1
 
 
e37ccb3
 
44402e2
215d546
0445b72
c690508
 
0445b72
a367ae6
 
 
2099764
 
 
 
 
 
0445b72
e37ccb3
186d961
 
 
 
 
 
 
 
 
 
 
 
 
3f03254
186d961
 
 
 
 
2099764
0445b72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import gradio as gr
import pandas as pd
import numpy as np
import easyocr
import torch
import cv2
import PIL
from PIL import ImageFont, ImageDraw, Image

lang_id = {
    "Afrikaans": "af",
    "Amharic": "am",
    "Arabic": "ar",
    "Asturian": "ast",
    "Azerbaijani": "az",
    "Bashkir": "ba",
    "Belarusian": "be",
    "Bulgarian": "bg",
    "Bengali": "bn",
    "Breton": "br",
    "Bosnian": "bs",
    "Catalan": "ca",
    "Cebuano": "ceb",
    "Czech": "cs",
    "Welsh": "cy",
    "Danish": "da",
    "German": "de",
    "Greeek": "el",
    "English": "en",
    "Spanish": "es",
    "Estonian": "et",
    "Persian": "fa",
    "Fulah": "ff",
    "Finnish": "fi",
    "French": "fr",
    "Western Frisian": "fy",
    "Irish": "ga",
    "Gaelic": "gd",
    "Galician": "gl",
    "Gujarati": "gu",
    "Hausa": "ha",
    "Hebrew": "he",
    "Hindi": "hi",
    "Croatian": "hr",
    "Haitian": "ht",
    "Hungarian": "hu",
    "Armenian": "hy",
    "Indonesian": "id",
    "Igbo": "ig",
    "Iloko": "ilo",
    "Icelandic": "is",
    "Italian": "it",
    "Japanese": "ja",
    "Javanese": "jv",
    "Georgian": "ka",
    "Kazakh": "kk",
    "Central Khmer": "km",
    "Kannada": "kn",
    "Korean": "ko",
    "Luxembourgish": "lb",
    "Ganda": "lg",
    "Lingala": "ln",
    "Lao": "lo",
    "Lithuanian": "lt",
    "Latvian": "lv",
    "Malagasy": "mg",
    "Macedonian": "mk",
    "Malayalam": "ml",
    "Mongolian": "mn",
    "Marathi": "mr",
    "Malay": "ms",
    "Burmese": "my",
    "Nepali": "ne",
    "Dutch": "nl",
    "Norwegian": "no",
    "Northern Sotho": "ns",
    "Occitan": "oc",
    "Oriya": "or",
    "Panjabi": "pa",
    "Polish": "pl",
    "Pushto": "ps",
    "Portuguese": "pt",
    "Romanian": "ro",
    "Russian": "ru",
    "Sindhi": "sd",
    "Sinhala": "si",
    "Slovak": "sk",
    "Slovenian": "sl",
    "Somali": "so",
    "Albanian": "sq",
    "Serbian": "sr",
    "Swati": "ss",
    "Sundanese": "su",
    "Swedish": "sv",
    "Swahili": "sw",
    "Tamil": "ta",
    "Thai": "th",
    "Tagalog": "tl",
    "Tswana": "tn",
    "Turkish": "tr",
    "Ukrainian": "uk",
    "Urdu": "ur",
    "Uzbek": "uz",
    "Vietnamese": "vi",
    "Wolof": "wo",
    "Xhosa": "xh",
    "Yiddish": "yi",
    "Yoruba": "yo",
    "Chinese": "zh",
    "Zulu": "zu",
}

ocr_lang=[
'abq',
'ady',
'af',
'ang',
'ar',
'as',
'ava',
'az',
'be',
'bg',
'bh',
'bho',
'bn',
'bs',
'ch_sim',
'ch_tra',
'che',
'cs',
'cy',
'da',
'dar',
'de',
'en',
'es',
'et',
'fa',
'fr',
'ga',
'gom',
'hi',
'hr',
'hu',
'id',
'inh',
'is',
'it',
'ja',
'kbd',
'kn',
'ko',
'ku',
'la',
'lbe',
'lez',
'lt',
'lv',
'mah',
'mai',
'mi',
'mn',
'mr',
'ms',
'mt',
'ne',
'new',
'nl',
'no',
'oc',
'pi',
'pl',
'pt',
'ro',
'ru',
'rs_cyrillic',
'rs_latin',
'sck',
'sk',
'sl',
'sq',
'sv',
'sw',
'ta',
'tab',
'te',
'th',
'tjk',
'tl',
'tr',
'ug',
'uk',
'ur',
'uz',
'vi',
    
]



def blur_im(img,bounds):
    im = cv2.imread(img)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    for bound in bounds:
        p0, p1, p2, p3 = bound[0]
        x = int(p0[0])
        y = int(p0[1])
        w = int(p2[0]) - int(x)
        h = int(p2[1]) - int(y)
        kernel = np.ones((5, 5), np.uint8)
        im[y:y+h, x:x+w] = cv2.dilate(im[y:y+h, x:x+w], kernel, iterations=2)
        im[y:y+h, x:x+w] = cv2.GaussianBlur(im[y:y+h, x:x+w],(51,51),0)
        
        
        #fontpath = "tamil/Latha.ttf"
    text = "New Text"
    #fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
    font = ImageFont.load("arial.pil", 40)
        #font = ImageFont.truetype(fontpath, 32)
    im = Image.fromarray(im)
    for bound in bounds:
        p0, p1, p2, p3 = bound[0]
        x = int(p0[0])
        y = int(p0[1])
        w = int(p2[0]) - int(x)
        h = int(p2[1]) - int(y)
        draw = ImageDraw.Draw(im)
        draw.text((x+5, y+5),text, font = font, fill=(0,0,0))
        #img_tamil = np.array(img_pil)   
    return im
    
def draw_boxes(image, bounds, color='blue', width=1):
    draw = ImageDraw.Draw(image)
    for bound in bounds:
        p0, p1, p2, p3 = bound[0]
        draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
    return image

def detect(img, target_lang,target_lang2=None):
    if target_lang2 != None and target_lang2 != "":
        lang=f"{lang_id[target_lang]}"
        lang2=f"{lang_id[target_lang2]}"
        lang=[lang,lang2]
    else:
        lang=[f"{lang_id[target_lang]}"]
        pass
    #global bounds
    reader = easyocr.Reader(lang)
    bounds = reader.readtext(img)
    im = PIL.Image.open(img)
    im_out=draw_boxes(im, bounds)
    #im.save('result.jpg')

    blr_out=blur_im(img,bounds)
    return im_out,blr_out,pd.DataFrame(bounds),pd.DataFrame(bounds).iloc[:,1:]


    

    
with gr.Blocks() as robot:
    with gr.Row():
        with gr.Column():
            im=gr.Image(type="filepath")
        with gr.Column():
            with gr.Row():
                target_lang = gr.Dropdown(label="Detect language", choices=list(lang_id.keys()),value="English")
                target_lang2 = gr.Dropdown(label="Detect language", choices=list(lang_id.keys()),value="")
                go_btn=gr.Button()
    with gr.Row():
        with gr.Column():
            out_im=gr.Image()
        with gr.Column():
            out_txt=gr.Textbox(lines=8)
            data_f=gr.Dataframe()
    with gr.Row():
        with gr.Column():
            trans_im=gr.Image()
        gr.Column()
                        

    go_btn.click(detect,[im,target_lang,target_lang2],[out_im,trans_im,out_txt,data_f])
robot.queue(concurrency_count=10).launch()