Spaces:
Running
Running
Update demo.py
Browse files
demo.py
CHANGED
@@ -4,15 +4,22 @@ import datadog_api_client
|
|
4 |
from PIL import Image
|
5 |
|
6 |
|
7 |
-
def idcard_recognition(
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
r = requests.post(url=url, files=files)
|
12 |
|
13 |
html = None
|
14 |
images = None
|
15 |
mrz = None
|
|
|
16 |
|
17 |
status = r.json().get('Status')
|
18 |
table_value = ""
|
@@ -20,8 +27,11 @@ def idcard_recognition(frame):
|
|
20 |
if r.json().get('MRZ') is not None:
|
21 |
mrz = r.json().get('MRZ')
|
22 |
|
|
|
|
|
|
|
23 |
for key, value in r.json().items():
|
24 |
-
if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position':
|
25 |
continue
|
26 |
|
27 |
mrz_value = ''
|
@@ -29,41 +39,125 @@ def idcard_recognition(frame):
|
|
29 |
mrz_value = mrz[key]
|
30 |
del mrz[key]
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
"</tr>".format(key=key, value=value, mrz_value=mrz_value))
|
37 |
-
table_value = table_value + row_value
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
if mrz is not None:
|
41 |
for key, value in mrz.items():
|
42 |
if key == 'MRZ':
|
43 |
value = value.replace('<', '<')
|
44 |
value = value.replace(',', '<p>')
|
45 |
-
|
46 |
row_value = ("<tr>"
|
47 |
"<td>{key}</td>"
|
48 |
"<td>{value}</td>"
|
49 |
"<td>{mrz_value}</td>"
|
|
|
50 |
"</tr>".format(key=key, value='', mrz_value=value))
|
51 |
table_value = table_value + row_value
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
table_value = ""
|
69 |
for key, value in r.json().items():
|
@@ -88,9 +182,10 @@ def idcard_recognition(frame):
|
|
88 |
with gr.Blocks() as demo:
|
89 |
with gr.Row():
|
90 |
with gr.Column(scale=3):
|
91 |
-
|
|
|
92 |
gr.Examples(['idcard_examples/1.jpg', 'idcard_examples/2.jpg', 'idcard_examples/3.jpg'],
|
93 |
-
inputs=
|
94 |
id_recognition_button = gr.Button("ID Card Recognition", variant="primary", size="lg")
|
95 |
with gr.Column(scale=5):
|
96 |
id_result_output = gr.HTML()
|
@@ -98,6 +193,6 @@ with gr.Blocks() as demo:
|
|
98 |
with gr.Column(scale=2):
|
99 |
image_result_output = gr.HTML()
|
100 |
|
101 |
-
id_recognition_button.click(idcard_recognition, inputs=
|
102 |
|
103 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
4 |
from PIL import Image
|
5 |
|
6 |
|
7 |
+
def idcard_recognition(frame1, frame2):
|
8 |
+
if frame1 is None:
|
9 |
+
return ["Front Image Empty", None]
|
10 |
+
|
11 |
+
url = "http://127.0.0.1:8080/idcard_recognition_multi_page"
|
12 |
+
if frame2 is None:
|
13 |
+
files = {'file1': open(frame1, 'rb')}
|
14 |
+
else:
|
15 |
+
files = {'file1': open(frame1, 'rb'), 'file2': open(frame2, 'rb')}
|
16 |
|
17 |
r = requests.post(url=url, files=files)
|
18 |
|
19 |
html = None
|
20 |
images = None
|
21 |
mrz = None
|
22 |
+
barcode = None
|
23 |
|
24 |
status = r.json().get('Status')
|
25 |
table_value = ""
|
|
|
27 |
if r.json().get('MRZ') is not None:
|
28 |
mrz = r.json().get('MRZ')
|
29 |
|
30 |
+
if r.json().get('Barcode') is not None:
|
31 |
+
barcode = r.json().get('Barcode')
|
32 |
+
|
33 |
for key, value in r.json().items():
|
34 |
+
if key == 'Status' or key == 'Images' or key == 'MRZ' or key == 'Position' or key == 'Barcode':
|
35 |
continue
|
36 |
|
37 |
mrz_value = ''
|
|
|
39 |
mrz_value = mrz[key]
|
40 |
del mrz[key]
|
41 |
|
42 |
+
barcode_value = ''
|
43 |
+
if barcode is not None and barcode.get(key) is not None:
|
44 |
+
barcode_value = barcode[key]
|
45 |
+
del barcode[key]
|
|
|
|
|
46 |
|
47 |
+
if mrz is not None and barcode is not None:
|
48 |
+
row_value = ("<tr>"
|
49 |
+
"<td>{key}</td>"
|
50 |
+
"<td>{value}</td>"
|
51 |
+
"<td>{mrz_value}</td>"
|
52 |
+
"<td>{barcode_value}</td>"
|
53 |
+
"</tr>".format(key=key, value=value, mrz_value=mrz_value, barcode_value=barcode_value))
|
54 |
+
elif mrz is not None:
|
55 |
+
row_value = ("<tr>"
|
56 |
+
"<td>{key}</td>"
|
57 |
+
"<td>{value}</td>"
|
58 |
+
"<td>{mrz_value}</td>"
|
59 |
+
"</tr>".format(key=key, value=value, mrz_value=mrz_value))
|
60 |
+
elif barcode is not None:
|
61 |
+
row_value = ("<tr>"
|
62 |
+
"<td>{key}</td>"
|
63 |
+
"<td>{value}</td>"
|
64 |
+
"<td>{barcode_value}</td>"
|
65 |
+
"</tr>".format(key=key, value=value, barcode_value=barcode_value))
|
66 |
+
else:
|
67 |
+
row_value = ("<tr>"
|
68 |
+
"<td>{key}</td>"
|
69 |
+
"<td>{value}</td>"
|
70 |
+
"</tr>".format(key=key, value=value))
|
71 |
+
|
72 |
+
table_value = table_value + row_value
|
73 |
|
74 |
+
if mrz is not None and barcode is not None:
|
75 |
for key, value in mrz.items():
|
76 |
if key == 'MRZ':
|
77 |
value = value.replace('<', '<')
|
78 |
value = value.replace(',', '<p>')
|
79 |
+
|
80 |
row_value = ("<tr>"
|
81 |
"<td>{key}</td>"
|
82 |
"<td>{value}</td>"
|
83 |
"<td>{mrz_value}</td>"
|
84 |
+
"<td></td>"
|
85 |
"</tr>".format(key=key, value='', mrz_value=value))
|
86 |
table_value = table_value + row_value
|
87 |
+
|
88 |
+
for key, value in barcode.items():
|
89 |
+
if key == 'Barcode':
|
90 |
+
value = value.replace('<', '<')
|
91 |
+
value = value.replace(',', '<p>')
|
92 |
+
|
93 |
+
row_value = ("<tr>"
|
94 |
+
"<td>{key}</td>"
|
95 |
+
"<td>{value}</td>"
|
96 |
+
"<td></td>"
|
97 |
+
"<td>{barcode_value}</td>"
|
98 |
+
"</tr>".format(key=key, value='', barcode_value=value))
|
99 |
+
table_value = table_value + row_value
|
100 |
+
elif mrz is not None:
|
101 |
+
for key, value in mrz.items():
|
102 |
+
if key == 'MRZ':
|
103 |
+
value = value.replace('<', '<')
|
104 |
+
value = value.replace(',', '<p>')
|
105 |
+
|
106 |
+
row_value = ("<tr>"
|
107 |
+
"<td>{key}</td>"
|
108 |
+
"<td>{value}</td>"
|
109 |
+
"<td>{mrz_value}</td>"
|
110 |
+
"</tr>".format(key=key, value='', mrz_value=value))
|
111 |
+
table_value = table_value + row_value
|
112 |
+
elif barcode is not None:
|
113 |
+
for key, value in barcode.items():
|
114 |
+
if key == 'Barcode':
|
115 |
+
value = value.replace('<', '<')
|
116 |
+
value = value.replace(',', '<p>')
|
117 |
+
|
118 |
+
row_value = ("<tr>"
|
119 |
+
"<td>{key}</td>"
|
120 |
+
"<td>{value}</td>"
|
121 |
+
"<td>{barcode_value}</td>"
|
122 |
+
"</tr>".format(key=key, value='', barcode_value=value))
|
123 |
+
table_value = table_value + row_value
|
124 |
+
|
125 |
+
if mrz is not None and barcode is not None:
|
126 |
+
html = ("<table>"
|
127 |
+
"<tr>"
|
128 |
+
"<th>Field</th>"
|
129 |
+
"<th>OCR</th>"
|
130 |
+
"<th>MRZ</th>"
|
131 |
+
"<th>Barcode</th>"
|
132 |
+
"</tr>"
|
133 |
+
"{table_value}"
|
134 |
+
"</table>".format(table_value=table_value))
|
135 |
+
elif mrz is not None:
|
136 |
+
html = ("<table>"
|
137 |
+
"<tr>"
|
138 |
+
"<th style=""width:20%"">Field</th>"
|
139 |
+
"<th style=""width:40%"">OCR</th>"
|
140 |
+
"<th style=""width:40%"">MRZ</th>"
|
141 |
+
"</tr>"
|
142 |
+
"{table_value}"
|
143 |
+
"</table>".format(table_value=table_value))
|
144 |
+
elif barcode is not None:
|
145 |
+
html = ("<table>"
|
146 |
+
"<tr>"
|
147 |
+
"<th style=""width:20%"">Field</th>"
|
148 |
+
"<th style=""width:40%"">OCR</th>"
|
149 |
+
"<th style=""width:40%"">Barcode</th>"
|
150 |
+
"</tr>"
|
151 |
+
"{table_value}"
|
152 |
+
"</table>".format(table_value=table_value))
|
153 |
+
else:
|
154 |
+
html = ("<table>"
|
155 |
+
"<tr>"
|
156 |
+
"<th>Field</th>"
|
157 |
+
"<th>OCR</th>"
|
158 |
+
"</tr>"
|
159 |
+
"{table_value}"
|
160 |
+
"</table>".format(table_value=table_value))
|
161 |
|
162 |
table_value = ""
|
163 |
for key, value in r.json().items():
|
|
|
182 |
with gr.Blocks() as demo:
|
183 |
with gr.Row():
|
184 |
with gr.Column(scale=3):
|
185 |
+
id_image_input1 = gr.Image(type='filepath', label='Front')
|
186 |
+
id_image_input2 = gr.Image(type='filepath', label='Back')
|
187 |
gr.Examples(['idcard_examples/1.jpg', 'idcard_examples/2.jpg', 'idcard_examples/3.jpg'],
|
188 |
+
inputs=id_image_input1)
|
189 |
id_recognition_button = gr.Button("ID Card Recognition", variant="primary", size="lg")
|
190 |
with gr.Column(scale=5):
|
191 |
id_result_output = gr.HTML()
|
|
|
193 |
with gr.Column(scale=2):
|
194 |
image_result_output = gr.HTML()
|
195 |
|
196 |
+
id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, image_result_output])
|
197 |
|
198 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|