Instantly share code, notes, and snippets.

Embed
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:
# 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)
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.top(), 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)
@zimenglan-sysu-512

This comment has been minimized.

Show comment
Hide comment
@zimenglan-sysu-512

zimenglan-sysu-512 Jul 26, 2016

hi, ageitgey,
how to install openface library?

zimenglan-sysu-512 commented Jul 26, 2016

hi, ageitgey,
how to install openface library?

@nitish11

This comment has been minimized.

Show comment
Hide comment
@nitish11

nitish11 Jul 27, 2016

To install openface, follow the below instructions:

To download the trained model:

  • sh models/get-models.sh

nitish11 commented Jul 27, 2016

To install openface, follow the below instructions:

To download the trained model:

  • sh models/get-models.sh
@karthikveeramani

This comment has been minimized.

Show comment
Hide comment
@karthikveeramani

karthikveeramani Nov 12, 2016

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

karthikveeramani commented Nov 12, 2016

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

@Tiaguituh05

This comment has been minimized.

Show comment
Hide comment
@Tiaguituh05

Tiaguituh05 Jan 6, 2017

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

Tiaguituh05 commented Jan 6, 2017

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

@lttzzlll

This comment has been minimized.

Show comment
Hide comment
@lttzzlll

lttzzlll May 21, 2017

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

lttzzlll commented May 21, 2017

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

@zhuangjiubzj

This comment has been minimized.

Show comment
Hide comment
@zhuangjiubzj

zhuangjiubzj Jun 3, 2017

I think you can try to use anaconda, and then follow the steps here to install dlib, openface.(https://github.com/samotiian/Installing_openface_with_anaconda)
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 https://conda.binstar.org/menpo opencv3
I hope its helpful.

zhuangjiubzj commented Jun 3, 2017

I think you can try to use anaconda, and then follow the steps here to install dlib, openface.(https://github.com/samotiian/Installing_openface_with_anaconda)
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 https://conda.binstar.org/menpo opencv3
I hope its helpful.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost 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)
     26 
     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!

ghost 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)
     26 
     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

This comment has been minimized.

Show comment
Hide comment
@ck090

ck090 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

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

@kartikchauhan

This comment has been minimized.

Show comment
Hide comment
@kartikchauhan

kartikchauhan Dec 6, 2017

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

kartikchauhan commented Dec 6, 2017

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

@nishantkumar1292

This comment has been minimized.

Show comment
Hide comment
@nishantkumar1292

nishantkumar1292 Dec 15, 2017

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

nishantkumar1292 commented Dec 15, 2017

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

@Branham94

This comment has been minimized.

Show comment
Hide comment
@Branham94

Branham94 Jan 29, 2018

Hello! does someone know how to install openface on windows ?

Branham94 commented Jan 29, 2018

Hello! does someone know how to install openface on windows ?

@Branham94

This comment has been minimized.

Show comment
Hide comment
@Branham94

Branham94 Jan 29, 2018

I've solved the issue

Branham94 commented Jan 29, 2018

I've solved the issue

@Lucas31500

This comment has been minimized.

Show comment
Hide comment
@Lucas31500

Lucas31500 Feb 5, 2018

i have the same error as @nishantkumar1292 : AttributeError: module 'openface' has no attribute 'AlignDlib'
All the steps work fine but when i run the script i got this error.
I am on ubuntu

Lucas31500 commented Feb 5, 2018

i have the same error as @nishantkumar1292 : AttributeError: module 'openface' has no attribute 'AlignDlib'
All the steps work fine but when i run the script i got this error.
I am on ubuntu

@shimimachluf

This comment has been minimized.

Show comment
Hide comment
@shimimachluf

shimimachluf Mar 15, 2018

I also had the "AttributeError: module 'openface' has no attribute 'AlignDlib'" error.
I installed python 3.4 and made sure all the modules/packages needed for the openface are versions in the required range (requirements.txt), and now it works!

shimimachluf commented Mar 15, 2018

I also had the "AttributeError: module 'openface' has no attribute 'AlignDlib'" error.
I installed python 3.4 and made sure all the modules/packages needed for the openface are versions in the required range (requirements.txt), and now it works!

@asthanakarishma

This comment has been minimized.

Show comment
Hide comment
@asthanakarishma

asthanakarishma Apr 4, 2018

I keep getting the following error:

Traceback (most recent call last): File "detect_face_alignment.py", line 15, in <module> detected = frontal_face_detector(img, 1) RuntimeError: Expected writable numpy.ndarray with shape set.

My code matches what is written here. Any ideas as to what I could be doing wrong?

asthanakarishma commented Apr 4, 2018

I keep getting the following error:

Traceback (most recent call last): File "detect_face_alignment.py", line 15, in <module> detected = frontal_face_detector(img, 1) RuntimeError: Expected writable numpy.ndarray with shape set.

My code matches what is written here. Any ideas as to what I could be doing wrong?

@AbhiAgarwal192

This comment has been minimized.

Show comment
Hide comment
@AbhiAgarwal192

AbhiAgarwal192 Aug 15, 2018

Hi @asthanakarishma, looks like you are also doing something else. Can you share your complete code here.

AbhiAgarwal192 commented Aug 15, 2018

Hi @asthanakarishma, looks like you are also doing something else. Can you share your complete code here.

@alex-kravets

This comment has been minimized.

Show comment
Hide comment
@alex-kravets

alex-kravets Sep 7, 2018

Hi,
I have a small problem and I have no idea how to solve it in a right way (actually I have, but not sure that it is correct).
Let's suppose that we need to get landmarks from the aligned image. To do that we need:

  1. find a face on the image using face_detector
  2. align image using face_aligner
  3. find face on aligned image using face_detector again
  4. obtain the landmarks with usage face_pose_predictor

And here the problem: face_detector returns empty dlib.rectangles object on aligned image. So, I am not able to use face_pose_predictor because it requires face rectangle which is absent.

My idea is that I can omit step #3 and use face_pose_predictor on aligned image without searching for the face on it. And the bounding box for landmarks detection specify as [[0, 0], [aligned_height, aligned_width]]. But I'm not confident that this is a correct way to deal with the problem.

alex-kravets commented Sep 7, 2018

Hi,
I have a small problem and I have no idea how to solve it in a right way (actually I have, but not sure that it is correct).
Let's suppose that we need to get landmarks from the aligned image. To do that we need:

  1. find a face on the image using face_detector
  2. align image using face_aligner
  3. find face on aligned image using face_detector again
  4. obtain the landmarks with usage face_pose_predictor

And here the problem: face_detector returns empty dlib.rectangles object on aligned image. So, I am not able to use face_pose_predictor because it requires face rectangle which is absent.

My idea is that I can omit step #3 and use face_pose_predictor on aligned image without searching for the face on it. And the bounding box for landmarks detection specify as [[0, 0], [aligned_height, aligned_width]]. But I'm not confident that this is a correct way to deal with the problem.

@York-169

This comment has been minimized.

Show comment
Hide comment
@York-169

York-169 Sep 27, 2018

There's something I don't understand:
In your article, it says "here’s the code for transforming the image using those landmarks.“.
but from my understanding of the source code. the transforming doesn't use any landmarks at all?
can you explain how?

York-169 commented Sep 27, 2018

There's something I don't understand:
In your article, it says "here’s the code for transforming the image using those landmarks.“.
but from my understanding of the source code. the transforming doesn't use any landmarks at all?
can you explain how?

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