-
Notifications
You must be signed in to change notification settings - Fork 5
/
face_detection.py
35 lines (28 loc) · 1.06 KB
/
face_detection.py
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
import cv2
import dlib
import json
import argparse
## Face detection
def face_detection(img):
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
detector = dlib.get_frontal_face_detector()
faces = detector(img, 1)
return faces
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Face Detection')
parser.add_argument('--img_path', required=True, help='Path for input image')
parser.add_argument('--out', required=True, help='Path for storing bboxes of faces')
args = parser.parse_args()
# ## Debug
# args.img_path = 'imgs/multi_faces.jpg'
# args.out = 'results/multi_faces.faces.json'
# Read images
img = cv2.imread(args.img_path)
faces = face_detection(img)
bboxs = []
for face in faces:
bboxs.append((face.left(), face.top(), face.right(), face.bottom()))
with open(args.out, 'w') as f:
json.dump(bboxs, f)