import gradio as gr import os import requests from PIL import Image import json # Configuration API_BASE_URL = os.getenv("API_BASE_URL") def face_compare(frame1, frame2, request: gr.Request = None): """Face comparison with enhanced result display""" try: url = f"{API_BASE_URL}/api/face_match" # Prepare files files = {} if frame1: files['image1'] = open(frame1, 'rb') if frame2: files['image2'] = open(frame2, 'rb') if not files: return "
" # Make API request response = requests.post(url=url, files=files) result = response.json() # Close files for file in files.values(): file.close() # Enhanced result processing return format_face_comparison_result(result, frame1, frame2) except Exception as e: return f"" def format_face_comparison_result(result, img1_path, img2_path): """Format face comparison results with professional styling""" detections = result.get("detections", []) matches = result.get("match", []) # Create result HTML html = "| First Image Face | Second Image Face | Similarity Score | Result |
|---|---|---|---|
|
{first_face_display}
Face {first_face_index}
|
{second_face_display}
Face {second_face_index}
|
{similarity:.4f} | {result_text} |
Experience our NIST FRVT Top Ranked 1:1 & 1:N Face Matching Technology
GitHub
""")
gr.HTML("""
HuggingFace
""")
gr.HTML("""
Live Demo
""")
gr.HTML("""
Documentation
""")
gr.HTML("""
YouTube
""")
gr.HTML("""
Google Play
""")
# Main Content - Upload and Results
with gr.Row(elem_classes="main-content-row"):
# Upload Section
with gr.Column(scale=0.6, elem_classes="upload-section"):
gr.Markdown("### Upload Images for Comparison", elem_classes="section-title")
with gr.Row(elem_classes="upload-images-row"):
# First Image
with gr.Column(scale=1, elem_classes="upload-image-col"):
im_match_in1 = gr.Image(
type='filepath',
height=350,
label="First Image",
show_download_button=False
)
# Second Image
with gr.Column(scale=1, elem_classes="upload-image-col"):
im_match_in2 = gr.Image(
type='filepath',
height=350,
label="Second Image",
show_download_button=False
)
# Examples and Button
with gr.Accordion("Try Example Images", open=True):
with gr.Row():
gr.Examples(
examples=[
"assets/1.jpg",
"assets/2.jpg",
"assets/3.jpg",
"assets/4.jpg",
],
inputs=im_match_in1,
label="First Image Examples"
)
gr.Examples(
examples=[
"assets/1-1.jpg",
"assets/2-1.jpg",
"assets/3-1.jpg",
"assets/4-1.jpg",
],
inputs=im_match_in2,
label="Second Image Examples"
)
btn_f_match = gr.Button(
"Compare Faces 🚀",
variant='primary',
elem_classes="button-primary"
)
# Results Section
with gr.Column(scale=0.4, elem_classes="result-section"):
gr.Markdown("### Comparison Results", elem_classes="section-title")
txt_compare_out = gr.HTML(
value="