import gradio as gr import pandas as pd import numpy as np import easyocr import torch import cv2 import PIL import sys import os 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((3, 3), np.uint8) im[y:y+h, x:x+w] = cv2.dilate(im[y:y+h, x:x+w], kernel, iterations=3) im[y:y+h, x:x+w] = cv2.GaussianBlur(im[y:y+h, x:x+w],(51,51),0) #fontpath = "tamil/Latha.ttf" #text = "ポイ橋て禁" text = "New Text" font = ImageFont.load_default() #font = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40) #font = ImageFont.load("arial.pil") #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(): target_lang = gr.Dropdown(label="Detect language", choices=list(lang_id.keys()),value="English") target_lang2 = gr.Dropdown(label="Detect language2", choices=list(lang_id.keys()),value="") go_btn=gr.Button() 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,target_lang2],[out_im,trans_im,out_txt,data_f]) robot.queue(concurrency_count=10).launch()