File size: 12,368 Bytes
b202228
 
 
 
 
f09357c
b202228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f09357c
b202228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f09357c
b202228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef3ca9e
b202228
 
 
cd03f3c
 
 
 
 
 
 
 
 
 
 
 
 
 
b202228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4953a95
b202228
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import gradio as gr
import requests
from PIL import Image

def check_id(frame):
    url = "https://api.miniai.live/api/check_id"
    files = {'image': open(frame, 'rb')}

    r = requests.post(url=url, files=files)

    html = None
    images = None
    mrz = None

    status = r.json().get('Status')
    table_value = ""

    if r.json().get('MRZ') is not None:
        mrz = r.json().get('MRZ')

    for key, value in r.json().items():
        if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
            continue

        mrz_value = ''
        if mrz is not None and mrz.get(key) is not None:
            mrz_value = mrz[key]
            del mrz[key]

        row_value = ("<tr>"
                        "<td>{key}</td>"
                        "<td>{value}</td>"
                        "<td>{mrz_value}</td>"
                    "</tr>".format(key=key, value=value, mrz_value=mrz_value))
        table_value = table_value + row_value


    if mrz is not None:
        for key, value in mrz.items():
            if key == 'MRZ':
                value = value.replace('<', '&lt;')
                value = value.replace(',', '<p>')

            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{value}</td>"
                            "<td>{mrz_value}</td>"
                        "</tr>".format(key=key, value='', mrz_value=value))
            table_value = table_value + row_value
            

    html = ("<table>"
                "<tr>"
                    "<th style=""width:20%"">Field</th>"
                    "<th style=""width:40%"">Value</th>"
                    "<th style=""width:40%"">MRZ</th>"
                "</tr>"
                "{table_value}"
                "</table>".format(table_value=table_value))
    
    table_value = ""
    for key, value in r.json().items():
        if key == 'Images':
            for details_key, details_value in value.items():
                for image_key, image_value in details_value.items():
                    if image_key == 'image':
                        row_value = ("<tr>"
                                        "<td>{key}</td>"
                                        "<td><img src=""data:image/png;base64,{base64_image} width = '200'  height= '100' /></td>"
                                    "</tr>".format(key=details_key, base64_image=image_value))
                        table_value = table_value + row_value

    images = ("<table>"
                "<tr>"
                    "<th>Field</th>"
                    "<th>Image</th>"
                "</tr>"
                "{table_value}"
                "</table>".format(table_value=table_value))
    
    return [html, images]


def check_bank(frame):
    url = "https://api.miniai.live/api/check_credit"
    files = {'image': open(frame, 'rb')}

    r = requests.post(url=url, files=files)

    html = None
    images = None

    status = r.json().get('Status')
    table_value = ""

    for key, value in r.json().items():
        if key == 'Status' or key == 'Images' or key == 'Position':
            continue

        row_value = ("<tr>"
                        "<td>{key}</td>"
                        "<td>{value}</td>"
                    "</tr>".format(key=key, value=value))
        table_value = table_value + row_value

    html = ("<table>"
                "<tr>"
                    "<th style=""width:20%"">Field</th>"
                    "<th style=""width:40%"">Value</th>"
                "</tr>"
                "{table_value}"
                "</table>".format(table_value=table_value))
    
    table_value = ""
    for key, value in r.json().items():
        if key == 'Images':
            for details_key, details_value in value.items():
                for image_key, image_value in details_value.items():
                    if image_key == 'image':
                        row_value = ("<tr>"
                                        "<td>{key}</td>"
                                        "<td><img src=""data:image/png;base64,{base64_image} width = '200'  height= '100' /></td>"
                                    "</tr>".format(key=details_key, base64_image=image_value))
                        table_value = table_value + row_value

    images = ("<table>"
                "<tr>"
                    "<th>Field</th>"
                    "<th>Image</th>"
                "</tr>"
                "{table_value}"
                "</table>".format(table_value=table_value))
    
    return [html, images]

def check_mrz(frame):
    url = "https://api.miniai.live/api/check_mrz"
    files = {'image': open(frame, 'rb')}

    r = requests.post(url=url, files=files)

    html = None
    images = None
    mrz = None

    status = r.json().get('Status')
    table_value = ""

    if r.json().get('MRZ') is not None:
        mrz = r.json().get('MRZ')

    for key, value in r.json().items():
        if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
            continue

        mrz_value = ''
        if mrz is not None and mrz.get(key) is not None:
            mrz_value = mrz[key]
            del mrz[key]

        row_value = ("<tr>"
                        "<td>{key}</td>"
                        "<td>{mrz_value}</td>"
                    "</tr>".format(key=key, mrz_value=mrz_value))
        table_value = table_value + row_value


    if mrz is not None:
        for key, value in mrz.items():
            if key == 'MRZ':
                value = value.replace('<', '&lt;')
                value = value.replace(',', '<p>')

            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{mrz_value}</td>"
                        "</tr>".format(key=key, mrz_value=value))
            table_value = table_value + row_value
            

    html = ("<table>"
                "<tr>"
                    "<th style=""width:20%"">Field</th>"
                    "<th style=""width:40%"">MRZ</th>"
                "</tr>"
                "{table_value}"
                "</table>".format(table_value=table_value))
    
    table_value = ""
    for key, value in r.json().items():
        if key == 'Images':
            for details_key, details_value in value.items():
                for image_key, image_value in details_value.items():
                    if image_key == 'image':
                        row_value = ("<tr>"
                                        "<td>{key}</td>"
                                        "<td><img src=""data:image/png;base64,{base64_image} width = '200'  height= '100' /></td>"
                                    "</tr>".format(key=details_key, base64_image=image_value))
                        table_value = table_value + row_value

    images = ("<table>"
                "<tr>"
                    "<th>Field</th>"
                    "<th>Image</th>"
                "</tr>"
                "{table_value}"
                "</table>".format(table_value=table_value))
    
    return [html, images]


# APP Interface
with gr.Blocks() as MiniAIdemo:
    gr.Markdown(
        """
        <a href="https://miniai.live" style="display: flex; align-items: center;">
            <img src="https://miniai.live/wp-content/uploads/2024/02/logo_name-1-768x426-1.png" style="width: 18%; margin-right: 15px;"/>
            <div>
                <p style="font-size: 50px; font-weight: bold; margin-right: 20px;">ID Document Recognition WebAPI Demo</p>
            </div>
        </a>
        <br/>
        <div style="display: flex; justify-content: center; align-items: center;"> 
           <table style="text-align: center;">
              <tr>
                 <td style="text-align: center; vertical-align: middle;"><a href="https://github.com/MiniAiLive"><img src="https://miniai.live/wp-content/uploads/2024/10/new_git-1-300x67.png" style="height: 50px; margin-right: 5px;" title="GITHUB"/></a></td> 
                 <td style="text-align: center; vertical-align: middle;"><a href="https://huggingface.co/MiniAiLive"><img src="https://miniai.live/wp-content/uploads/2024/10/new_hugging-1-300x67.png" style="height: 50px; margin-right: 5px;" title="HuggingFace"/></a></td> 
                 <td style="text-align: center; vertical-align: middle;"><a href="https://demo.miniai.live"><img src="https://miniai.live/wp-content/uploads/2024/10/new_gradio-300x67.png" style="height: 50px; margin-right: 5px;" title="Gradio"/></a></td> 
              </tr> 
              <tr>
                 <td style="text-align: center; vertical-align: middle;"><a href="https://docs.miniai.live/"><img src="https://miniai.live/wp-content/uploads/2024/10/a-300x70.png" style="height: 50px; margin-right: 5px;" title="Documentation"/></a></td> 
                 <td style="text-align: center; vertical-align: middle;"><a href="https://www.youtube.com/@miniailive"><img src="https://miniai.live/wp-content/uploads/2024/10/Untitled-1-300x70.png" style="height: 50px; margin-right: 5px;" title="Youtube"/></a></td> 
                 <td style="text-align: center; vertical-align: middle;"><a href="https://play.google.com/store/apps/dev?id=5831076207730531667"><img src="https://miniai.live/wp-content/uploads/2024/10/googleplay-300x62.png" style="height: 50px; margin-right: 5px;" title="Google Play"/></a></td>
              </tr>
           </table>
        </div>
        <br/>
        """
    )
    with gr.Tabs():
        with gr.Tab("ID Document Recognition"):
            with gr.Row():
                with gr.Column(scale=3):
                    im_id_in = gr.Image(type='filepath', height=300)
                    gr.Examples(
                        [
                            "images/id/demo1.jpg",
                            "images/id/demo2.png",
                            "images/id/demo3.png",
                        ],
                        inputs=im_id_in
                    )
                    btn_f_id = gr.Button("Check Document Details", variant='primary')
                with gr.Column(scale=5):
                    table_id_out = gr.HTML()
                with gr.Column(scale=2):
                    im_id_out = gr.HTML()

            btn_f_id.click(check_id, inputs=im_id_in, outputs=[table_id_out, im_id_out])
        with gr.Tab("Bank/Credit Recognition"):
            with gr.Row():
                with gr.Column(scale=3):
                    im_bank_in = gr.Image(type='filepath', height=300)
                    gr.Examples(
                        [
                            "images/bank/demo1.jpg",
                            "images/bank/demo2.png",
                            "images/bank/demo3.png",
                        ],
                        inputs=im_bank_in
                    )
                    btn_f_bank = gr.Button("Check Document Details", variant='primary')
                with gr.Column(scale=5):
                    table_bank_out = gr.HTML()
                with gr.Column(scale=2):
                    im_bank_out = gr.HTML()

            btn_f_bank.click(check_bank, inputs=im_bank_in, outputs=[table_bank_out, im_bank_out])
        with gr.Tab("MRZ/Barcode Recognition"):
            with gr.Row():
                with gr.Column(scale=3):
                    im_mrz_in = gr.Image(type='filepath', height=300)
                    gr.Examples(
                        [
                            "images/mrz_barcode/demo1.png",
                            "images/mrz_barcode/demo2.png",
                        ],
                        inputs=im_mrz_in
                    )
                    btn_f_mrz = gr.Button("Check Document Details", variant='primary')
                with gr.Column(scale=5):
                    table_mrz_out = gr.HTML()
                with gr.Column(scale=2):
                    im_mrz_out = gr.HTML()

            btn_f_mrz.click(check_mrz, inputs=im_mrz_in, outputs=[table_mrz_out, im_mrz_out])
    gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FID-Document-Recognition-Demo"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiniAiLive%2FID-Document-Recognition-Demo&label=VISITORS&labelColor=%2337d67a&countColor=%23ff8a65&style=plastic&labelStyle=none" /></a>')
if __name__ == "__main__":
    MiniAIdemo.launch()