Learnning Dlib(五) Dlib face landmark detection

官方例子
人臉模型68點繪製,非常非常慢,需要優化。

下載模型

下載後放入lib 目錄下
這裏寫圖片描述

代碼如下

@interface ViewController ()
{
    shape_predictor sp;
    NSString *imagePath;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    imagePath = [[NSBundle mainBundle] pathForResource:@"hao" ofType:@"jpg"];
    self.imageView.image = [UIImage imageWithContentsOfFile:imagePath];

    NSString *modelFileName = [[NSBundle mainBundle] pathForResource:@"shape_predictor_68_face_landmarks" ofType:@"dat"];
    std::string modelFileNameCString = [modelFileName UTF8String];
    deserialize(modelFileNameCString) >> sp;
}
- (void)faceDetector{

    std::string fileName = [imagePath UTF8String];

    //creat image
    array2d<rgb_pixel> img;

    frontal_face_detector detector = get_frontal_face_detector();

    // And we also need a shape_predictor.  This is the tool that will predict face
    // landmark positions given an image and face bounding box.  Here we are just
    // loading the model from the shape_predictor_68_face_landmarks.dat file you gave
    // as a command line argument.

        //load ios image
    load_image(img,fileName);

    // Make the image bigger by a factor of two.  This is useful since
    // the face detector looks for faces that are about 80 by 80 pixels
    // or larger.  Therefore, if you want to find faces that are smaller
    // than that then you need to upsample the image as we do here by
    // calling pyramid_up().  So this will allow it to detect faces that
    // are at least 40 by 40 pixels in size.  We could call pyramid_up()
    // again to find even smaller faces, but note that every time we
    // upsample the image we make the detector run slower since it must
    // process a larger image.
    pyramid_up(img);

    // Now tell the face detector to give us a list of bounding boxes
    // around all the faces in the image.
    std::vector<rectangle> dets = detector(img);
    NSLog(@"人臉個數 %lu",dets.size());//檢測到人臉的數量

    if (dets.size() == 0) {
        return;
    }

//    // draw Rectangle on face
//    for (int i = 0;i < dets.size(); i++) {
//        draw_rectangle(img,dets[i],rgb_pixel(0, 255, 255));
//    }


    std::vector<full_object_detection> shapes;

    for (unsigned long j = 0; j < dets.size(); ++j)
    {
        full_object_detection shape = sp(img, dets[j]);

        // and draw them into the image (samplebuffer)
        for (unsigned long k = 0; k < shape.num_parts(); k++) {
            dlib::point p = shape.part(k);
            // p 點的直徑 3 參數爲原點直徑 rgb_pixel 顏色
            draw_solid_circle(img, p, 3, dlib::rgb_pixel(0, 255, 255));

        }
        std::cout << "number of parts: "<< shape.num_parts() << std::endl;
        std::cout << "pixel position of first part:  " << shape.part(0) << std::endl;
        std::cout << "pixel position of second part: " << shape.part(1) <<  std::endl;
        // You get the idea, you can get all the face part locations if
        // you want them.  Here we just store them in shapes so we can
        // put them on the screen.
        shapes.push_back(shape);
    }

    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [docPath objectAtIndex:0];
    documentsPath = [documentsPath stringByAppendingPathComponent:@"test.png"];
    const char *savePath = [documentsPath UTF8String];
    save_jpeg(img, savePath);
    self.imageView.image = [UIImage imageWithContentsOfFile:documentsPath];

    cout << "Hit enter to process the next image..." << endl;
    cin.get();


 }

一張臉
多張臉

發佈了161 篇原創文章 · 獲贊 91 · 訪問量 188萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章