-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmod_cnn_face_detection.py
46 lines (42 loc) · 1.65 KB
/
mmod_cnn_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
36
37
38
39
40
41
42
43
44
45
46
from helpers import convert_and_trim_bb
import argparse
import imutils
import time
import dlib
import cv2
#https://www.pyimagesearch.com/2021/04/19/face-detection-with-dlib-hog-and-cnn/
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str, required=True,
help="path to input image")
ap.add_argument("-m", "--model", type=str,
default="mmod_human_face_detector.dat",
help="path to dlib's CNN face detector model")
ap.add_argument("-u", "--upsample", type=int, default=1,
help="# of times to upsample")
args = vars(ap.parse_args())
# load dlib's CNN face detector
print("[INFO] loading CNN face detector...")
detector = dlib.cnn_face_detection_model_v1(args["model"])
# load the input image from disk, resize it, and convert it from
# BGR to RGB channel ordering (which is what dlib expects)
image = cv2.imread(args["image"])
image = imutils.resize(image, width=600)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# perform face detection using dlib's face detector
start = time.time()
print("[INFO[ performing face detection with dlib...")
results = detector(rgb, args["upsample"])
end = time.time()
print("[INFO] face detection took {:.4f} seconds".format(end - start))
# convert the resulting dlib rectangle objects to bounding boxes,
# then ensure the bounding boxes are all within the bounds of the
# input image
boxes = [convert_and_trim_bb(image, r.rect) for r in results]
# loop over the bounding boxes
for (x, y, w, h) in boxes:
# draw the bounding box on our image
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)