Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import sys
import dlib
import cv2
import openface
# You can download the required pre-trained face detection model here:
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)
face_aligner = openface.AlignDlib(predictor_model)
# Take the image file name from the command line
file_name = sys.argv[1]
# Load the image
image = cv2.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))
# 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.right(), face_rect.bottom()))
# Get the the face's pose
pose_landmarks = face_pose_predictor(image, face_rect)
# Use openface to calculate and perform the face alignment
alignedFace = face_aligner.align(534, image, face_rect, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
# Save the aligned image to a file
cv2.imwrite("aligned_face_{}.jpg".format(i), alignedFace)

hi, ageitgey,
how to install openface library?

To install openface, follow the below instructions:

To download the trained model:

  • sh models/

Those installation steps don't work for me on mac. "import dlib" fails, and "import openface" fails with opencv dependency.

Hey, is there any way we can get only the eyes images after the align?
Thank you

Also, those installation steps don't work on windows. Maybe only support ubantu linux.

I think you can try to use anaconda, and then follow the steps here to install dlib, openface.(
And for those who can't install opencv library correctly, you can also use conda to install it by type the below command in your terminal:
conda install -c opencv3
I hope its helpful.

gmgoro commented Jun 29, 2017

The problem that I'm experiencing is different:

     24 # Run the HOG face detector on the image data
---> 25 detected_faces = face_detector(image, 1)
     27 print("Found {} faces in the image file {}".format(len(detected_faces), file_name))

RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

I've tried all that is suggested in ageitgey/face_recognition#21
,but couldn't fix it. Please help!
Thank you!

ck090 commented Aug 24, 2017

@karthikveermani it's better if you use Ubuntu for doing this project as in case of mac there are different, more complicated ways to install the dependencies.. I have a mac but i run ubuntu on vm

file_name = sys.argv[1] has been written two times. It only needs to be written once.

I am getting the error AttributeError: module 'openface' has no attribute 'AlignDlib'. Am I missing something here?
import openface works fine.

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