Omnibus commited on
Commit
175a8ce
·
1 Parent(s): ae579d0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import easyocr
3
+
4
+
5
+
6
+ ocr_id = {
7
+ "Afrikaans": "af",
8
+ "Albanian": "sq",
9
+ "Arabic": "ar",
10
+ "Azerbaijani": "az",
11
+ "Belarusian": "be",
12
+ "Bulgarian": "bg",
13
+ "Bengali": "bn",
14
+ "Bosnian": "bs",
15
+ "Chinese (simplified)": "ch_sim",
16
+ "Chinese (traditional)": "ch_tra",
17
+ "Croatian": "hr",
18
+ "Czech": "cs",
19
+ "Danish": "da",
20
+ "Dutch": "nl",
21
+ "English": "en",
22
+ "Estonian": "et",
23
+ "French": "fr",
24
+ "German": "de",
25
+ "Irish": "ga",
26
+ "Hindi": "hi",
27
+ "Hungarian": "hu",
28
+ "Indonesian": "id",
29
+ "Icelandic": "is",
30
+ "Italian": "it",
31
+ "Japanese": "ja",
32
+ "Kannada": "kn",
33
+ "Korean": "ko",
34
+ "Lithuanian": "lt",
35
+ "Latvian": "lv",
36
+ "Mongolian": "mn",
37
+ "Marathi": "mr",
38
+ "Malay": "ms",
39
+ "Nepali": "ne",
40
+ "Norwegian": "no",
41
+ "Occitan": "oc",
42
+ "Polish": "pl",
43
+ "Portuguese": "pt",
44
+ "Romanian": "ro",
45
+ "Russian": "ru",
46
+ "Serbian (cyrillic)": "rs_cyrillic",
47
+ "Serbian (latin)": "rs_latin",
48
+ "Slovak": "sk",
49
+ "Slovenian": "sl",
50
+ "Spanish": "es",
51
+ "Swedish": "sv",
52
+ "Swahili": "sw",
53
+ "Tamil": "ta",
54
+ "Thai": "th",
55
+ "Tagalog": "tl",
56
+ "Turkish": "tr",
57
+ "Ukrainian": "uk",
58
+ "Urdu": "ur",
59
+ "Uzbek": "uz",
60
+ "Vietnamese": "vi",
61
+ "Welsh": "cy",
62
+ "Zulu": "zu",
63
+ }
64
+
65
+ def detect_lang(img,conf,blength=200):
66
+ targ = 0
67
+ ser_len=len(ocr_id)
68
+ #targ =[]
69
+ img = Image.open(img)
70
+ img1 = np.array(img)
71
+ #keyd = ocr_id.keys()
72
+ numb=0
73
+ mylist = []
74
+ for key in ocr_id.keys():
75
+ mylist.append(key)
76
+ for numb, key in enumerate(mylist):
77
+ #for i,keyp in enumerate(keyd):
78
+ #keyp = keyd[key]
79
+ try:
80
+ lang=[f"{ocr_id[mylist[numb]]}"]
81
+ #lang=value
82
+ #img.thumbnail((1000,1000), Image.Resampling.LANCZOS)
83
+ #path = f"/tmp/{uuid.uuid4()}.jpg"
84
+ #img.save(path)
85
+ reader = easyocr.Reader(lang)
86
+ bounds = reader.readtext(img1)
87
+ print (f'{bounds[0][1]} = {bounds[0][2]}')
88
+
89
+ if len(bounds) > blength:
90
+ return print(f"Max Bounds Exceed, bounds={len(bounds)}")
91
+ else:
92
+ pass
93
+ if bounds[0][2] > targ:
94
+ targ = bounds[0][2]
95
+
96
+ out = (f'{keyp} = Confidence: {bounds[0][2]}')
97
+ #numb+=1
98
+ if numb>=ser_len:
99
+ return out
100
+ if targ >=75:
101
+ return out
102
+ except Exception:
103
+
104
+ pass
105
+
106
+ with gr.Blocks() as app:
107
+ im = gr.Image(type = "filepath")
108
+ ocr_sens=gr.Slider(0.1, 1, step=0.05,value=0.25,label="Detect Min Confidence")
109
+ max_tok=gr.Number(label="Max Tokens",step=1, value=200)
110
+ det_btn = gr.Button()
111
+ det_btn.click(detect_lang,[im,ocr_sens,],det_out)
112
+ app.launch()