Spaces:
Sleeping
Sleeping
| from mtcnn import MTCNN | |
| detector = MTCNN() | |
| def detect_faces(image): | |
| """ | |
| Detects faces in an image using MTCNN. | |
| Args: | |
| image (numpy.ndarray): The input image. | |
| Returns: | |
| list: A list of dictionaries, where each dictionary contains | |
| 'box' (bounding box coordinates [x, y, width, height]) | |
| and 'confidence' (the confidence score). | |
| """ | |
| faces = detector.detect_faces(image) | |
| return faces | |
| if __name__ == '__main__': | |
| # Example usage | |
| import cv2 | |
| img = cv2.imread("test_image.jpg") # Replace with your image path | |
| if img is not None: | |
| detected_faces = detect_faces(img) | |
| print(f"Detected {len(detected_faces)} faces.") | |
| for face in detected_faces: | |
| print(face['box'], face['confidence']) | |
| x, y, w, h = face['box'] | |
| cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
| cv2.imshow("Detected Faces", img) | |
| cv2.waitKey(0) | |
| cv2.destroyAllWindows() | |
| else: | |
| print("Error loading image.") |