Using OpenCV and Dlib to predict facial attractiveness.
This project was written by python2, but it has been refactored by python3, and it runs with opencv and dlib, so we should init the environment at first.
$ brew install python3
$ pip3 install numpy
$ pip3 install sklearn
$ brew install opencv
Enter into your python site-packages path, I use python virtual env, and it is ~/Projects/pyvenv/lib/python3.6/site-packages
, then create soft link for cv2.so.
$ ln -s /usr/local/Cellar/opencv/3.4.0_1/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so
After that, you can use cv2 in your python project.
import cv2
Execute commands below, it may spend some time to setup dlib, be patient.
$ brew install cmake
$ brew install dlib
$ pip install dlib
Download data file from http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
And replace the file: data/shape_predictor_68_face_landmarks.dat
The workflow of this project is:
1. Train Model
2. Get Landmarks
3. Generate Features
4. Predict
Every step has a python file, you can run them one by one.
Generate the model which will be used to predict facial attractiveness later, it will read data from data/train_features
and /data/train_ratings
.
$ python source/trainModel.py
Get the landmarks of the faces detected in the image/test.jpg
, it will read data from data/shape_predictor_68_face_landmarks.dat
and then output a file named data/my_landmarks
.
$ python source/getLandmarks.py
Generate the features which will be as the input of the model we get before, it will output a file named data/my_features
.
$ python source/generateFeatures.py
Predict facial attractiveness
$ python source/myPredict.py
If you want to run source/run.py
to reduce operations, you should pay attention to that maybe program read a empty file which is generated by the previous step.
$ python source/run.py
-
Use the grammer of python3 to make it run, such as
print
toprint()
, changemin_samples_split = 1
intomin_samples_split = 1.0
and reshape the dimensions of array data in some necessary place. -
There should be a loop in the
trainModel
process, if not, the predict result will be all the same, because themy_face_rating.pkl
was generated incompletely.
[
[ 2.70908844]
[ 2.70908844]
[ 2.70908844]
[ 2.70908844]
[ 2.70908844]
[ 2.70908844]
]
-
If just one face was detected, it would be error. Fix it by reshape array data in this case.
-
Rename some files to make the structure of this project more clear.