import cv2 import face_recognition import numpy as np import gradio as gr # Load reference image and encode face reference_image = face_recognition.load_image_file("reference.jpg") reference_encoding = face_recognition.face_encodings(reference_image)[0] def detect_and_recognize(image_path): # Load input image image = face_recognition.load_image_file(image_path) # Detect faces face_locations = face_recognition.face_locations(image) face_encodings = face_recognition.face_encodings(image, face_locations) # Convert image for OpenCV visualization image_cv2 = cv2.imread(image_path) recognized_faces = [] for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # Compare detected face with reference matches = face_recognition.compare_faces([reference_encoding], face_encoding) label = "Match" if matches[0] else "Unknown" # Draw a rectangle around the face cv2.rectangle(image_cv2, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(image_cv2, label, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) recognized_faces.append(label) output_path = "output.jpg" cv2.imwrite(output_path, image_cv2) return output_path, recognized_faces # Gradio Interface iface = gr.Interface(fn=detect_and_recognize, inputs="file", outputs=["image", "text"], title="Face Recognition") iface.launch()