Create a gist now

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()
@GooFace2

This comment has been minimized.

Show comment
Hide comment
@GooFace2

GooFace2 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. :-)

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

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

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.

Show comment
Hide comment
@narasimhatejav

narasimhatejav Oct 16, 2017

Instead of

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

narasimhatejav 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.

Show comment
Hide comment
@hammer-of-thor

hammer-of-thor Nov 2, 2017

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

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

hammer-of-thor 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.

Show comment
Hide comment
@JimReno

JimReno 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

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

This comment has been minimized.

Show comment
Hide comment
@NanzhuLin

NanzhuLin 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?

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?

@ni3-k

This comment has been minimized.

Show comment
Hide comment
@ni3-k

ni3-k Mar 7, 2018

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

ni3-k commented Mar 7, 2018

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

@ggbaird

This comment has been minimized.

Show comment
Hide comment
@ggbaird

ggbaird 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.

ggbaird 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.

Show comment
Hide comment
@Katerin-G

Katerin-G Mar 10, 2018

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

Katerin-G commented Mar 10, 2018

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

@Mushahid2521

This comment has been minimized.

Show comment
Hide comment
@Mushahid2521

Mushahid2521 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?

Mushahid2521 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.

Show comment
Hide comment
@FightForCS

FightForCS Apr 10, 2018

How to show HoG features?

FightForCS commented Apr 10, 2018

How to show HoG features?

@elhoussinetalab

This comment has been minimized.

Show comment
Hide comment
@elhoussinetalab

elhoussinetalab Jun 15, 2018

Where is the Hog representation

elhoussinetalab commented Jun 15, 2018

Where is the Hog representation

@gvkrao96

This comment has been minimized.

Show comment
Hide comment
@gvkrao96

gvkrao96 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!

gvkrao96 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!

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