Skip to content

Instantly share code, notes, and snippets.

@ageitgey
Created July 23, 2016 16:57
Show Gist options
  • Save ageitgey/1c1cb1c60ace321868f7410d48c228e1 to your computer and use it in GitHub Desktop.
Save ageitgey/1c1cb1c60ace321868f7410d48c228e1 to your computer and use it in GitHub Desktop.
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
Copy link

Abd2z 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
Copy link

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
Copy link

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

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

@JimReno
Copy link

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
Copy link

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
Copy link

ni3-k commented Mar 7, 2018

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

@ggbaird
Copy link

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
Copy link

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

@Mushahid2521
Copy link

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
Copy link

How to show HoG features?

@ELHoussineT
Copy link

Where is the Hog representation

@venkrishg
Copy link

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
Copy link

HarishAgarwal88 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
Copy link

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
Copy link

Xueei commented Nov 26, 2018

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

@Xueei
Copy link

Xueei commented Nov 26, 2018

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

@3286360470
Copy link

Do you know how to show HOG fetures?

@3286360470
Copy link

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)

@singh-bhagirath
Copy link

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

@rohansh-tty
Copy link

rohansh-tty commented Jan 6, 2020

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

If you're using OpenCV use this function

import cv2

v1 = cv2.VideoCapture(0) # 0 -- indicates your webcam
print(v1.isOpened()) # Returns bool value based on the video capture object

@Vickey-ZWQ
Copy link

Vickey-ZWQ commented May 18, 2020

I have successfully run this script and it return an image with a box in face. But I just want a HOG image just like that:

hog

How can I get it?

@gratefullee
Copy link

from skimage.feature import hog
from skimage import exposure
import cv2


image = cv2.imread('/your/image/path')
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
                    cells_per_block=(1, 1), visualize=True, multichannel=True)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')

# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))

ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.show()

hog

@Dankrou
Copy link

Dankrou commented May 4, 2021

from skimage.feature import hog
from skimage import exposure
import cv2


image = cv2.imread('/your/image/path')
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
                    cells_per_block=(1, 1), visualize=True, multichannel=True)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')

# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))

ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.show()

hog

Hi, you forgot to add: "from matplotlib import pyplot as plt".

@gratefullee
Copy link

@Dankrou yeah it was on a different kernel but thanks for pointing that out.

@DaruinHerrera
Copy link

import os
import sys
import dlib
from skimage import io, exposure
from skimage.feature import hog
import cv2
import matplotlib.pyplot as plt

main = os.path.abspath(sys.argv[0])
file_name = os.path.abspath(os.path.join(os.path.dirname(main), "examples","faces","2.jpg"))

Contruye histograma de un rostros para identificación

image = cv2.imread(file_name)
fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(1, 1), visualize=True, channel_axis=2)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')

Rescale histogram for better display

hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 20))

ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.show()

@ELHoussineT
Copy link

ELHoussineT commented May 26, 2023 via email

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