File size: 2,196 Bytes
1323bb3
 
 
 
88aa1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1323bb3
6e0cabb
841b400
88aa1ab
841b400
 
 
 
 
 
 
 
bf0c0c7
841b400
9bbc0a0
841b400
bf0c0c7
 
1323bb3
 
 
 
 
88aa1ab
1323bb3
 
73102cb
1323bb3
6e0cabb
10b386d
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
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)