-
Notifications
You must be signed in to change notification settings - Fork 7
/
face_detection_cnn_dlib.py
81 lines (61 loc) · 1.99 KB
/
face_detection_cnn_dlib.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# import required packages
import cv2
import dlib
import argparse
import time
# handle command line arguments
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True, help='path to image file')
ap.add_argument('-w', '--weights', default='./mmod_human_face_detector.dat',
help='path to weights file')
args = ap.parse_args()
# load input image
image = cv2.imread(args.image)
if image is None:
print("Could not read input image")
exit()
# initialize hog + svm based face detector
hog_face_detector = dlib.get_frontal_face_detector()
# initialize cnn based face detector with the weights
cnn_face_detector = dlib.cnn_face_detection_model_v1(args.weights)
start = time.time()
# apply face detection (hog)
faces_hog = hog_face_detector(image, 1)
end = time.time()
print("Execution Time (in seconds) :")
print("HOG : ", format(end - start, '.2f'))
# loop over detected faces
for face in faces_hog:
x = face.left()
y = face.top()
w = face.right() - x
h = face.bottom() - y
# draw box over face
cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)
start = time.time()
# apply face detection (cnn)
faces_cnn = cnn_face_detector(image, 1)
end = time.time()
print("CNN : ", format(end - start, '.2f'))
# loop over detected faces
for face in faces_cnn:
x = face.rect.left()
y = face.rect.top()
w = face.rect.right() - x
h = face.rect.bottom() - y
# draw box over face
cv2.rectangle(image, (x,y), (x+w,y+h), (0,0,255), 2)
# write at the top left corner of the image
# for color identification
img_height, img_width = image.shape[:2]
cv2.putText(image, "HOG", (img_width-50,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0,255,0), 2)
cv2.putText(image, "CNN", (img_width-50,40), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0,0,255), 2)
# display output image
cv2.imshow("face detection with dlib", image)
cv2.waitKey()
# save output image
cv2.imwrite("cnn_face_detection.png", image)
# close all windows
cv2.destroyAllWindows()