ani0226 commited on
Commit
7a3eb3f
·
verified ·
1 Parent(s): 20bc2e2

Create face_detection.py

Browse files
Files changed (1) hide show
  1. face_detection.py +35 -0
face_detection.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mtcnn import MTCNN
2
+
3
+ detector = MTCNN()
4
+
5
+ def detect_faces(image):
6
+ """
7
+ Detects faces in an image using MTCNN.
8
+
9
+ Args:
10
+ image (numpy.ndarray): The input image.
11
+
12
+ Returns:
13
+ list: A list of dictionaries, where each dictionary contains
14
+ 'box' (bounding box coordinates [x, y, width, height])
15
+ and 'confidence' (the confidence score).
16
+ """
17
+ faces = detector.detect_faces(image)
18
+ return faces
19
+
20
+ if __name__ == '__main__':
21
+ # Example usage
22
+ import cv2
23
+ img = cv2.imread("test_image.jpg") # Replace with your image path
24
+ if img is not None:
25
+ detected_faces = detect_faces(img)
26
+ print(f"Detected {len(detected_faces)} faces.")
27
+ for face in detected_faces:
28
+ print(face['box'], face['confidence'])
29
+ x, y, w, h = face['box']
30
+ cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
31
+ cv2.imshow("Detected Faces", img)
32
+ cv2.waitKey(0)
33
+ cv2.destroyAllWindows()
34
+ else:
35
+ print("Error loading image.")