Using the Facemark API Python [closed]

asked 2019-01-02 08:27:59 -0500

alfonsofernandezvillar gravatar image


So far I have been using dlib landmarks detector (both the 5 face landmarks detector and the 68 face landmarks detector). Now, I would like to use Facemark API, which I think it has been introduced in OpenCV since 3.4.0 version, but I am not able to get this sample running:

# load image: 
image = cv2.imread("my_image.png", 0)

# find faces:
cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
faces = cascade.detectMultiScale(image, 1.3, 5)

# create landmark detector and load lbf model:
facemark = cv2.face.createFacemarkLBF()

# run landmark detector:
ok, landmarks =, faces)

# print results:
print ("landmarks LBF",ok, landmarks)

The fit function seems to take a lot of execution time and after a while, an error code is obtained.

Am I doing something wrong?

Thanks in advanced

PS: From this post: "Python support: It appears that there is still no python support yet as of OpenCV 3.4":

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-11-18 04:55:28.253400


yea, it's crashing inside the fit() function, right ?

have a look here, please (situation is still unchanged, sad as it is)

berak gravatar imageberak ( 2019-01-02 08:33:48 -0500 )edit

Yes, It's crashing inside the fit() function.

I have seen the link you have provided. I can read de following: "Closing the issue, as I believe it has been resolved. Please let me know if it has to remain open, till there is a sweeter solution!" but I do not see the solution.

I have seen this:

But I do not understand why this path is added: sys.path.append('/home/travis/build/berak/tt/ocv/lib/python2.7/dist-packages/')

alfonsofernandezvillar gravatar imagealfonsofernandezvillar ( 2019-01-02 09:10:01 -0500 )edit

the travis build bot had a weird python setup (it put the cv2 into dist-packages, and then it could not find it), imho you should ignore it.

IF you'd want to patch the cv2 code you'd have to try with this (and rebuild)

berak gravatar imageberak ( 2019-01-02 09:36:57 -0500 )edit

I have installed opencv using pip (Unofficial pre-built OpenCV packages for Python: opencv-python

So as you suggest I have to install OpenCV from source code, modify the function FacemarkLBFImpl::fit and rebuild, right?

alfonsofernandezvillar gravatar imagealfonsofernandezvillar ( 2019-01-02 10:24:41 -0500 )edit

yes, indeed. sad as it is, you'll have to mess with building from src for this ;(

(again, your python code above is all fine, the problem is with the c++ / wrapping code)

berak gravatar imageberak ( 2019-01-02 10:26:08 -0500 )edit

Ok, thank you @berak.

alfonsofernandezvillar gravatar imagealfonsofernandezvillar ( 2019-01-02 10:43:44 -0500 )edit

I have an additional question related to Facemark API. I think that is too silly to question it in another (separate) question, but if you consider that it is better to create another, I’ll do.

Why “FacemarkKazemi” is not trainable while both “FacemarkLBF” and “FacemarkAAM” are trainable?

alfonsofernandezvillar gravatar imagealfonsofernandezvillar ( 2019-01-02 11:33:15 -0500 )edit

actually, to my knowledge, NONE of those are trainable from python (another bug here)

berak gravatar imageberak ( 2019-01-02 11:37:10 -0500 )edit

if you want my 2ct: go for the LBF model (it's pretty accurate and fast)

berak gravatar imageberak ( 2019-01-02 11:39:27 -0500 )edit