Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# 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")
# 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 =
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 =
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)
# save output image
cv2.imwrite("cnn_face_detection.png", image)
# close all windows

This comment has been minimized.

Copy link

commented Mar 18, 2019

what does it mean by
usage: detec [-h] -i IMAGE [-w WEIGHTS]
detec error: argument -i/--image is required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.