Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@Crypt-iQ
Copy link

Crypt-iQ commented Jun 29, 2017

On line #19, you defined file_name = sys.argv[1] again

@arasharchor
Copy link

arasharchor commented Jul 25, 2017

how to save the image with landmark? I wanted to try draw_rectangle() and save_png(), but it seems they are not supported in dlib python api.

@ck090
Copy link

ck090 commented Aug 24, 2017

On line #19 it's better if you directly specify the path to the image rather than taking the argument from the python command line.

@gaarons
Copy link

gaarons commented Sep 13, 2017

I second the comment of @smajida
Additionally, the dlib.image_window() seems to be incompatible on my virtualenv; any advice on this hang would be appreciated. I'm looking to face landmark and alter the images then save them as @smajida also seems to be suggesting. Thanks

@wangyongjie-ntu
Copy link

wangyongjie-ntu commented Sep 23, 2017

@smajida U can save image like this:
image[pose_landmarks.part(j).x - 2 : pose_landmarks.part(j).x + 2, pose_landmarks.part(j).y - 2: pose_landmarks.part(j).y + 1] = 255
io.imsave('test.jpg', image)

@tauseef83
Copy link

tauseef83 commented Sep 24, 2017

what is j?

@goodrahstar
Copy link

goodrahstar commented Oct 7, 2018

@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

shreyashi2104 commented Feb 6, 2019

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

kevindree commented Feb 20, 2019

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

RubinXnibu commented Apr 3, 2019

@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

subhrajitb commented Oct 2, 2019

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

HongPhuc89 commented Apr 23, 2021

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