File size: 9,064 Bytes
00855e0
 
 
 
 
 
bf9f705
d889286
 
 
bf9f705
a983494
 
284da74
a983494
00855e0
 
 
 
 
 
efd376b
00855e0
 
 
 
 
 
 
efd376b
 
 
00855e0
732ae27
00855e0
 
 
 
 
 
 
3c12d87
 
 
 
00855e0
3c12d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00855e0
3c12d87
00855e0
 
 
 
3c12d87
00855e0
 
 
 
3c12d87
00855e0
 
3c12d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00855e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a412f4
 
6a23da3
35234d4
 
 
 
 
 
 
0a412f4
cc7d5bd
0a412f4
00855e0
 
 
0a412f4
 
 
 
 
 
 
 
 
00855e0
 
453cfa5
 
00855e0
850d31b
00855e0
 
 
 
 
 
 
bf9f705
0a412f4
00855e0
c277a37
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
import gradio as gr
import requests
import datadog_api_client
from PIL import Image


def idcard_recognition(frame1, frame2):
    if frame1 is None:
        return ["Front Image Empty", None]
    
    url = "http://127.0.0.1:8080/idcard_recognition_multi_page"
    if frame2 is None:
        files = {'file1': open(frame1, 'rb')}
    else:
        files = {'file1': open(frame1, 'rb'), 'file2': open(frame2, 'rb')}

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

    html = None
    images = None
    mrz = None
    barcode = None

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

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

    if r.json().get('Barcode') is not None:
        barcode = r.json().get('Barcode')
        
    for key, value in r.json().items():
        if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position' or key == 'Barcode':
            continue

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

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

        if mrz is not None and barcode is not None:
            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{value}</td>"
                            "<td>{mrz_value}</td>"
                            "<td>{barcode_value}</td>"
                        "</tr>".format(key=key, value=value, mrz_value=mrz_value, barcode_value=barcode_value))
        elif mrz is not None:
            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{value}</td>"
                            "<td>{mrz_value}</td>"
                        "</tr>".format(key=key, value=value, mrz_value=mrz_value))
        elif barcode is not None:    
            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{value}</td>"
                            "<td>{barcode_value}</td>"
                        "</tr>".format(key=key, value=value, barcode_value=barcode_value))
        else:
            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{value}</td>"
                        "</tr>".format(key=key, value=value))

        table_value = table_value + row_value    

    if mrz is not None and barcode 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>"
                            "<td></td>"
                        "</tr>".format(key=key, value='', mrz_value=value))
            table_value = table_value + row_value
    
        for key, value in barcode.items():
            if key == 'Barcode':
                value = value.replace('<', '&lt;')
                value = value.replace(',', '<p>')
    
            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{value}</td>"
                            "<td></td>"
                            "<td>{barcode_value}</td>"
                        "</tr>".format(key=key, value='', barcode_value=value))
            table_value = table_value + row_value
    elif 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                
    elif barcode is not None:
        for key, value in barcode.items():
            if key == 'Barcode':
                value = value.replace('<', '&lt;')
                value = value.replace(',', '<p>')
    
            row_value = ("<tr>"
                            "<td>{key}</td>"
                            "<td>{value}</td>"
                            "<td>{barcode_value}</td>"
                        "</tr>".format(key=key, value='', barcode_value=value))
            table_value = table_value + row_value
    
    if mrz is not None and barcode is not None:
        html = ("<table>"
                    "<tr>"
                        "<th>Field</th>"
                        "<th>OCR</th>"
                        "<th>MRZ</th>"
                        "<th>Barcode</th>"
                    "</tr>"
                    "{table_value}"
                    "</table>".format(table_value=table_value))
    elif mrz is not None:
        html = ("<table>"
                    "<tr>"
                        "<th style=""width:20%"">Field</th>"
                        "<th style=""width:40%"">OCR</th>"
                        "<th style=""width:40%"">MRZ</th>"
                    "</tr>"
                    "{table_value}"
                    "</table>".format(table_value=table_value))
    elif barcode is not None:
        html = ("<table>"
                    "<tr>"
                        "<th style=""width:20%"">Field</th>"
                        "<th style=""width:40%"">OCR</th>"
                        "<th style=""width:40%"">Barcode</th>"
                    "</tr>"
                    "{table_value}"
                    "</table>".format(table_value=table_value))
    else:
        html = ("<table>"
                    "<tr>"
                        "<th>Field</th>"
                        "<th>OCR</th>"
                    "</tr>"
                    "{table_value}"
                    "</table>".format(table_value=table_value))
    
    table_value = ""
    for key, value in r.json().items():
        if key == 'Images':
            for image_key, image_value in value.items():
                row_value = ("<tr>"
                                "<td>{key}</td>"
                                "<td><img src=""data:image/png;base64,{base64_image} width = '200'  height= '100' /></td>"
                            "</tr>".format(key=image_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]

with gr.Blocks() as demo:
    gr.Markdown(
        """
    # KBY-AI - ID Card Recognition
    We offer SDKs for face recognition, liveness detection(anti-spoofing) and ID card recognition.

    <h4 style="display: flex; align-items: center;">
        ID Document Liveness Detection - Linux - <a href="https://web.kby-ai.com">https://web.kby-ai.com</a>
        <span>
            <img src="https://github.com/kby-ai/.github/assets/125717930/bcf351c5-8b7a-496e-a8f9-c236eb8ad59e" style="margin: 4px; width: 36px; height: 20px">
        </span>
    </h4>
    
    ##### KYC Verification Demo - https://github.com/kby-ai/KYC-Verification-Demo-Android
    ##### ID Capture Web Demo - https://cap.kby-ai.com
    ##### Documentation - Help Center - https://docs.kby-ai.com
    """
    )
    with gr.TabItem("ID Card Recognition"):
        gr.Markdown(
            """
        ##### Docker Hub - https://hub.docker.com/r/kbyai/idcard-recognition
        ```bash
        sudo docker pull kbyai/idcard-recognition:latest
        sudo docker run -e LICENSE="xxxxx" -p 8082:8080 -p 9002:9000 kbyai/idcard-recognition:latest
        ```
        """
        )
        with gr.Row():
            with gr.Column(scale=3):
                id_image_input1 = gr.Image(type='filepath', label='Front')
                id_image_input2 = gr.Image(type='filepath', label='Back')
                gr.Examples(['idcard_examples/1.jpg', 'idcard_examples/2.jpg', 'idcard_examples/3.jpg'], 
                            inputs=id_image_input1)
                id_recognition_button = gr.Button("ID Card Recognition")
            with gr.Column(scale=5):
                id_result_output = gr.HTML()
        
            with gr.Column(scale=2):
                image_result_output = gr.HTML()

        id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, image_result_output])
    gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fkby-ai%2FIDCardRecognition"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fkby-ai%2FIDCardRecognition&countColor=%23263759" /></a>')

demo.launch(server_name="0.0.0.0", server_port=7860)