-
-
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() |
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.
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.
@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)
what is j?
since its 68 face_landmarks module then pose_landmarks.part() will have 68 point each points will have x and y cordinate values.
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?
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.datI have tried mentioning the entire path too. Can someone help regarding this?
@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.
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.datI 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).
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
On line #19, you defined file_name = sys.argv[1] again