import gradio as gr import numpy as np import face_recognition def compare_faces(image1, image2): img1 = np.array(image1) img2 = np.array(image2) img1_encodings = face_recognition.face_encodings(img1) img2_encodings = face_recognition.face_encodings(img2) if not img1_encodings or not img2_encodings: return "Face not detected in one or both images. Please try another image." distance = np.linalg.norm(img1_encodings[0] - img2_encodings[0]) # Modify the similarity formula to make 60-70% into 90-100% similarity = 100 - (distance * 10) # Amplify the impact of small distances # Apply a non-linear transformation to boost the 60-70% range to 90-100% if 60 <= similarity < 70: similarity = 90 + (similarity - 60) * (10 / 10) # Linear map 60-70 to 90-100 return f"Similarity: {similarity:.2f}%" with gr.Blocks() as demo: gr.Markdown("### Face Similarity Checker") gr.Markdown("Upload two images to compare the faces in them.") gr.Markdown("- 70 - 80 % can be called as **identical**, even the same person with different position got this result") gr.Markdown("- 60 - 70 % can be called as **perfect couple**") gr.Markdown("This is documentation about results, I am not fixing this up to 9*% because model works well whether there is a face or not.") with gr.Row(): with gr.Column(): img1 = gr.Image(label="Upload Image 1", image_mode='RGB', type="pil") with gr.Column(): img2 = gr.Image(label="Upload Image 2", image_mode='RGB', type="pil") btn = gr.Button("Compare Faces") output = gr.Label() btn.click(compare_faces, inputs=[img1, img2], outputs=output) demo.launch()