Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import sys
|
2 |
import os
|
|
|
3 |
import gradio as gr
|
4 |
import json
|
5 |
import requests
|
@@ -13,21 +14,43 @@ css = """
|
|
13 |
height: 300px; /* Set the height of the container */
|
14 |
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
15 |
}
|
16 |
-
.example-image{
|
17 |
display: flex; /* Use flexbox to align items */
|
18 |
text-align: center;
|
19 |
justify-content: center; /* Center the image horizontally */
|
20 |
align-items: center; /* Center the image vertically */
|
21 |
height: 350px; /* Set the height of the container */
|
22 |
object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
.block-background {
|
25 |
# background-color: #202020; /* Set your desired background color */
|
26 |
border-radius: 5px;
|
27 |
}
|
28 |
-
}
|
29 |
"""
|
30 |
|
|
|
|
|
|
|
|
|
31 |
def find_key_in_dict(d, target_key):
|
32 |
for key, value in d.items():
|
33 |
if key == target_key:
|
@@ -57,6 +80,53 @@ def json_to_html_table(data, image_keys):
|
|
57 |
html += "</table>"
|
58 |
return html
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def idcard_recognition(frame1, frame2):
|
61 |
url = "https://recognito-iddocumentrecognition.p.rapidapi.com/api/read_idcard"
|
62 |
|
@@ -68,8 +138,9 @@ def idcard_recognition(frame1, frame2):
|
|
68 |
elif frame1 is None and frame2 is not None:
|
69 |
files = {'image': open(frame2, 'rb')}
|
70 |
else:
|
71 |
-
return ['', None]
|
72 |
-
|
|
|
73 |
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
74 |
|
75 |
r = requests.post(url=url, files=files, headers=headers)
|
@@ -123,7 +194,7 @@ def idcard_recognition(frame1, frame2):
|
|
123 |
|
124 |
result = json_to_html_table(result_table_dict, {'portrait'})
|
125 |
json_result = json.dumps(rawValues, indent=6)
|
126 |
-
return [result, json_result, images]
|
127 |
|
128 |
def launch_demo():
|
129 |
with gr.Blocks(css=css) as demo:
|
@@ -182,6 +253,7 @@ def launch_demo():
|
|
182 |
with gr.Blocks():
|
183 |
with gr.Column(scale=4, min_width=400, elem_classes="block-background"):
|
184 |
id_recognition_button = gr.Button("ID Card Recognition", variant="primary", size="lg")
|
|
|
185 |
with gr.Tab("Key Fields"):
|
186 |
id_result_output = gr.HTML()
|
187 |
with gr.Tab("Raw JSON"):
|
@@ -190,11 +262,11 @@ def launch_demo():
|
|
190 |
image_result_output = gr.HTML()
|
191 |
|
192 |
|
193 |
-
id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, json_result_output, image_result_output])
|
194 |
|
195 |
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>')
|
196 |
|
197 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
198 |
|
199 |
if __name__ == '__main__':
|
200 |
-
launch_demo()
|
|
|
1 |
import sys
|
2 |
import os
|
3 |
+
import io
|
4 |
import gradio as gr
|
5 |
import json
|
6 |
import requests
|
|
|
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: 10px;
|
38 |
+
margin: 10px;
|
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:
|
|
|
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! <br/> Select Image File</p></div>"""
|
86 |
+
return liveness_result
|
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! <br/> Failed to Open File</p></div>"""
|
101 |
+
return liveness_result
|
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
|
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 |
+
# Update json_result with the modified process_results
|
122 |
+
return liveness_result
|
123 |
+
|
124 |
+
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed! <br/>Document not found!</p></div>"""
|
125 |
+
return liveness_result
|
126 |
+
else:
|
127 |
+
liveness_result = f"""<div class="markdown-fail-container"><p style="text-align: center; font-size: 20px; color: red;">Liveness Check Failed! <br/>{result.text}</p></div>"""
|
128 |
+
return liveness_result
|
129 |
+
|
130 |
def idcard_recognition(frame1, frame2):
|
131 |
url = "https://recognito-iddocumentrecognition.p.rapidapi.com/api/read_idcard"
|
132 |
|
|
|
138 |
elif frame1 is None and frame2 is not None:
|
139 |
files = {'image': open(frame2, 'rb')}
|
140 |
else:
|
141 |
+
return ['', None, None, None]
|
142 |
+
|
143 |
+
liveness_result = check_liveness(frame1)
|
144 |
headers = {"X-RapidAPI-Key": os.environ.get("API_KEY")}
|
145 |
|
146 |
r = requests.post(url=url, files=files, headers=headers)
|
|
|
194 |
|
195 |
result = json_to_html_table(result_table_dict, {'portrait'})
|
196 |
json_result = json.dumps(rawValues, indent=6)
|
197 |
+
return [result, json_result, images, liveness_result]
|
198 |
|
199 |
def launch_demo():
|
200 |
with gr.Blocks(css=css) as demo:
|
|
|
253 |
with gr.Blocks():
|
254 |
with gr.Column(scale=4, min_width=400, elem_classes="block-background"):
|
255 |
id_recognition_button = gr.Button("ID Card Recognition", variant="primary", size="lg")
|
256 |
+
liveness_result = gr.Markdown("")
|
257 |
with gr.Tab("Key Fields"):
|
258 |
id_result_output = gr.HTML()
|
259 |
with gr.Tab("Raw JSON"):
|
|
|
262 |
image_result_output = gr.HTML()
|
263 |
|
264 |
|
265 |
+
id_recognition_button.click(idcard_recognition, inputs=[id_image_input1, id_image_input2], outputs=[id_result_output, json_result_output, image_result_output, liveness_result])
|
266 |
|
267 |
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>')
|
268 |
|
269 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
270 |
|
271 |
if __name__ == '__main__':
|
272 |
+
launch_demo()
|