grapestech/face_analysis
0
1 2import gradio as gr3from deepface import DeepFace4import cv25import matplotlib.pyplot as plt6import os7 8# Function to compare two specific face images9def compare_two_faces(img1_path, img2_path):10 result = DeepFace.verify(img1_path=img1_path, img2_path=img2_path)11 custom_output = 'Verified' if result['verified'] else 'Not Verified'12 return result, custom_output13 14# Function to verify faces with multiple uploaded files15def verify_faces(img1_path, img2_paths):16 result_text = ""17 for img2_path in img2_paths:18 result = DeepFace.verify(img1_path=img1_path, img2_path=img2_path)19 result_text += f"Comparing {img1_path} with {img2_path}: {'Verified' if result['verified'] else 'Not Verified'}\n"20 if result['verified']:21 img = cv2.imread(img2_path)22 x = result['facial_areas']['img2']['x']23 y = result['facial_areas']['img2']['y']24 w = result['facial_areas']['img2']['w']25 h = result['facial_areas']['img2']['h']26 cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 1)27 img_new = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)28 plt.imshow(img_new)29 plt.axis('off')30 plt.show()31 else:32 result_text += f"No facial areas detected for {img2_path}\n"33 return result_text34 35# Function to analyze a face image36def analyze_face(image_path):37 objs = DeepFace.analyze(img_path=image_path, actions=['age', 'gender', 'race', 'emotion'])38 analysis_result = {39 'Age': objs[0]['age'],40 'Gender': objs[0]['gender'],41 'Race': objs[0]['dominant_race'],42 'Emotion': objs[0]['dominant_emotion']43 }44 return analysis_result45 46# Function to extract faces and count47def extract_faces(image_path):48 face_count = DeepFace.extract_faces(img_path=image_path, detector_backend='mtcnn')49 img = cv2.imread(image_path)50 for face in face_count:51 x = face['facial_area']['x']52 y = face['facial_area']['y']53 w = face['facial_area']['w']54 h = face['facial_area']['h']55 cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 1)56 img_new = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)57 plt.imshow(img_new)58 plt.axis('off')59 plt.show()60 return len(face_count)61 62# Gradio Interface63with gr.Blocks() as demo:64 with gr.Tabs():65 with gr.TabItem("Compare Two Faces"):66 img1_path = gr.Image(type="filepath", label="Face Image 1")67 img2_path = gr.Image(type="filepath", label="Face Image 2")68 compare_btn = gr.Button("Compare")69 compare_output = gr.Textbox(label="Comparison Result")70 compare_custom_output = gr.Textbox(label="Custom Output")71 72 compare_btn.click(compare_two_faces, inputs=[img1_path, img2_path], outputs=[compare_output, compare_custom_output])73 74 with gr.TabItem("Verify Faces with Multiple Files"):75 img1_path = gr.Image(type="filepath", label="Face Image")76 img2_paths = gr.File(label="Upload Multiple Images for Verification", file_count="multiple")77 verify_btn = gr.Button("Verify")78 verify_output = gr.Textbox(label="Verification Results")79 80 verify_btn.click(verify_faces, inputs=[img1_path, img2_paths], outputs=verify_output)81 82 with gr.TabItem("Analyze Face"):83 img_path = gr.Image(type="filepath", label="Face Image")84 analyze_btn = gr.Button("Analyze")85 analysis_output = gr.Textbox(label="Analysis Results")86 87 analyze_btn.click(analyze_face, inputs=img_path, outputs=analysis_output)88 89 with gr.TabItem("Extract and Count Faces"):90 img_path_count = gr.Image(type="filepath", label="Group Face Image")91 extract_btn = gr.Button("Extract Faces")92 count_output = gr.Textbox(label="Number of Faces Detected")93 94 extract_btn.click(extract_faces, inputs=img_path_count, outputs=count_output)95 96demo.launch()97 