Skip to content

Instantly share code, notes, and snippets.

@arunponnusamy
Last active November 30, 2020 17:25
Show Gist options
  • Save arunponnusamy/7013c8617f97937250cb2c2de57c9b11 to your computer and use it in GitHub Desktop.
Save arunponnusamy/7013c8617f97937250cb2c2de57c9b11 to your computer and use it in GitHub Desktop.
# 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()
@aleeshasara
Copy link

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

@warapetch
Copy link

run this file by cnn_face_detection_dlib.py -w {mmod_human_face_detector.dat} -i {image file for detect}
like this :
python3 cnn_face_detection_dlib.py -w ../dlib_examples/mmod_human_face_detector.dat -i ../dlib_examples/faces/2008_001009.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment