Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import sys
import dlib
from skimage import io
# Take the image file name from the command line
file_name = sys.argv[1]
# Create a HOG face detector using the built-in dlib class
face_detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
# Load the image into an array
image = io.imread(file_name)
# Run the HOG face detector on the image data.
# The result will be the bounding boxes of the faces in our image.
detected_faces = face_detector(image, 1)
print("I found {} faces in the file {}".format(len(detected_faces), file_name))
# Open a window on the desktop showing the image
win.set_image(image)
# Loop through each face we found in the image
for i, face_rect in enumerate(detected_faces):
# Detected faces are returned as an object with the coordinates
# of the top, left, right and bottom edges
print("- Face #{} found at Left: {} Top: {} Right: {} Bottom: {}".format(i, face_rect.left(), face_rect.top(), face_rect.right(), face_rect.bottom()))
# Draw a box around each face we found
win.add_overlay(face_rect)
# Wait until the user hits <enter> to close the window
dlib.hit_enter_to_continue()

GooFace2 commented Oct 12, 2017

I tried to run it on Windows and python 3.6 but got syntax error.
Users\user1> python face.py
Traceback (most recent call last):
File "C:\Users\user1\Desktop\face.py", line 6, in
file_name = sys.argv[1]
IndexError: list index out of range
Appreciate if there is any help ,hints :-(

narasimhatejav commented Oct 16, 2017

Instead of

file_name = sys.argv[1] change to your image path file_name = "Your Image path"

This means that you have to run python from the commandline like so:

python step-1_find-faces.py "imagefiles"

JimReno commented Dec 12, 2017

The sys.argv[1]means you need to parse a image to the file. If you have a image named 'cat.jpg' in the working directory, try
python face.py cat.jpg

NanzhuLin commented Jan 9, 2018

Hey I got "module 'dlib' has no attribute 'image_window'" and I followed https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf to install dlib. using mac 10.12.6. Do you have any idea what I did wrong?

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