import sys sys.path.append('../') import os import gradio as gr import cv2 import time import numpy as np from PIL import Image from engine.header import * file_path = os.path.abspath(__file__) gradio_path = os.path.dirname(file_path) root_path = os.path.dirname(gradio_path) version = get_version().decode('utf-8') print_info('\t \t version {}'.format(version)) device_id = get_deviceid().decode('utf-8') print_info('\t \t\t {}'.format(device_id)) g_activation_result = -1 MATCH_THRESHOLD = 0.67 css = """ .example-image img{ display: flex; /* Use flexbox to align items */ justify-content: center; /* Center the image horizontally */ align-items: center; /* Center the image vertically */ height: 300px; /* Set the height of the container */ object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */ } .example-image{ display: flex; /* Use flexbox to align items */ justify-content: center; /* Center the image horizontally */ align-items: center; /* Center the image vertically */ height: 350px; /* Set the height of the container */ object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */ } .face-row { display: flex; justify-content: space-around; /* Distribute space evenly between elements */ align-items: center; /* Align items vertically */ width: 100%; /* Set the width of the row to 100% */ } .face-image{ justify-content: center; /* Center the image horizontally */ align-items: center; /* Center the image vertically */ height: 160px; /* Set the height of the container */ width: 160px; object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */ } .face-image img{ justify-content: center; /* Center the image horizontally */ align-items: center; /* Center the image vertically */ height: 160px; /* Set the height of the container */ object-fit: contain; /* Preserve aspect ratio while fitting the image within the container */ } .markdown-success-container { background-color: #F6FFED; padding: 20px; margin: 20px; border-radius: 1px; border: 2px solid green; text-align: center; } .markdown-fail-container { background-color: #FFF1F0; padding: 20px; margin: 20px; border-radius: 1px; border: 2px solid red; text-align: center; } .block-background { # background-color: #202020; /* Set your desired background color */ border-radius: 5px; } """ def activate_sdk(): online_key = os.environ.get("FR_LICENSE_KEY") offline_key_path = os.path.join(root_path, "license.txt") dict_path = os.path.join(root_path, "engine/bin") ret = -1 if online_key is None: print_warning("Recognition online license key not found!") else: print_info(f"FR_LICENSE_KEY: {online_key}") ret = init_sdk(dict_path.encode('utf-8'), online_key.encode('utf-8')) if ret == 0: print_log("Successfully online init SDK!") else: print_error(f"Failed to online init SDK, Error code {ret}\n Trying offline init SDK..."); if os.path.exists(offline_key_path) is False: print_warning("Recognition offline license key file not found!") print_error(f"Falied to offline init SDK, Error code {ret}") return ret else: ret = init_sdk_offline(dict_path.encode('utf-8'), offline_key_path.encode('utf-8')) if ret == 0: print_log("Successfully offline init SDK!") else: print_error(f"Falied to offline init SDK, Error code {ret}") return ret return ret def compare_face_clicked(frame1, frame2, threshold): global g_activation_result if g_activation_result != 0: gr.Warning("SDK Activation Failed!") return None, None, None, None, None, None, None, None, None try: image1 = open(frame1, 'rb') image2 = open(frame2, 'rb') except: raise gr.Error("Please select images files!") image_mat1 = cv2.imdecode(np.frombuffer(image1.read(), np.uint8), cv2.IMREAD_COLOR) image_mat2 = cv2.imdecode(np.frombuffer(image2.read(), np.uint8), cv2.IMREAD_COLOR) start_time = time.time() result, score, face_bboxes, face_features = compare_face(image_mat1, image_mat2, float(threshold)) end_time = time.time() process_time = (end_time - start_time) * 1000 try: image1 = Image.open(frame1) image2 = Image.open(frame2) images = [image1, image2] face1 = Image.new('RGBA',(150, 150), (80,80,80,0)) face2 = Image.new('RGBA',(150, 150), (80,80,80,0)) faces = [face1, face2] face_bboxes_result = [] if face_bboxes is not None: for i, bbox in enumerate(face_bboxes): x1 = bbox[0] y1 = bbox[1] x2 = bbox[2] y2 = bbox[3] if x1 < 0: x1 = 0 if y1 < 0: y1 = 0 if x2 >= images[i].width: x2 = images[i].width - 1 if y2 >= images[i].height: y2 = images[i].height - 1 face_bbox_str = f"x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}" face_bboxes_result.append(face_bbox_str) faces[i] = images[i].crop((x1, y1, x2, y2)) face_image_ratio = faces[i].width / float(faces[i].height) resized_w = int(face_image_ratio * 150) resized_h = 150 faces[i] = faces[i].resize((int(resized_w), int(resized_h))) except: pass matching_result = Image.open(os.path.join(gradio_path, "icons/blank.png")) similarity_score = "" if faces[0] is not None and faces[1] is not None: if score is not None: str_score = str("{:.4f}".format(score)) if result == "SAME PERSON": matching_result = Image.open(os.path.join(gradio_path, "icons/same.png")) similarity_score = f"""

Similarity score: {str_score}

""" else: matching_result = Image.open(os.path.join(gradio_path, "icons/different.png")) similarity_score = f"""

Similarity score: {str_score}

""" return faces[0], faces[1], matching_result, similarity_score, face_bboxes_result[0], face_bboxes_result[1], face_features[0], face_features[1], str(process_time) def launch_demo(activate_result): with gr.Blocks(css=css) as demo: gr.Markdown( f"""

Face Recognition {version}

🤝 Contact us for our on-premise Face Recognition, Liveness Detection SDKs deployment

  www.recognito.vision     www.recognito.vision     www.recognito.vision     www.recognito.vision

                      

""" ) with gr.Group(): if activate_result == 0: gr.Markdown("""

 Activation Success!

""") else: gr.Markdown("""

 Activation Failed!

""") gr.Textbox(device_id, label="Hardware ID") with gr.Row(): with gr.Column(scale=2): with gr.Row(): with gr.Column(scale=1): compare_face_input1 = gr.Image(label="Image1", type='filepath', elem_classes="example-image") gr.Examples([os.path.join(root_path,'examples/1.jpg'), os.path.join(root_path,'examples/2.jpg'), os.path.join(root_path,'examples/3.jpg'), os.path.join(root_path,'examples/4.jpg')], inputs=compare_face_input1) with gr.Column(scale=1): compare_face_input2 = gr.Image(label="Image2", type='filepath', elem_classes="example-image") gr.Examples([os.path.join(root_path,'examples/5.jpg'), os.path.join(root_path,'examples/6.jpg'), os.path.join(root_path,'examples/7.jpg'), os.path.join(root_path,'examples/8.jpg')], inputs=compare_face_input2) with gr.Blocks(): with gr.Column(scale=1, min_width=400, elem_classes="block-background"): txt_threshold = gr.Textbox(f"{MATCH_THRESHOLD}", label="Matching Threshold", interactive=True) compare_face_button = gr.Button("Compare Face", variant="primary", size="lg") with gr.Row(elem_classes="face-row"): face_output1 = gr.Image(value=os.path.join(gradio_path,'icons/face.jpg'), label="Face 1", scale=0, elem_classes="face-image") compare_result = gr.Image(value=os.path.join(gradio_path,'icons/blank.png'), min_width=30, scale=0, show_download_button=False, show_label=False) face_output2 = gr.Image(value=os.path.join(gradio_path,'icons/face.jpg'), label="Face 2", scale=0, elem_classes="face-image") similarity_markdown = gr.Markdown("") txt_speed = gr.Textbox(f"", label="Processing Time (ms)", interactive=False, visible=False) with gr.Group(): gr.Markdown(""" face1""") txt_bbox1 = gr.Textbox(f"", label="Rect", interactive=False) txt_feature1 = gr.Textbox(f"", label="Feature", interactive=False, max_lines=5) with gr.Group(): gr.Markdown(""" face2""") txt_bbox2 = gr.Textbox(f"", label="Rect", interactive=False) txt_feature2 = gr.Textbox(f"", label="Feature", interactive=False, max_lines=5) compare_face_button.click(compare_face_clicked, inputs=[compare_face_input1, compare_face_input2, txt_threshold], outputs=[face_output1, face_output2, compare_result, similarity_markdown, txt_bbox1, txt_bbox2, txt_feature1, txt_feature2, txt_speed]) demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False) if __name__ == '__main__': g_activation_result = activate_sdk() launch_demo(g_activation_result)