Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import easyocr | |
| import numpy as np | |
| from PIL import Image | |
| 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 detect_lang_ocr(img,lang): | |
| try: | |
| lang = [f'{lang}'] | |
| reader = easyocr.Reader(lang) | |
| bounds = reader.readtext(img) | |
| tot_b = len(bounds) | |
| tot_num = 0 | |
| for i,bound in enumerate(bounds): | |
| #idx = (int(i) - 1) | |
| tot_num = tot_num + bound[2] | |
| out = tot_num / tot_b | |
| return out | |
| except Exception as e: | |
| print (f"Error: {e}") | |
| out = e | |
| return out | |
| with gr.Blocks() as app: | |
| im = gr.Image(type = "filepath") | |
| ocr_sens=gr.Slider(0.1, 1, step=0.05,value=0.25,label="Detect Min Confidence") | |
| lang = gr.Textbox(visible=True) | |
| max_tok=gr.Number(label="Max Tokens",step=1, value=200) | |
| det_btn = gr.Button() | |
| det_out = gr.Textbox() | |
| det_btn.click(detect_lang_ocr,[im,lang],det_out) | |
| app.queue(concurrency_count=500).launch(max_threads=40) |