iOS移动开发社区iOS 艾欧艾斯iOS开发技术

iOS图像处理(四)CIDetector特征识别(人脸识别)

2016-06-27  本文已影响8651人  JerryLMJ

前言

CIDetectorCore Image框架中提供的一个识别类,包括对人脸、形状、条码、文本的识别,本文主要介绍人脸特征识别。

人脸识别功能不单单可以对人脸进行获取,还可以获取眼睛和嘴等面部特征信息。但是CIDetector不包括面纹编码提取,也就是说CIDetector只能判断是不是人脸,而不能判断这张人脸是谁的,比如说面部打卡这种功能是实现不了的。

创建

// 创建图形上下文
CIContext * context = [CIContext contextWithOptions:nil];
// 创建自定义参数字典
NSDictionary * param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
// 创建识别器对象
CIDetector * faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:param];

我们先来看看识别器的类型都有哪些,这里我们设置的是CIDetectorTypeFace,人脸识别探测器类型。

// 人脸识别探测器类型
CORE_IMAGE_EXPORT NSString* const CIDetectorTypeFace NS_AVAILABLE(10_7, 5_0);
// 矩形检测探测器类型
CORE_IMAGE_EXPORT NSString* const CIDetectorTypeRectangle NS_AVAILABLE(10_10, 8_0);
// 条码检测探测器类型
CORE_IMAGE_EXPORT NSString* const CIDetectorTypeQRCode NS_AVAILABLE(10_10, 8_0);
// 文本检测探测器类型
#if __OBJC2__
CORE_IMAGE_EXPORT NSString* const CIDetectorTypeText NS_AVAILABLE(10_11, 9_0);
#endif

参数设置

说完识别器的类型我们再来看看,识别器的参数设置。识别器参数的设置是以一个字典形式的参数传入的。这里的NSDictionary * param就是我们要设置的参数字典。

// 识别精度低,但识别速度快、性能高
CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyLow NS_AVAILABLE(10_7, 5_0); 
// 识别精度高,但识别速度慢、性能低
CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyHigh NS_AVAILABLE(10_7, 5_0); 

使用实例

进行识别的函数如下:

- (CI_ARRAY(CIFeature*) *)featuresInImage:(CIImage *)image
    NS_AVAILABLE(10_7, 5_0);
- (CI_ARRAY(CIFeature*) *)featuresInImage:(CIImage *)image
                                  options:(nullable CI_DICTIONARY(NSString*,id) *)options
    NS_AVAILABLE(10_8, 5_0);

实例
这是一个在图像上标注眼睛和嘴的代码片段

UIImage * imageInput = [_inputImgView image];
CIImage * image = [CIImage imageWithCGImage:imageInput.CGImage];

CIContext * context = [CIContext contextWithOptions:nil];
NSDictionary * param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
CIDetector * faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:param];

NSArray * detectResult = [faceDetector featuresInImage:image];

UIView * resultView = [[UIView alloc] initWithFrame:_inputImgView.frame];
[self.view addSubview:resultView];

for (CIFaceFeature * faceFeature in detectResult) {
    UIView *faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
    faceView.layer.borderColor = [UIColor redColor].CGColor;
    faceView.layer.borderWidth = 1;
    [resultView addSubview:faceView];
    
    
    if (faceFeature.hasLeftEyePosition) {
        UIView * leftEyeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        [leftEyeView setCenter:faceFeature.leftEyePosition];
        leftEyeView.layer.borderWidth = 1;
        leftEyeView.layer.borderColor = [UIColor redColor].CGColor;
        [resultView addSubview:leftEyeView];
    }
    
    
    if (faceFeature.hasRightEyePosition) {
        UIView * rightEyeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        [rightEyeView setCenter:faceFeature.rightEyePosition];
        rightEyeView.layer.borderWidth = 1;
        rightEyeView.layer.borderColor = [UIColor redColor].CGColor;
        [resultView addSubview:rightEyeView];
    }
    
    if (faceFeature.hasMouthPosition) {
        UIView * mouthView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 5)];
        [mouthView setCenter:faceFeature.mouthPosition];
        mouthView.layer.borderWidth = 1;
        mouthView.layer.borderColor = [UIColor redColor].CGColor;
        [resultView addSubview:mouthView];
    }
}

在以上的代码中
NSArray * detectResult = [faceDetector featuresInImage:image];
detectResult是识别后返回的一个结果数组,元素类型为CIFaceFeature,这是一个人脸特征类,其中包括了的面部上的一些特征属性,大家可以去这个类的头文件中看一下,都是一些浅显易懂的属性,这里不再介绍。

实例代码

这里是一个关于人脸识别的Demo:
https://github.com/MajorLMJ/LMJFaceRecognition

再多说一句其实我们可以通过人脸识别的功能实现像微信等应用的头像自动识别剪裁功能。

上一篇iOS图像处理(三)CIFilter滤镜介绍

版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!

上一篇下一篇

猜你喜欢

热点阅读