Spaces:
Runtime error
Runtime error
File size: 9,218 Bytes
0445b72 3f03254 c60cfa4 0445b72 6465660 0445b72 19ca9d4 5412e8d 4f34618 eb054d8 6313927 6cc97b1 7b85041 98046f7 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 7b85041 429301e 6628404 0840e72 a367ae6 215d546 0840e72 2099764 7e2f99d a150dbe 0840e72 e748eae 0840e72 6f1535f a150dbe f04efc7 7e2f99d 0680ac2 7e2f99d f18bb89 0840e72 0680ac2 a367ae6 2099764 7e2f99d 0445b72 7e2f99d 3d1449e a150dbe 0445b72 6f1535f 0edde58 44402e2 86d74a1 e37ccb3 0edde58 3d87288 7e2f99d 5412e8d 7e2f99d 0445b72 7e2f99d 5412e8d 7e2f99d 6f1535f 2099764 0445b72 a1aed99 126b712 e37ccb3 186d961 126b712 7e2f99d 186d961 7e2f99d 0840e72 6f1535f 0840e72 7a5720e 186d961 98046f7 3f03254 98046f7 186d961 0840e72 585669b 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
import gradio as gr
import pandas as pd
import numpy as np
import easyocr
import torch
import cv2
import PIL
import sys
import os
import uuid
from PIL import ImageFont, ImageDraw, Image
this=gr.Interface.load("spaces/Omnibus/Translate-100_link")
lang_id = {
"":"",
"Afrikaans": "af",
"Albanian": "sq",
"Amharic": "am",
"Arabic": "ar",
"Armenian": "hy",
"Asturian": "ast",
"Azerbaijani": "az",
"Bashkir": "ba",
"Belarusian": "be",
"Bulgarian": "bg",
"Bengali": "bn",
"Breton": "br",
"Bosnian": "bs",
"Burmese": "my",
"Catalan": "ca",
"Cebuano": "ceb",
"Chinese": "zh",
"Chinese (simplified)": "zh",
"Chinese (traditional)": "zh",
"Croatian": "hr",
"Czech": "cs",
"Danish": "da",
"Dutch": "nl",
"English": "en",
"Estonian": "et",
"Fulah": "ff",
"Finnish": "fi",
"French": "fr",
"Western Frisian": "fy",
"Gaelic": "gd",
"Galician": "gl",
"Georgian": "ka",
"German": "de",
"Greek": "el",
"Gujarati": "gu",
"Hausa": "ha",
"Hebrew": "he",
"Hindi": "hi",
"Haitian": "ht",
"Hungarian": "hu",
"Irish": "ga",
"Indonesian": "id",
"Igbo": "ig",
"Iloko": "ilo",
"Icelandic": "is",
"Italian": "it",
"Japanese": "ja",
"Javanese": "jv",
"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",
"Nepali": "ne",
"Norwegian": "no",
"Northern Sotho": "ns",
"Occitan": "oc",
"Oriya": "or",
"Panjabi": "pa",
"Persian": "fa",
"Polish": "pl",
"Pushto": "ps",
"Portuguese": "pt",
"Romanian": "ro",
"Russian": "ru",
"Sindhi": "sd",
"Sinhala": "si",
"Slovak": "sk",
"Slovenian": "sl",
"Spanish": "es",
"Somali": "so",
"Serbian": "sr",
"Serbian (cyrillic)": "sr",
"Serbian (latin)": "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",
"Welsh": "cy",
"Wolof": "wo",
"Xhosa": "xh",
"Yiddish": "yi",
"Yoruba": "yo",
"Zulu": "zu",
}
ocr_id = {
"":"",
"Afrikaans": "af",
"Albanian": "sq",
"Arabic": "ar",
"Azerbaijani": "az",
"Belarusian": "be",
"Bulgarian": "bg",
"Bengali": "bn",
"Bosnian": "bs",
"Chinese (simplified)": "ch_sim",
"Chinese (traditional)": "ch_tra",
"Croatian": "hr",
"Czech": "cs",
"Danish": "da",
"Dutch": "nl",
"English": "en",
"Estonian": "et",
"French": "fr",
"German": "de",
"Irish": "ga",
"Hindi": "hi",
"Hungarian": "hu",
"Indonesian": "id",
"Icelandic": "is",
"Italian": "it",
"Japanese": "ja",
"Kannada": "kn",
"Korean": "ko",
"Lithuanian": "lt",
"Latvian": "lv",
"Mongolian": "mn",
"Marathi": "mr",
"Malay": "ms",
"Nepali": "ne",
"Norwegian": "no",
"Occitan": "oc",
"Polish": "pl",
"Portuguese": "pt",
"Romanian": "ro",
"Russian": "ru",
"Serbian (cyrillic)": "rs_cyrillic",
"Serbian (latin)": "rs_latin",
"Slovak": "sk",
"Slovenian": "sl",
"Spanish": "es",
"Swedish": "sv",
"Swahili": "sw",
"Tamil": "ta",
"Thai": "th",
"Tagalog": "tl",
"Turkish": "tr",
"Ukrainian": "uk",
"Urdu": "ur",
"Uzbek": "uz",
"Vietnamese": "vi",
"Welsh": "cy",
"Zulu": "zu",
}
def blur_im(img,bounds,target_lang,trans_lang,ocr_sens,font_fac,t_color):
im = cv2.imread(img)
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
if t_color == "Black":
t_fill = (0,0,0)
pass
elif t_color == "White":
t_fill = (255,255,255)
pass
for bound in bounds:
if bound[2]>=(ocr_sens):
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((3, 3), np.uint8)
if t_color=="Black":
im[y:y+h, x:x+w] = cv2.dilate(im[y:y+h, x:x+w], kernel, iterations=3)
pass
elif t_color=="White":
im[y:y+h, x:x+w] = cv2.erode(im[y:y+h, x:x+w], kernel, iterations=1)
pass
else:
pass
im[y:y+h, x:x+w] = cv2.GaussianBlur(im[y:y+h, x:x+w],(51,51),0)
else:
pass
im = Image.fromarray(im)
for bound in bounds:
if bound[2]>=(ocr_sens):
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)
text = this(bound[1],target_lang,trans_lang)
font_size=int(int(h)*font_fac)
font = ImageFont.truetype("./fonts/unifont-15.0.01.ttf", int(font_size))
draw.text((x, y),text, font = font, fill=t_fill)
else:
pass
return im
def draw_boxes(image, bounds, ocr_sens,width=1):
draw = ImageDraw.Draw(image)
for bound in bounds:
if bound[2]>=(ocr_sens):
color = "blue"
else:
color = "red"
p0, p1, p2, p3 = bound[0]
draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
return image
def detect(img, target_lang,trans_lang,ocr_sens,font_fac,t_color):
'''
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"{ocr_id[target_lang]}"]
img = Image.open(img)
img.thumbnail((1000,1000), Image.Resampling.LANCZOS)
path = f"/tmp/{uuid.uuid4()}.jpg"
img.save(path)
img1 = np.array(img)
reader = easyocr.Reader(lang)
bounds = reader.readtext(img1)
im = PIL.Image.open(path)
im_out=draw_boxes(im, bounds,ocr_sens)
blr_out=blur_im(path,bounds,target_lang,trans_lang,ocr_sens,font_fac,t_color)
return im_out,blr_out,pd.DataFrame(bounds),pd.DataFrame(bounds).iloc[:,1:]
with gr.Blocks() as robot:
gr.Markdown("""<h1><center>Translate Image to Image</center></h1><h4><center>EasyOCR and facebook/m2m100_1.2B</center></h4><h7><center>If you recieve other users images, please report it in the Community button</center></h7><h7><center>*translation may not be accurate</center></h7>""")
with gr.Accordion(label="Description",open=False):
with gr.Row():
gr.Markdown("""<p> Drop your image in the "Image to Translate" Box<br>
Select the Language to Detect in the Image<br>
Select the Language to Translate to<br>
Click "Go" Button </p>""")
gr.Markdown("""<p> Translation Model: <a href="https://huggingface.co/facebook/m2m100_1.2B">facebook/m2m100_1.2B</a><br>
OCR: <a href="https://www.jaided.ai/easyocr">easyocr</a><br>
Influence by:<br>
<a href="https://huggingface.co/spaces/venz/AW-05-GR-NLP-Image2Text-Multilingual-OCR">venz/AW-05-GR-NLP-Image2Text-Multilingual-OCR</a><br>
<a href="https://huggingface.co/spaces/Iker/Translate-100-languages">Iker/Translate-100-languages</a><br>
</p>""")
with gr.Row():
with gr.Column():
im=gr.Image(label="Image to Translate",type="filepath")
with gr.Column():
with gr.Group():
with gr.Row():
with gr.Column():
target_lang = gr.Dropdown(label="Detect language:", choices=list(ocr_id.keys()),value="English")
trans_lang = gr.Dropdown(label="Translate to:", choices=list(lang_id.keys()),value="Chinese")
with gr.Column():
ocr_sens=gr.Slider(0.1, 1, step=0.05,value=0.25,label="Detect Min Confidence")
font_fac=gr.Slider(0.1, 1, step =0.1,value=0.4,label="Font Scale")
ocr_space=gr.Slider(1,10, step=1,value=5,label="Future Function")
text_color=gr.Radio(label="Font Color",choices=["Black", "White"], value="Black")
go_btn=gr.Button("Go")
with gr.Row():
with gr.Column():
out_im=gr.Image()
with gr.Column():
trans_im=gr.Image()
with gr.Row():
out_txt=gr.Textbox(lines=8)
data_f=gr.Dataframe()
go_btn.click(detect,[im,target_lang,trans_lang,ocr_sens,font_fac,text_color],[out_im,trans_im,out_txt,data_f])
#go_btn.click(detect,[im,target_lang,target_lang2],[out_im,trans_im,out_txt,data_f])
robot.queue(concurrency_count=10).launch() |