原文
通过图片的实验!Dlib 无法达到移动端视频的要求!但是可以根据上文进行优化,原理很简单,就是把图片缩小0.4,取得坐标,然后在放大 4 倍,就是原图的坐标,这时速度还是可以在移动端应用。
这篇博客比较详细
用openCv 测试了下 速度提升上去了,但是精确度却大打折扣。
还有一个方案就是用系统的AVMetadataFaceObject,去脸的区域,然后在用dlib 进行人脸对齐,取点。
相关demo
GitHub
openCv 对齐相关代码
- (void)openCv{
NSLog(@"openCv Start");
//get image path
std::string fileName =[imagePath UTF8String];
NSString *harr = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_alt2" ofType:@"xml"];
std::string harrName =[harr UTF8String];
cv::CascadeClassifier faceDetector;
faceDetector.load(harrName);
// Change to dlib's image format. No memory is copied.
array2d<rgb_pixel> img;
array2d<rgb_pixel> img_small;
//load ios image
load_image(img,fileName);
cv::Mat face = cv::imread(fileName);
// cv::resize(img, img_small, cv::Size(), 1.0/FACE_DOWNSAMPLE_RATIO, 1.0/FACE_DOWNSAMPLE_RATIO);
std::vector<cv::Rect> faces;
cv::Mat face_gray;
cvtColor( face, face_gray, CV_BGR2GRAY); //rgb类型转换为灰度类型
equalizeHist(face_gray, face_gray); //直方图均衡化
faceDetector.detectMultiScale(face_gray,faces,1.1,3,0|CV_HAAR_DO_CANNY_PRUNING,cv::Size(30,30));
// faceDetector.detectMultiScale(face_gray, faces, 1.1, 3, 0); // 分类器对象调用
NSLog(@"检测到人脸个数:%lu\n", faces.size());
printf("检测到人脸个数:%lu\n", faces.size());
if (faces.size() == 0) {
return;
}
for (int i = 0;i < faces.size(); i++) {
dlib::rectangle det;
//将opencv检测到的矩形转换为dlib需要的数据结构,这里没有判断检测不到人脸的情况
det.set_left(faces[i].x);
det.set_top(faces[i].y);
det.set_right(faces[i].x+faces[i].width);
det.set_bottom(faces[i].y+faces[i].height);
std::vector<full_object_detection> shapes;
full_object_detection shape = sp(img, det);
// and draw them into the image (samplebuffer)
for (unsigned long k = 0; k < shape.num_parts(); k++) {
point p = shape.part(k);
// NSLog(@"point %lu frmae %ld",k,p.x());
// p 点的直径 3 参数为原点直径 rgb_pixel 颜色
draw_solid_circle(img, p, 2, 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];
NSLog(@"openCv End");
cout << "Hit enter to process the next image..." << endl;
cin.get();
}