官方例子
官方例子检测图片速度特别慢,有待后期优化。
#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());
}