Ubuntu commited on
Commit
b202228
1 Parent(s): c10acce

Adding application

Browse files
app.py ADDED
@@ -0,0 +1,291 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from PIL import Image
4
+
5
+ def check_id(frame):
6
+ url = "https://api.miniai.live/api/id_check"
7
+ files = {'image': open(frame, 'rb')}
8
+
9
+ r = requests.post(url=url, files=files)
10
+
11
+ html = None
12
+ images = None
13
+ mrz = None
14
+
15
+ status = r.json().get('Status')
16
+ table_value = ""
17
+
18
+ if r.json().get('MRZ') is not None:
19
+ mrz = r.json().get('MRZ')
20
+
21
+ for key, value in r.json().items():
22
+ if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
23
+ continue
24
+
25
+ mrz_value = ''
26
+ if mrz is not None and mrz.get(key) is not None:
27
+ mrz_value = mrz[key]
28
+ del mrz[key]
29
+
30
+ row_value = ("<tr>"
31
+ "<td>{key}</td>"
32
+ "<td>{value}</td>"
33
+ "<td>{mrz_value}</td>"
34
+ "</tr>".format(key=key, value=value, mrz_value=mrz_value))
35
+ table_value = table_value + row_value
36
+
37
+
38
+ if mrz is not None:
39
+ for key, value in mrz.items():
40
+ if key == 'MRZ':
41
+ value = value.replace('<', '&lt;')
42
+ value = value.replace(',', '<p>')
43
+
44
+ row_value = ("<tr>"
45
+ "<td>{key}</td>"
46
+ "<td>{value}</td>"
47
+ "<td>{mrz_value}</td>"
48
+ "</tr>".format(key=key, value='', mrz_value=value))
49
+ table_value = table_value + row_value
50
+
51
+
52
+ html = ("<table>"
53
+ "<tr>"
54
+ "<th style=""width:20%"">Field</th>"
55
+ "<th style=""width:40%"">Value</th>"
56
+ "<th style=""width:40%"">MRZ</th>"
57
+ "</tr>"
58
+ "{table_value}"
59
+ "</table>".format(table_value=table_value))
60
+
61
+ table_value = ""
62
+ for key, value in r.json().items():
63
+ if key == 'Images':
64
+ for details_key, details_value in value.items():
65
+ for image_key, image_value in details_value.items():
66
+ if image_key == 'image':
67
+ row_value = ("<tr>"
68
+ "<td>{key}</td>"
69
+ "<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
70
+ "</tr>".format(key=details_key, base64_image=image_value))
71
+ table_value = table_value + row_value
72
+
73
+ images = ("<table>"
74
+ "<tr>"
75
+ "<th>Field</th>"
76
+ "<th>Image</th>"
77
+ "</tr>"
78
+ "{table_value}"
79
+ "</table>".format(table_value=table_value))
80
+
81
+ return [html, images]
82
+
83
+
84
+ def check_bank(frame):
85
+ url = "https://api.miniai.live/api/bank_credit_check"
86
+ files = {'image': open(frame, 'rb')}
87
+
88
+ r = requests.post(url=url, files=files)
89
+
90
+ html = None
91
+ images = None
92
+
93
+ status = r.json().get('Status')
94
+ table_value = ""
95
+
96
+ for key, value in r.json().items():
97
+ if key == 'Status' or key == 'Images' or key == 'Position':
98
+ continue
99
+
100
+ row_value = ("<tr>"
101
+ "<td>{key}</td>"
102
+ "<td>{value}</td>"
103
+ "</tr>".format(key=key, value=value))
104
+ table_value = table_value + row_value
105
+
106
+ html = ("<table>"
107
+ "<tr>"
108
+ "<th style=""width:20%"">Field</th>"
109
+ "<th style=""width:40%"">Value</th>"
110
+ "</tr>"
111
+ "{table_value}"
112
+ "</table>".format(table_value=table_value))
113
+
114
+ table_value = ""
115
+ for key, value in r.json().items():
116
+ if key == 'Images':
117
+ for details_key, details_value in value.items():
118
+ for image_key, image_value in details_value.items():
119
+ if image_key == 'image':
120
+ row_value = ("<tr>"
121
+ "<td>{key}</td>"
122
+ "<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
123
+ "</tr>".format(key=details_key, base64_image=image_value))
124
+ table_value = table_value + row_value
125
+
126
+ images = ("<table>"
127
+ "<tr>"
128
+ "<th>Field</th>"
129
+ "<th>Image</th>"
130
+ "</tr>"
131
+ "{table_value}"
132
+ "</table>".format(table_value=table_value))
133
+
134
+ return [html, images]
135
+
136
+ def check_mrz(frame):
137
+ url = "https://api.miniai.live/api/mrz_barcode_check"
138
+ files = {'image': open(frame, 'rb')}
139
+
140
+ r = requests.post(url=url, files=files)
141
+
142
+ html = None
143
+ images = None
144
+ mrz = None
145
+
146
+ status = r.json().get('Status')
147
+ table_value = ""
148
+
149
+ if r.json().get('MRZ') is not None:
150
+ mrz = r.json().get('MRZ')
151
+
152
+ for key, value in r.json().items():
153
+ if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
154
+ continue
155
+
156
+ mrz_value = ''
157
+ if mrz is not None and mrz.get(key) is not None:
158
+ mrz_value = mrz[key]
159
+ del mrz[key]
160
+
161
+ row_value = ("<tr>"
162
+ "<td>{key}</td>"
163
+ "<td>{mrz_value}</td>"
164
+ "</tr>".format(key=key, mrz_value=mrz_value))
165
+ table_value = table_value + row_value
166
+
167
+
168
+ if mrz is not None:
169
+ for key, value in mrz.items():
170
+ if key == 'MRZ':
171
+ value = value.replace('<', '&lt;')
172
+ value = value.replace(',', '<p>')
173
+
174
+ row_value = ("<tr>"
175
+ "<td>{key}</td>"
176
+ "<td>{mrz_value}</td>"
177
+ "</tr>".format(key=key, mrz_value=value))
178
+ table_value = table_value + row_value
179
+
180
+
181
+ html = ("<table>"
182
+ "<tr>"
183
+ "<th style=""width:20%"">Field</th>"
184
+ "<th style=""width:40%"">MRZ</th>"
185
+ "</tr>"
186
+ "{table_value}"
187
+ "</table>".format(table_value=table_value))
188
+
189
+ table_value = ""
190
+ for key, value in r.json().items():
191
+ if key == 'Images':
192
+ for details_key, details_value in value.items():
193
+ for image_key, image_value in details_value.items():
194
+ if image_key == 'image':
195
+ row_value = ("<tr>"
196
+ "<td>{key}</td>"
197
+ "<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
198
+ "</tr>".format(key=details_key, base64_image=image_value))
199
+ table_value = table_value + row_value
200
+
201
+ images = ("<table>"
202
+ "<tr>"
203
+ "<th>Field</th>"
204
+ "<th>Image</th>"
205
+ "</tr>"
206
+ "{table_value}"
207
+ "</table>".format(table_value=table_value))
208
+
209
+ return [html, images]
210
+
211
+
212
+ # APP Interface
213
+ with gr.Blocks() as MiniAIdemo:
214
+ gr.Markdown(
215
+ """
216
+ <a href="https://miniai.live" style="display: flex; align-items: center;">
217
+ <img src="https://miniai.live/wp-content/uploads/2024/02/logo_name-1-768x426-1.png" style="width: 18%; margin-right: 15px;"/>
218
+ <div>
219
+ <p style="font-size: 50px; font-weight: bold; margin-right: 20px;">IDSDK Web Online Demo</p>
220
+ </div>
221
+ </a>
222
+
223
+ <br/>
224
+ <ul>
225
+ <li style="font-size: 18px;">Visit and learn more about our Service : <a href="https://miniai.live" target="_blank" style="font-size: 18px;">https://www.miniai.live</a></li>
226
+ <li style="font-size: 18px;">Check our SDK for cross-platform from Github : <a href="https://github.com/MiniAiLive" target="_blank" style="font-size: 18px;">https://github.com/MiniAiLive</a></li>
227
+ <li style="font-size: 18px;">Quick view our Youtube Demo Video : <a href="https://www.youtube.com/@miniailive" target="_blank" style="font-size: 18px;">MiniAiLive Youtube Channel</a></li>
228
+ <li style="font-size: 18px;">Demo with Android device from Google Play : <a href="https://play.google.com/store/apps/dev?id=5831076207730531667" target="_blank" style="font-size: 18px;">MiniAiLive Google Play</a></li>
229
+ </ul>
230
+ <br/>
231
+ """
232
+ )
233
+ with gr.Tabs():
234
+ with gr.Tab("ID Document Recognition"):
235
+ with gr.Row():
236
+ with gr.Column(scale=3):
237
+ im_id_in = gr.Image(type='filepath', height=300)
238
+ gr.Examples(
239
+ [
240
+ "images/id/demo1.jpg",
241
+ "images/id/demo2.png",
242
+ "images/id/demo3.png",
243
+ ],
244
+ inputs=im_id_in
245
+ )
246
+ btn_f_id = gr.Button("Check Document Details", variant='primary')
247
+ with gr.Column(scale=5):
248
+ table_id_out = gr.HTML()
249
+ with gr.Column(scale=2):
250
+ im_id_out = gr.HTML()
251
+
252
+ btn_f_id.click(check_id, inputs=im_id_in, outputs=[table_id_out, im_id_out])
253
+ with gr.Tab("Bank/Credit Recognition"):
254
+ with gr.Row():
255
+ with gr.Column(scale=3):
256
+ im_bank_in = gr.Image(type='filepath', height=300)
257
+ gr.Examples(
258
+ [
259
+ "images/bank/demo1.jpg",
260
+ "images/bank/demo2.png",
261
+ "images/bank/demo3.png",
262
+ ],
263
+ inputs=im_bank_in
264
+ )
265
+ btn_f_bank = gr.Button("Check Document Details", variant='primary')
266
+ with gr.Column(scale=5):
267
+ table_bank_out = gr.HTML()
268
+ with gr.Column(scale=2):
269
+ im_bank_out = gr.HTML()
270
+
271
+ btn_f_bank.click(check_bank, inputs=im_bank_in, outputs=[table_bank_out, im_bank_out])
272
+ with gr.Tab("MRZ/Barcode Recognition"):
273
+ with gr.Row():
274
+ with gr.Column(scale=3):
275
+ im_mrz_in = gr.Image(type='filepath', height=300)
276
+ gr.Examples(
277
+ [
278
+ "images/mrz_barcode/demo1.png",
279
+ "images/mrz_barcode/demo2.png",
280
+ ],
281
+ inputs=im_mrz_in
282
+ )
283
+ btn_f_mrz = gr.Button("Check Document Details", variant='primary')
284
+ with gr.Column(scale=5):
285
+ table_mrz_out = gr.HTML()
286
+ with gr.Column(scale=2):
287
+ im_mrz_out = gr.HTML()
288
+
289
+ btn_f_mrz.click(check_mrz, inputs=im_mrz_in, outputs=[table_mrz_out, im_mrz_out])
290
+ if __name__ == "__main__":
291
+ MiniAIdemo.launch()
images/bank/demo1.jpg ADDED
images/bank/demo2.png ADDED
images/bank/demo3.png ADDED
images/id/demo1.jpg ADDED
images/id/demo2.png ADDED
images/id/demo3.png ADDED
images/mrz_barcode/demo1.png ADDED
images/mrz_barcode/demo2.png ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ requests