File size: 8,219 Bytes
666d384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d34f5d1
 
 
 
10c1315
 
666d384
 
 
 
 
 
 
 
10c1315
634b044
10c1315
 
 
 
 
666d384
 
 
 
 
 
10c1315
666d384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10c1315
 
 
 
 
 
 
666d384
 
 
 
 
 
10c1315
666d384
 
7805701
20d59b5
666d384
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
import gradio as gr
import requests
import json
from PIL import Image

def idcard_recognition(frame1, frame2):
    url = "http://127.0.0.1:8000/ocr/idcard"
    files = None
    if frame1 is not None and frame2 is not None:
        files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
    elif frame1 is not None and frame2 is None:
        files = {'image1': open(frame1, 'rb')}
    elif frame1 is None and frame2 is not None:
        files = {'image1': open(frame2, 'rb')}
    else:
        return ['', None]

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

    images = None   
    resultValues = {}
    table_value = ""
    for key, value in r.json().items():

        if key == 'data':
            if 'image' in value:
                del value['image']
            resultValues[key] = value
        else:
            resultValues[key] = value


    if 'data' in r.json():
        for key, value in r.json()['data'].items():
            if key == 'image':
                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))
    
    json_result = json.dumps(resultValues, indent=4)
    return [json_result, images]

def barcode_recognition(frame):
    url = "http://127.0.0.1:8000/ocr/barcode"
    files = None
    if frame is None:
        return ['', None]

    files = {'image': open(frame, 'rb')}
    r = requests.post(url=url, files=files)

    images = None   
    resultValues = {}
    table_value = ""
    for key, value in r.json().items():

        if key == 'data':
            if 'image' in value:
                del value['image']
            resultValues[key] = value
        else:
            resultValues[key] = value


    if 'data' in r.json():
        for key, value in r.json()['data'].items():
            if key == 'image':
                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))
    
    json_result = json.dumps(resultValues, indent=4)
    return [json_result, images]

def credit_recognition(frame):
    url = "http://127.0.0.1:8000/ocr/credit"
    files = None
    if frame is None:
        return ['', None]

    files = {'image': open(frame, 'rb')}
    r = requests.post(url=url, files=files)

    images = None   
    resultValues = {}
    table_value = ""
    for key, value in r.json().items():

        if key == 'data':
            if 'image' in value:
                del value['image']
            resultValues[key] = value
        else:
            resultValues[key] = value


    if 'data' in r.json():
        for key, value in r.json()['data'].items():
            if key == 'image':
                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))
    
    json_result = json.dumps(resultValues, indent=4)
    return [json_result, images]

with gr.Blocks() as demo:
    gr.Markdown(
        """
    # ID Document Recognition
    Get your own ID Document Recognition Server by duplicating this space.<br/>
    Or run on your own machine using docker.<br/>
    ```docker run -it -p 7860:7860 --platform=linux/amd64 \
	-e LICENSE_KEY="YOUR_VALUE_HERE" \
	registry.hf.space/faceonlive-id-document-recognition-sdk:latest```<br/><br/>
    Contact us at https://faceonlive.com for issues and support.<br/><br/>
    ** For security and privacy, kindly refrain from uploading real ID card or credit card information on this platform.
    """
    )
    with gr.TabItem("ID Card Recognition"):
        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')
                id_recognition_button = gr.Button("ID Card Recognition")
                id_examples = gr.Examples(
                    examples=[['samples/11.jpg', 'samples/12.jpg'], ['samples/21.jpg', 'samples/22.jpg'], ['samples/3.jpg', None], ['samples/4.jpg', None]],
                    inputs=[id_image_input1, id_image_input2],
                    outputs=None,
                    fn=idcard_recognition
                )

            with gr.Column(scale=5):
                id_result_output = gr.JSON()
        
            with gr.Column(scale=2):
                image_result_output = gr.HTML()

        id_examples.outputs = [id_result_output, image_result_output]
        id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, image_result_output])
    with gr.TabItem("Barcode Recognition"):
        with gr.Row():
            with gr.Column(scale=3):
                barcode_image_input = gr.Image(type='filepath')
                barcode_recognition_button = gr.Button("Barcode Recognition")
            with gr.Column(scale=5):
                barcode_result_output = gr.JSON()
        
            with gr.Column(scale=2):
                image_result_output = gr.HTML()
        
        barcode_recognition_button.click(barcode_recognition, inputs=barcode_image_input, outputs=[barcode_result_output, image_result_output])

    with gr.TabItem("Credit Card Recognition"):
        with gr.Row():
            with gr.Column(scale=3):
                credit_image_input = gr.Image(type='filepath')
                credit_recognition_button = gr.Button("Credit Card Recognition")
                credit_examples = gr.Examples(
                    examples=['samples/c1.jpg', 'samples/c2.jpg', 'samples/c3.jpg'],
                    inputs=credit_image_input,
                    outputs=None,
                    fn=credit_recognition
                )

            with gr.Column(scale=5):
                credit_result_output = gr.JSON()
        
            with gr.Column(scale=2):
                image_result_output = gr.HTML()
        
        credit_examples.outputs = [credit_result_output, image_result_output]
        credit_recognition_button.click(credit_recognition, inputs=credit_image_input, outputs=[credit_result_output, image_result_output])

    gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FID-Document-Recognition-SDK"><img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FFaceOnLive%2FID-Document-Recognition-SDK&labelColor=%23ff8a65&countColor=%2337d67a&style=flat&labelStyle=upper" /></a>')

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