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();


 }

一张脸
多张脸

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值