Skip to content

Instantly share code, notes, and snippets.

Embed
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()
@Abd2z

This comment has been minimized.

Copy link

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 :-(

################################################
################################################

Thank you very much t for your nice comments. yes I missed the arg destination file.
It is sorted out. :-)

@narasimhatejav

This comment has been minimized.

Copy link

commented Oct 16, 2017

Instead of

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

@hammer-of-thor

This comment has been minimized.

Copy link

commented Nov 2, 2017

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

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

@JimReno

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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?

@ni3-k

This comment has been minimized.

Copy link

commented Mar 7, 2018

How can I connect this to webcam so that it can detect my face?

@ggbaird

This comment has been minimized.

Copy link

commented Mar 7, 2018

@ni3-k:
There's a video series from sentdex that goes over using a (albeit different) machine learning model to draw bounding boxes over an image using a webcam. You should be able to use all of the code from this project, while just using the openCV functions he puts in during the video to grab the image from the webcam.

I found this series really helpful when making my first real-time object recognition project; hopefully you will find it helpful too, even though it is using a different ML model.

@Katerin-G

This comment has been minimized.

Copy link

commented Mar 10, 2018

Where can I see the continuation of the article (code)? :)

@Mushahid2521

This comment has been minimized.

Copy link

commented Mar 16, 2018

RuntimeError: Unsupported image type, must be 8bit gray or RGB image

please help and if I keep the image file in the folder of the python file and pass the file_name="example.jpg" will it work?

@FightForCS

This comment has been minimized.

Copy link

commented Apr 10, 2018

How to show HoG features?

@elhoussinetalab

This comment has been minimized.

Copy link

commented Jun 15, 2018

Where is the Hog representation

@gvkrao96

This comment has been minimized.

Copy link

commented Jul 31, 2018

I am unable to install dlib . The following error is shown
CMake Error in CMakeLists.txt:
Generator

  NMake Makefiles

does not support platform specification, but platform

  x64

was specified.

CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!

@HarishAgarwal88

This comment has been minimized.

Copy link

commented Oct 7, 2018

Seems to work well with images on human face, added one cat image where it was not able to identify face. Any hint what is wrong?

@Kapish96

This comment has been minimized.

Copy link

commented Nov 20, 2018

After giving the command in anaconda promt the image window is not opening.
Can you please help me out in this?
Please reply me as soon as possible.

@Xueei

This comment has been minimized.

Copy link

commented Nov 26, 2018

@FightForCS
Do you know how to show HoG features now?
I want to see HOG image.

@Xueei

This comment has been minimized.

Copy link

commented Nov 26, 2018

@elhoussinetalab
Do you find the HOG image?
I want to know how to show the HOG image.

@3286360470

This comment has been minimized.

Copy link

commented Jan 17, 2019

Do you know how to show HOG fetures?

@3286360470

This comment has been minimized.

Copy link

commented Jan 17, 2019

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?
You can uninstall dlib absolutely, and install the dlib again.(If this can't help you, you can install the other version to try it)

@kks96500

This comment has been minimized.

Copy link

commented Mar 18, 2019

Its not working for all images ,only faces from particular images of human are being detected

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.