Skip to content

Instantly share code, notes, and snippets.

@ageitgey
Created July 23, 2016 17:26
Show Gist options
  • Star 97 You must be signed in to star a gist
  • Fork 50 You must be signed in to fork a gist
  • Save ageitgey/ae340db3e493530d5e1f9c15292e5c74 to your computer and use it in GitHub Desktop.
Save ageitgey/ae340db3e493530d5e1f9c15292e5c74 to your computer and use it in GitHub Desktop.
import sys
import dlib
from skimage import io
# You can download the required pre-trained face detection model here:
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
predictor_model = "shape_predictor_68_face_landmarks.dat"
# 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()
face_pose_predictor = dlib.shape_predictor(predictor_model)
win = dlib.image_window()
# Take the image file name from the command line
file_name = sys.argv[1]
# Load the image
image = io.imread(file_name)
# Run the HOG face detector on the image data
detected_faces = face_detector(image, 1)
print("Found {} faces in the image file {}".format(len(detected_faces), file_name))
# Show the desktop window with 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)
# Get the the face's pose
pose_landmarks = face_pose_predictor(image, face_rect)
# Draw the face landmarks on the screen.
win.add_overlay(pose_landmarks)
dlib.hit_enter_to_continue()
@goodrahstar
Copy link

@tauseef83

since its 68 face_landmarks module then pose_landmarks.part() will have 68 point each points will have x and y cordinate values.

@shreyashi2104
Copy link

I am facing a problem in using/reading the pretrained prediction model as loaded in line 7. I am running this code on VSCode. The error being displayed is:

face_pose_predictor = dlib.shape_predictor(predictor_model)
RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat

I have tried mentioning the entire path too. Can someone help regarding this?

@kevindree
Copy link

I am facing the same issue , anybody can help ?

I am facing a problem in using/reading the pretrained prediction model as loaded in line 7. I am running this code on VSCode. The error being displayed is:

face_pose_predictor = dlib.shape_predictor(predictor_model)
RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat

I have tried mentioning the entire path too. Can someone help regarding this?

@RubinXnibu
Copy link

@kevindree: @shreyashi2104: You guys are missing the landmarks model file. You need to download the file from here:
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
and you need to decompress it with bunzip2 (linux, mac) or 7-Zip (Windows) and put it in the same directory as this python script, or you need to update line # 7 of step-2a_finding-face-landmarks.py to point to where you saved the decompressed file.

@subhrajitb
Copy link

I am facing a problem in using/reading the pretrained prediction model as loaded in line 7. I am running this code on VSCode. The error being displayed is:

face_pose_predictor = dlib.shape_predictor(predictor_model)
RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat

I have tried mentioning the entire path too. Can someone help regarding this?

Try with jpeg images. The code does not work with png (and possibly with other non-jpeg formats).

@HongPhuc89
Copy link

I am facing the same issue , anybody can help ?

I am facing a problem in using/reading the pretrained prediction model as loaded in line 7. I am running this code on VSCode. The error being displayed is:
face_pose_predictor = dlib.shape_predictor(predictor_model)
RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
I have tried mentioning the entire path too. Can someone help regarding this?

Please download dat file at: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

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