import gradio as gr
import os
import requests
from PIL import Image
def face_compare(frame1, frame2):
url = "https://face.miniai.live/api/face_match"
files = {'image1': open(frame1, 'rb'), 'image2': open(frame2, 'rb')}
r = requests.post(url=url, files=files)
response = r.json()
detections = response.get("detections", [])
matches = response.get("match", [])
detection_rows = ""
match_rows = ""
# Process detections
for detection in detections:
face_image = detection.get("face", "")
face_img_tag = f"" if face_image else "N/A"
first_face_index = detection.get("firstFaceIndex", "N/A")
second_face_index = detection.get("secondFaceIndex", "N/A")
detection_rows += f"""
{first_face_index} |
{second_face_index} |
{face_img_tag} |
"""
# Process matches
for match in matches:
first_face_index = match.get("firstFaceIndex", "N/A")
second_face_index = match.get("secondFaceIndex", "N/A")
similarity = match.get("similarity", "N/A")
match_rows += f"""
{first_face_index} |
{second_face_index} |
{similarity:.6f} |
"""
# Create HTML tables
detections_table = f"""
Face Detection
First Face Index |
Second Face Index |
Face Image |
{detection_rows}
"""
matches_table = f"""
Matching Results
First Face Index |
Second Face Index |
Similarity |
{match_rows}
"""
return detections_table + matches_table
def check_liveness(frame):
url = "https://facelive.miniai.live/api/check_liveness"
files = {'image': open(frame, 'rb')}
r = requests.post(url=url, files=files)
html = None
table_value = ""
for key, value in r.json().items():
row_value = (""
"{key} | "
"{value} | "
"
".format(key=key, value=value))
table_value = table_value + row_value
html = (""
""
"Field | "
"Value | "
"
"
"{table_value}"
"
".format(table_value=table_value))
return html
# APP Interface
with gr.Blocks() as MiniAIdemo:
gr.Markdown(
"""
FaceRecognition-LivenessDetection SDK Demo
Experience our NIST FRVT Top Ranked FaceRecognition, iBeta 2 Certified Face Liveness Detection Engine
"""
)
with gr.Tabs():
with gr.Tab("Face Recognition"):
with gr.Row():
with gr.Column():
im_match_in1 = gr.Image(type='filepath', height=300)
gr.Examples(
[
"images/compare/demo-pic22.jpg",
"images/compare/demo-pic60.jpg",
"images/compare/demo-pic35.jpg",
"images/compare/demo-pic33.jpg",
"images/compare/demo-pic34.jpg",
],
inputs=im_match_in1
)
with gr.Column():
im_match_in2 = gr.Image(type='filepath', height=300)
gr.Examples(
[
"images/compare/demo-pic41.jpg",
"images/compare/demo-pic32.jpg",
"images/compare/demo-pic39.jpg",
"images/compare/demo-pic61.jpg",
"images/compare/demo-pic40.jpg",
],
inputs=im_match_in2
)
with gr.Column():
txt_compare_out = gr.HTML()
btn_f_match = gr.Button("Check Comparing!", variant='primary')
btn_f_match.click(face_compare, inputs=[im_match_in1, im_match_in2], outputs=txt_compare_out)
with gr.Tab("Face Liveness Detection"):
with gr.Row():
with gr.Column():
im_liveness_in = gr.Image(type='filepath', height=300)
gr.Examples(
[
"images/liveness/f_real_andr.jpg",
"images/liveness/f_fake_andr_mask3d.jpg",
"images/liveness/f_fake_andr_monitor.jpg",
"images/liveness/f_fake_andr_outline.jpg",
"images/liveness/f_fake_andr_outline3d.jpg",
"images/liveness/1.jpg",
"images/liveness/3.png",
"images/liveness/4.jpg",
],
inputs=im_liveness_in
)
btn_f_liveness = gr.Button("Check Liveness!", variant='primary')
with gr.Column():
livness_result_output = gr.HTML()
btn_f_liveness.click(check_liveness, inputs=im_liveness_in, outputs=livness_result_output)
gr.HTML('')
if __name__ == "__main__":
MiniAIdemo.launch()