Skip to content

Instantly share code, notes, and snippets.

@Wessi
Last active July 20, 2017 10:23
Show Gist options
  • Save Wessi/a104818f13bacb421d5f5dfe5d8d1cc1 to your computer and use it in GitHub Desktop.
Save Wessi/a104818f13bacb421d5f5dfe5d8d1cc1 to your computer and use it in GitHub Desktop.
Code that extracts the 68 point landmarks from dlib
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <fstream>
using namespace dlib;
using namespace std;
double lmPoints[68][2];
int main()
{
int noOfFrames = 0;
try
{
cv::VideoCapture cap("/home/icog-labs/puppeteer/AllSingleFrames/Stretchm/Stretchm.mp4"); ///home/icog-labs/Downloads/Evolve_Sophia_demo.mkv //"/home/icog-labs/puppeteer/AllSingleFrames/BrowsDown.mp4"
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("/home/icog-labs/wessi/shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
ofstream eyebrow_r ("/home/icog-labs/puppeteer/CSVs/CSV-080617/Stretchm.csv");
eyebrow_r << "Frame";
for (int i=0; i<68; i++){
eyebrow_r << ", X"<<i<<", Y"<<i;
}
while(!win.is_closed())
{
// Grab a frame
cv::Mat temp;
cap >> temp;
// Turn OpenCV's Mat into something dlib can deal with. Note that this just
// wraps the Mat object, it doesn't copy anything. So cimg is only valid as
// long as temp is valid. Also don't do anything to temp that would cause it
// to reallocate the memory which stores the image as that will make cimg
// contain dangling pointers. This basically means you shouldn't modify temp
// while using cimg.
cv_image<bgr_pixel> cimg(temp);
// Detect faces
std::vector<rectangle> faces = detector(cimg);
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
// Display it all on the screen
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
noOfFrames++;
// cout << "Frame with face detected... " << noOfFrames << endl;
/*load demo1w, demo2w,... and write the dlib points around the eyebrow to the text file named demo1w and demo2w separately...
*/
if(shapes.size()>0) {
// noOfFrames++;
// cout << "Frame with face detected... " << noOfFrames << endl;
for (unsigned long i = 0; i < shapes.size(); ++i) // Iterate through shapes
{
const full_object_detection& d = shapes[i]; // Hold the detected face i
for (unsigned long i = 0; i < 68; ++i) // Hold all the 68 face landmark coordinates(x, y)
{
lmPoints[i][0] = (double)d.part(i).x();
lmPoints[i][1] = (double)d.part(i).y();
}
}
if (eyebrow_r.is_open()){
cout << "\n################## Frame-" << noOfFrames << " ##################"<< endl;
eyebrow_r << "\n" << noOfFrames; //myfile << ", "<<i<<", "<<i;
for (int a = 0; a<68; a++){
cout << "index-" <<a<<", X="<<lmPoints[a][0]<<", Y="<<lmPoints[a][1]<<endl;
eyebrow_r << ", "<<lmPoints[a][0]<<", "<<lmPoints[a][1];
}
} else cout<<"\nUnable to open file!\n";
}else{
cout << "No Frame with face detected... " << endl;
}
}
if (noOfFrames > 0){
Con
}
eyebrow_r.close();
}
catch(serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch(exception& e)
{
cout << e.what() << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment