File size: 1,048 Bytes
7a3eb3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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.")