Spaces:
Build error
Build error
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from simple_facerec import SimpleFacerec | |
| # Initialize face recognition | |
| sfr = SimpleFacerec() | |
| sfr.load_encoding_images("images/") | |
| def detect_faces(image): | |
| if image is None: | |
| return "No image provided" | |
| # Convert from BGR to RGB | |
| frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| # Detect faces | |
| face_locations, face_names = sfr.detect_known_faces(frame) | |
| # Draw rectangles and names on the image | |
| for face_loc, name in zip(face_locations, face_names): | |
| y1, x2, y2, x1 = face_loc[0], face_loc[1], face_loc[2], face_loc[3] | |
| # Draw rectangle | |
| cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) | |
| # Add name | |
| cv2.putText(frame, name, (x1, y1 - 10), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0), 2) | |
| return cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=detect_faces, | |
| inputs=gr.Image(sources=["webcam"], streaming=True), # New syntax uses "sources" | |
| outputs=gr.Image(), | |
| title="Face Recognition System", | |
| description="Use webcam or upload an image to detect and recognize faces.", | |
| live=True | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch(share=True) # Ensure share is set to True |