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

}

一張臉結果如圖

多張臉如圖

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