Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
HTML_HEADER = """
|
| 7 |
+
<header style="text-align: center; padding: 20px; border-bottom: 2px solid #cc3300;">
|
| 8 |
+
<h1>Demo of Face Liveness Detection</h1>
|
| 9 |
+
<p style="font-size: 18px;">
|
| 10 |
+
To learn more, visit our website: <a href="https://dataspike.io/" target="_blank" style="font-size: 20px; text-decoration: none;">
|
| 11 |
+
https://dataspike.io/ </a>
|
| 12 |
+
</p>
|
| 13 |
+
</header>
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
HTML_IQA_FAILED = """
|
| 17 |
+
<h3 style="color: #cc3300;">Liveness Checks Not Performed</h3>
|
| 18 |
+
<p>
|
| 19 |
+
The liveness checks could not be conducted because the quality of the submitted image is not acceptable.
|
| 20 |
+
Please ensure that your selfie meets the following criteria for optimal quality:
|
| 21 |
+
</p>
|
| 22 |
+
<ul>
|
| 23 |
+
<li>The face should be clearly visible and not obscured.</li>
|
| 24 |
+
<li>The lighting should be adequate, avoiding extreme shadows or bright spots.</li>
|
| 25 |
+
<li>The image should be in focus, without any blurriness.</li>
|
| 26 |
+
<li>Avoid using filters or effects that alter the appearance of your face.</li>
|
| 27 |
+
</ul>
|
| 28 |
+
<p>
|
| 29 |
+
Please re-upload a clearer selfie to proceed with the liveness verification.
|
| 30 |
+
</p>
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
HTML_HEAD_POSITION_FAILED = """
|
| 34 |
+
<h4 style="color: #cc3300;">Liveness Checks Not Performed</h4>
|
| 35 |
+
<p>
|
| 36 |
+
The liveness checks could not be conducted because the position of the head in the submitted image does not meet the required criteria.
|
| 37 |
+
Please ensure that your selfie adheres to the following guidelines for optimal head positioning:
|
| 38 |
+
</p>
|
| 39 |
+
<ul>
|
| 40 |
+
<li>The entire head should be visible in the frame without any cropping.</li>
|
| 41 |
+
<li>The head should not be tilted more than 25 degrees in any direction.</li>
|
| 42 |
+
<li>The face should be facing forward, with both eyes clearly visible.</li>
|
| 43 |
+
<li>Avoid extreme angles or positions that obscure facial features.</li>
|
| 44 |
+
</ul>
|
| 45 |
+
<p>
|
| 46 |
+
Please re-upload a selfie that meets these criteria to proceed with the liveness verification.
|
| 47 |
+
</p>
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
HTML_SUCCESSFUL_PRECHECKS = """
|
| 51 |
+
<h3>Explanation of Results</h3>
|
| 52 |
+
<p>Below is an explanation of the checks we run on the uploaded selfie to determine its quality, head position, and whether the selfie is genuine or not.</p>
|
| 53 |
+
|
| 54 |
+
<ul>
|
| 55 |
+
<li><strong>Overall Status</strong>: Indicates whether all checks were successful. The selfie must meet the requirements for image quality, head position, and pass the liveness checks for the status to be <em>success</em>.</li>
|
| 56 |
+
|
| 57 |
+
<li><strong>Ran Checks</strong>: A list of checks performed on the selfie. These include prechecks for image quality (e.g., ensuring the image is not blurred, the head is fully visible and not tilted beyond acceptable angles). If these prechecks pass, liveness checks are performed to assess if the selfie is genuine.</li>
|
| 58 |
+
|
| 59 |
+
<li><strong>Face Box</strong>: The coordinates of the detected face, represented as (x1, y1, x2, y2), which define a rectangle surrounding the face within the image.</li>
|
| 60 |
+
|
| 61 |
+
<li><strong>Head Position Metrics</strong>:
|
| 62 |
+
<ul>
|
| 63 |
+
<li><strong>Pitch</strong>: The up or down tilt of the head. A positive value indicates a downward tilt, while a negative value indicates an upward tilt.</li>
|
| 64 |
+
<li><strong>Yaw</strong>: The left or right rotation of the head. Positive values indicate a turn to the right, and negative values indicate a turn to the left.</li>
|
| 65 |
+
<li><strong>Roll</strong>: The sideways tilt of the head. Positive values show a tilt to the right, and negative values show a tilt to the left.</li>
|
| 66 |
+
</ul>
|
| 67 |
+
</li>
|
| 68 |
+
|
| 69 |
+
<li><strong>Eye Aspect Ratio (EAR)</strong>: A measure of eye openness. A low EAR value indicates the eyes may be closed or blinking. This is an important metric for determining whether the person is actively looking at the camera.</li>
|
| 70 |
+
|
| 71 |
+
<li><strong>Liveness Score</strong>: A score ranging from 0 to 1 that estimates the likelihood that the selfie is of a real person. A higher score means a higher probability of authenticity.</li>
|
| 72 |
+
</ul>
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def main(file_path):
|
| 77 |
+
if file_path:
|
| 78 |
+
url = "https://api.dataspike.io/api/v4/upload/liveness-demo"
|
| 79 |
+
headers = {"ds-api-token": os.getenv("API_KEY")}
|
| 80 |
+
files = {"file": open(file_path, "rb")}
|
| 81 |
+
response = requests.post(url, headers=headers, files=files)
|
| 82 |
+
liveness_result = response.json()
|
| 83 |
+
|
| 84 |
+
if (
|
| 85 |
+
"ran_checks" not in liveness_result.keys()
|
| 86 |
+
or len(liveness_result["ran_checks"]) == 0
|
| 87 |
+
):
|
| 88 |
+
msg = ""
|
| 89 |
+
elif liveness_result["ran_checks"] == ["ImageQuality"]:
|
| 90 |
+
msg = HTML_IQA_FAILED
|
| 91 |
+
elif liveness_result["ran_checks"] == ["ImageQuality", "HeadPosition"]:
|
| 92 |
+
msg = HTML_HEAD_POSITION_FAILED
|
| 93 |
+
else:
|
| 94 |
+
msg = HTML_SUCCESSFUL_PRECHECKS
|
| 95 |
+
|
| 96 |
+
return liveness_result, msg, True
|
| 97 |
+
else:
|
| 98 |
+
liveness_result = {
|
| 99 |
+
"overallStatus": "Failure",
|
| 100 |
+
"errors": [
|
| 101 |
+
"The image is not found. Please, submit an image first and then click the button 'Check Liveness!'"
|
| 102 |
+
],
|
| 103 |
+
}
|
| 104 |
+
return liveness_result, "", False
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
with gr.Blocks() as LivenessDemo:
|
| 108 |
+
header_box = gr.HTML(HTML_HEADER)
|
| 109 |
+
with gr.Row():
|
| 110 |
+
with gr.Column(scale=1):
|
| 111 |
+
input_img_path = gr.Image(label="Input Image", type="filepath", height=300)
|
| 112 |
+
gr.Examples(
|
| 113 |
+
[
|
| 114 |
+
"images/real_client054_android_SD_scene01.jpg",
|
| 115 |
+
"images/attack_client006_android_SD_ipad_video_scene01.jpg",
|
| 116 |
+
"images/attack_client055_android_SD_iphone_video_scene01.jpg",
|
| 117 |
+
"images/attack_client026_android_SD_printed_photo_scene01.jpg",
|
| 118 |
+
"images/attack_deepfake_1.jpeg",
|
| 119 |
+
"images/attack_deepfake_2.png",
|
| 120 |
+
"images/attack_deepfake_3.jpeg",
|
| 121 |
+
],
|
| 122 |
+
inputs=input_img_path,
|
| 123 |
+
)
|
| 124 |
+
check_button = gr.Button("Check Liveness!", variant="primary")
|
| 125 |
+
with gr.Column():
|
| 126 |
+
liveness_result = gr.JSON(label="Liveness Result")
|
| 127 |
+
explanation_box = gr.HTML("", visible=False)
|
| 128 |
+
|
| 129 |
+
state = gr.State(False)
|
| 130 |
+
check_button.click(
|
| 131 |
+
main,
|
| 132 |
+
inputs=input_img_path,
|
| 133 |
+
outputs=[liveness_result, explanation_box, state],
|
| 134 |
+
)
|
| 135 |
+
state.change(
|
| 136 |
+
lambda show: gr.update(visible=show), inputs=state, outputs=explanation_box
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
LivenessDemo.launch()
|