OpenVC本地图片人脸识别

人脸识别有多种方式:
CoreImage、OpenCV、Vision、AVFoundation。
可参考大佬的:
https://www.jianshu.com/p/1eb1930562ca

1.jpg
//UIImageToMat
    cv::Mat cvimage;
    UIImageToMat(image, cvimage);
    
    if (!cvimage.empty()) {
        //转换为灰度图(降低复杂度,优化计算)
        cv::Mat gray;
        cvtColor(cvimage,gray,CV_BGR2GRAY);
        
        //直方图均匀化(改善图像的对比度和亮度)
        cv::Mat equalizedImg;
        equalizeHist(gray, equalizedImg);

        //存放人脸rect的vector
        std::vector faces;
        
        //加载opencv官方 人脸检测器
        NSString *cascadePath = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_alt2" ofType:@"xml"];
        faceDetector.load([cascadePath UTF8String]);
        
        //人脸识别
        faceDetector.detectMultiScale(gray, faces, 1.1, 3, 0, cv::Size(30,30));
        
        if (faces.size() > 0) {
            cout << "face:" << faces.size() << endl;
        }

        //遍历face ,在cvimage上画框
        for (vector::const_iterator rect = faces.begin(); rect != faces.end(); rect++) {
            rectangle(cvimage, cvPoint(rect->x, rect->y), cvPoint(rect->x + rect->width - 1, rect->y + rect->height - 1), cvScalar(255, 0, 255), 1, 1, 0);
        }
        
        UIImage *imageEND = MatToUIImage(cvimage);
        imageView.image = imageEND;
    }

如果需要交互,就需要自己去在imageView上加控件了:

  
        for (int i = 0; i < faces.size(); i++) {
            cv::Rect rect = faces[i];
            
            int x = rect.x;
            int y = rect.y;
            int width = rect.width;
            int height = rect.height;
            
            UIView *faceView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
            faceView.backgroundColor = [UIColor clearColor];
            faceView.layer.borderColor = [UIColor orangeColor].CGColor;
            faceView.layer.borderWidth = 1;
            
            [imageView addSubview:faceView];
            
            NSLog(@"face_%d: {%d, %d, %d, %d}",i,x,y,width,height);
        }
         

效果图:


WechatIMG26.jpeg

你可能感兴趣的:(OpenVC本地图片人脸识别)