Learnning Dlib(四) Dlib face detector

官方例子
官方例子检测图片速度特别慢,有待后期优化。

#include <dlib/image_processing.h>
#include <dlib/image_io.h>
#include <dlib/array2d.h>
#include <iostream>
#include <dlib/image_processing/frontal_face_detector.h>
using namespace dlib;
using namespace std;
- (void)faceDetector{
    frontal_face_detector detector = get_frontal_face_detector();

    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"hao" ofType:@"jpg"];

    //creat rgb image
    array2d<rgb_pixel> img;

    //get image path
    std::string fileName =[imagePath UTF8String];

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

    std::vector<rectangle> dets = detector(img);
    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));
    }


    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];

    NSLog(@"人脸个数 %lu",dets.size());

}

一张脸结果如图

多张脸如图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值