Delete app.py
Browse files
app.py
DELETED
@@ -1,291 +0,0 @@
|
|
1 |
-
import sys
|
2 |
-
import os
|
3 |
-
import io
|
4 |
-
import gradio as gr
|
5 |
-
import json
|
6 |
-
import requests
|
7 |
-
from PIL import Image
|
8 |
-
|
9 |
-
css = """
|
10 |
-
.example-image img{
|
11 |
-
display: flex; /* Use flexbox to align items */
|
12 |
-
justify-content: center; /* Center the image horizontally */
|
13 |
-
align-items: center; /* Center the image vertically */
|
14 |
-
height: 300px; /* Set the height of the container */
|
15 |
-
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
16 |
-
}
|
17 |
-
.example-image img{
|
18 |
-
display: flex; /* Use flexbox to align items */
|
19 |
-
text-align: center;
|
20 |
-
justify-content: center; /* Center the image horizontally */
|
21 |
-
align-items: center; /* Center the image vertically */
|
22 |
-
height: 350px; /* Set the height of the container */
|
23 |
-
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
24 |
-
}
|
25 |
-
|
26 |
-
.markdown-success-container {
|
27 |
-
background-color: #F6FFED;
|
28 |
-
padding: 20px;
|
29 |
-
margin: 20px;
|
30 |
-
border-radius: 1px;
|
31 |
-
border: 2px solid green;
|
32 |
-
text-align: center;
|
33 |
-
}
|
34 |
-
|
35 |
-
.markdown-fail-container {
|
36 |
-
background-color: #FFF1F0;
|
37 |
-
padding: 20px;
|
38 |
-
margin: 20px;
|
39 |
-
border-radius: 1px;
|
40 |
-
border: 2px solid red;
|
41 |
-
text-align: center;
|
42 |
-
}
|
43 |
-
|
44 |
-
.block-background {
|
45 |
-
# background-color: #202020; /* Set your desired background color */
|
46 |
-
border-radius: 5px;
|
47 |
-
}
|
48 |
-
"""
|
49 |
-
|
50 |
-
screenReplayThreshold = 0.5
|
51 |
-
portraitReplaceThreshold = 0.5
|
52 |
-
printedCopyThreshold = 0.5
|
53 |
-
|
54 |
-
def find_key_in_dict(d, target_key):
|
55 |
-
for key, value in d.items():
|
56 |
-
if key == target_key:
|
57 |
-
return value
|
58 |
-
elif isinstance(value, dict): # If the value is a dictionary, search recursively
|
59 |
-
result = find_key_in_dict(value, target_key)
|
60 |
-
if result is not None:
|
61 |
-
return result
|
62 |
-
return None
|
63 |
-
|
64 |
-
def json_to_html_table(data, image_keys):
|
65 |
-
html = "<table border='1' style='border-collapse: collapse; width: 100%;'>"
|
66 |
-
for key, value in data.items():
|
67 |
-
if isinstance(value, dict):
|
68 |
-
html += f"<tr><td colspan='2'><strong>{key}</strong></td></tr>"
|
69 |
-
for sub_key, sub_value in value.items():
|
70 |
-
if sub_key in image_keys:
|
71 |
-
html += f"<tr><td>{sub_key}</td><td><img src='data:image/png;base64,{sub_value}' width = '200' height= '100' /></td></tr>"
|
72 |
-
else:
|
73 |
-
html += f"<tr><td>{sub_key}</td><td>{sub_value}</td></tr>"
|
74 |
-
else:
|
75 |
-
if key in image_keys:
|
76 |
-
html += f"<tr><td>{key}</td><td><img src='data:image/png;base64,{value}' width = '200' height= '100' /></td></tr>"
|
77 |
-
else:
|
78 |
-
html += f"<tr><td>{key}</td><td>{value}</td></tr>"
|
79 |
-
|
80 |
-
html += "</table>"
|
81 |
-
return html
|
82 |
-
|
83 |
-
def check_liveness(frame):
|
84 |
-
if frame is None:
|
85 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
86 |
-
return [liveness_result, {"status": "error", "result": "select image file!"}]
|
87 |
-
|
88 |
-
img_bytes = io.BytesIO()
|
89 |
-
Image.open(frame).save(img_bytes, format="JPEG")
|
90 |
-
img_bytes.seek(0)
|
91 |
-
|
92 |
-
url = "https://recognito-iddocumentlivenessdetection.p.rapidapi.com/process_image"
|
93 |
-
|
94 |
-
try:
|
95 |
-
files = {'image': img_bytes}
|
96 |
-
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
97 |
-
|
98 |
-
result = requests.post(url=url, files=files, headers=headers)
|
99 |
-
except:
|
100 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
101 |
-
return [liveness_result, {"status": "error", "result": "failed to open file!"}]
|
102 |
-
|
103 |
-
if result.ok:
|
104 |
-
json_result = result.json()
|
105 |
-
if json_result.get("resultCode") == "Error":
|
106 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
107 |
-
return [liveness_result, {"status": "error", "result": "server error!"}]
|
108 |
-
|
109 |
-
process_results = json_result.get("result")
|
110 |
-
status = process_results.get("status")
|
111 |
-
if status == "Ok":
|
112 |
-
screenReply = process_results.get("screenReply")
|
113 |
-
portraitReplace = process_results.get("portraitReplace")
|
114 |
-
printedCopy = process_results.get("printedCopy")
|
115 |
-
liveness_result = f"""<div class="markdown-success-container"><p style="text-align: center; font-size: 20px; color: green;">Liveness Check: GENUINE</p></div>"""
|
116 |
-
|
117 |
-
# Check for "Spoof" condition
|
118 |
-
if screenReply < screenReplayThreshold or portraitReplace < portraitReplaceThreshold or printedCopy < printedCopyThreshold:
|
119 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check: SPOOF</p></div>"""
|
120 |
-
|
121 |
-
json_output = {"Screen Replay Check": "Failed" if screenReply < screenReplayThreshold else "Success",
|
122 |
-
"Portrait Replace Check": "Failed" if portraitReplace < portraitReplaceThreshold else "Success",
|
123 |
-
"Printed Cutout Check": "Failed" if printedCopy < printedCopyThreshold else "Success"}
|
124 |
-
# Update json_result with the modified process_results
|
125 |
-
return [liveness_result, json_output]
|
126 |
-
|
127 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
128 |
-
return [liveness_result, {"status": "error", "result": "document not found!"}]
|
129 |
-
else:
|
130 |
-
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed</p></div>"""
|
131 |
-
return [liveness_result, {"status": "error", "result": f"{result.text}"}]
|
132 |
-
|
133 |
-
def idcard_recognition(frame1, frame2):
|
134 |
-
url = "https://recognito-iddocumentrecognition.p.rapidapi.com/api/read_idcard"
|
135 |
-
|
136 |
-
files = None
|
137 |
-
if frame1 is not None and frame2 is not None:
|
138 |
-
files = {'image': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
|
139 |
-
elif frame1 is not None and frame2 is None:
|
140 |
-
files = {'image': open(frame1, 'rb')}
|
141 |
-
elif frame1 is None and frame2 is not None:
|
142 |
-
files = {'image': open(frame2, 'rb')}
|
143 |
-
else:
|
144 |
-
return ['', None, None]
|
145 |
-
|
146 |
-
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
147 |
-
|
148 |
-
r = requests.post(url=url, files=files, headers=headers)
|
149 |
-
|
150 |
-
images = None
|
151 |
-
rawValues = {}
|
152 |
-
image_table_value = ""
|
153 |
-
result_table_dict = {
|
154 |
-
'portrait':'',
|
155 |
-
'documentName':'',
|
156 |
-
'score':'',
|
157 |
-
'countryName':'',
|
158 |
-
'name':'',
|
159 |
-
'sex':'',
|
160 |
-
'address':'',
|
161 |
-
'dateOfBirth':'',
|
162 |
-
'dateOfIssue':'',
|
163 |
-
'dateOfExpiry':'',
|
164 |
-
'documentNumber':'',
|
165 |
-
}
|
166 |
-
|
167 |
-
if 'data' in r.json():
|
168 |
-
for key, value in r.json()['data'].items():
|
169 |
-
if key == 'image':
|
170 |
-
for image_key, image_value in value.items():
|
171 |
-
row_value = ("<tr>"
|
172 |
-
"<td>{key}</td>"
|
173 |
-
"<td><img src=""data:image/png;base64,{base64_image} width = '200' height= '100' /></td>"
|
174 |
-
"</tr>".format(key=image_key, base64_image=image_value))
|
175 |
-
image_table_value = image_table_value + row_value
|
176 |
-
|
177 |
-
images = ("<table>"
|
178 |
-
"<tr>"
|
179 |
-
"<th>Field</th>"
|
180 |
-
"<th>Image</th>"
|
181 |
-
"</tr>"
|
182 |
-
"{image_table_value}"
|
183 |
-
"</table>".format(image_table_value=image_table_value))
|
184 |
-
|
185 |
-
for key, value in r.json().items():
|
186 |
-
if key == 'data':
|
187 |
-
if 'image' in value:
|
188 |
-
del value['image']
|
189 |
-
rawValues[key] = value
|
190 |
-
else:
|
191 |
-
rawValues[key] = value
|
192 |
-
|
193 |
-
|
194 |
-
for result_key in result_table_dict.keys():
|
195 |
-
result_table_dict[result_key] = find_key_in_dict(r.json(), result_key)
|
196 |
-
|
197 |
-
result = json_to_html_table(result_table_dict, {'portrait'})
|
198 |
-
json_result = json.dumps(rawValues, indent=6)
|
199 |
-
return [result, json_result, images]
|
200 |
-
|
201 |
-
def launch_demo():
|
202 |
-
with gr.Blocks(css=css) as demo:
|
203 |
-
gr.Markdown(
|
204 |
-
f"""
|
205 |
-
<a href="https://recognito.vision" style="display: flex; align-items: center;">
|
206 |
-
<img src="https://recognito.vision/wp-content/uploads/2024/03/Recognito-modified.png" style="width: 8%; margin-right: 15px;"/>
|
207 |
-
<div>
|
208 |
-
<p style="font-size: 32px; font-weight: bold; margin: 0;">Recognito</p>
|
209 |
-
<p style="font-size: 18px; margin: 0;">www.recognito.vision</p>
|
210 |
-
</div>
|
211 |
-
</a>
|
212 |
-
<p style="font-size: 20px; font-weight: bold;">📘 Product Documentation</p>
|
213 |
-
<div style="display: flex; align-items: center;">
|
214 |
-
  <a href="https://docs.recognito.vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/05/book.png" style="width: 48px; margin-right: 5px;"/></a>
|
215 |
-
</div>
|
216 |
-
<p style="font-size: 20px; font-weight: bold;">🏠 Visit Recognito</p>
|
217 |
-
<div style="display: flex; align-items: center;">
|
218 |
-
  <a href="https://recognito.vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/recognito_64_cl.png" style="width: 32px; margin-right: 5px;"/></a>
|
219 |
-
<a href="https://www.linkedin.com/company/recognito-vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/linkedin_64_cl.png" style="width: 32px; margin-right: 5px;"/></a>
|
220 |
-
<a href="https://huggingface.co/recognito" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/hf_64_cl.png" style="width: 32px; margin-right: 5px;"/></a>
|
221 |
-
<a href="https://github.com/recognito-vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/github_64_cl.png" style="width: 32px; margin-right: 5px;"/></a>
|
222 |
-
<a href="https://hub.docker.com/u/recognito" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/03/docker_64_cl.png" style="width: 32px; margin-right: 5px;"/></a>
|
223 |
-
<a href="https://www.youtube.com/@recognito-vision" style="display: flex; align-items: center;"><img src="https://recognito.vision/wp-content/uploads/2024/04/youtube_64_cl.png" style="width: 32px; margin-right: 5px;"/></a>
|
224 |
-
</div>
|
225 |
-
<p style="font-size: 20px; font-weight: bold;">🤝 Contact us for our on-premise ID Document Verification SDKs deployment</p>
|
226 |
-
<div style="display: flex; align-items: center;">
|
227 |
-
  <a target="_blank" href="mailto:[email protected]"><img src="https://img.shields.io/badge/[email protected]?logo=gmail " alt="www.recognito.vision"></a>
|
228 |
-
<a target="_blank" href="https://wa.me/+14158003112"><img src="https://img.shields.io/badge/whatsapp-+14158003112-blue.svg?logo=whatsapp " alt="www.recognito.vision"></a>
|
229 |
-
<a target="_blank" href="https://t.me/recognito_vision"><img src="https://img.shields.io/badge/telegram-@recognito__vision-blue.svg?logo=telegram " alt="www.recognito.vision"></a>
|
230 |
-
<a target="_blank" href="https://join.slack.com/t/recognito-workspace/shared_invite/zt-2d4kscqgn-"><img src="https://img.shields.io/badge/slack-recognito__workspace-blue.svg?logo=slack " alt="www.recognito.vision"></a>
|
231 |
-
</div>
|
232 |
-
<br/>
|
233 |
-
"""
|
234 |
-
)
|
235 |
-
|
236 |
-
with gr.Tabs():
|
237 |
-
with gr.Tab("ID Document Recognition"):
|
238 |
-
with gr.Row():
|
239 |
-
with gr.Column(scale=6):
|
240 |
-
with gr.Row():
|
241 |
-
with gr.Column(scale=3):
|
242 |
-
id_image_input1 = gr.Image(type='filepath', label='Front', elem_classes="example-image")
|
243 |
-
with gr.Column(scale=3):
|
244 |
-
id_image_input2 = gr.Image(type='filepath', label='Back', elem_classes="example-image")
|
245 |
-
|
246 |
-
with gr.Row():
|
247 |
-
id_examples = gr.Examples(
|
248 |
-
examples=[['examples/1_f.png', 'examples/1_b.png'],
|
249 |
-
['examples/2_f.png', 'examples/2_b.png'],
|
250 |
-
['examples/3_f.png', 'examples/3_b.png'],
|
251 |
-
['examples/4.png', None]],
|
252 |
-
inputs=[id_image_input1, id_image_input2],
|
253 |
-
outputs=None,
|
254 |
-
fn=idcard_recognition
|
255 |
-
)
|
256 |
-
|
257 |
-
with gr.Blocks():
|
258 |
-
with gr.Column(scale=4, min_width=400, elem_classes="block-background"):
|
259 |
-
id_recognition_button = gr.Button("ID Card Recognition", variant="primary", size="lg")
|
260 |
-
|
261 |
-
with gr.Tab("Key Fields"):
|
262 |
-
id_result_output = gr.HTML()
|
263 |
-
with gr.Tab("Raw JSON"):
|
264 |
-
json_result_output = gr.JSON()
|
265 |
-
with gr.Tab("Images"):
|
266 |
-
image_result_output = gr.HTML()
|
267 |
-
|
268 |
-
id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, json_result_output, image_result_output])
|
269 |
-
|
270 |
-
with gr.Tab("ID Document Liveness Detection"):
|
271 |
-
with gr.Row():
|
272 |
-
with gr.Column(scale=1):
|
273 |
-
id_image_input = gr.Image(label="Image", type='filepath', elem_classes="example-image")
|
274 |
-
gr.Examples(examples=['examples/1_f.png', 'examples/2_f.png', 'examples/3_f.png', 'examples/4.png'], inputs=id_image_input)
|
275 |
-
|
276 |
-
with gr.Blocks():
|
277 |
-
with gr.Column(scale=1, elem_classes="block-background"):
|
278 |
-
check_liveness_button = gr.Button("Check Document Liveness", variant="primary", size="lg")
|
279 |
-
|
280 |
-
liveness_result = gr.Markdown("")
|
281 |
-
json_output = gr.JSON()
|
282 |
-
|
283 |
-
check_liveness_button.click(check_liveness, inputs=id_image_input, outputs=[liveness_result, json_output])
|
284 |
-
|
285 |
-
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Frecognito%2FID-Document-Verification&labelColor=%2337d67a&countColor=%23263759&style=flat" /></a>')
|
286 |
-
|
287 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
288 |
-
|
289 |
-
if __name__ == '__main__':
|
290 |
-
launch_demo()
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|