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

前言

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就是我们要设置的参数字典。

  • 我们这里设置了一个识别精度CIDetectorAccuracy,识别精度的值有:
// 识别精度低,但识别速度快、性能高
CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyLow NS_AVAILABLE(10_7, 5_0); 
// 识别精度高,但识别速度慢、性能低
CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyHigh NS_AVAILABLE(10_7, 5_0); 
  • 除了精度的设置,还有CIDetectorTracking,指定使用特征跟踪,这个功能就像相机中的人脸跟踪功能。
  • CIDetectorMinFeatureSize用于设置将要识别的特征的最小尺寸,也就是说小于这个尺寸的特征将不识别。
    @ 对于人脸检测器,这个关键字的值是从0.0 ~ 1.0NSNumber值,这个值表示:基于输入图像短边长度的百分比。有效值范围:0.01 <= CIDetectorMinFeatureSize <= 0.5。为这个参数设定更高值仅用于提高性能。默认值是0.15
    @ 对于矩形探测器,这个关键字的值是从0.0 ~ 1.0NSNumber值,这个值表示:基于输入图像短边长度的百分比。有效值范围:0.2 <= CIDetectorMinFeatureSize <= 1.0的默认值是0.2
    @ 对于文本探测器,这个关键字的值是一个范围从0.0 ~ 1.0NSNumber值,这个值表示:基于输入图像高度的百分比。有效值范围:0.0 <= CIDetectorMinFeatureSize <= 1.0。默认值是10/(输入图像的高度)

  • CIDetectorMaxFeatureCount是针对矩形探测器的,用于设置返回矩形特征的最多个数。这个关键字的值是一个1~...NSNumber值。有效范围1 < = CIDetectorMaxFeatureCount < = 256。默认值为1

  • CIDetectorNumberOfAngles用于设置角度的个数,值是1、3、5、7、9、11中的一个值。

  • CIDetectorImageOrientation用于设置识别方向,值是一个从1 ~ 8的整型的NSNumber。如果值存在,检测将会基于这个方向进行,但返回的特征仍然是基于这些图像的。

  • CIDetectorEyeBlink如果设置这个参数为true(bool类型的NSNumber),识别器将提取眨眼特征。

  • CIDetectorSmile如果设置这个参数为ture(bool类型的NSNumber),识别器将提取微笑特征。

  • CIDetectorFocalLength用于设置每帧焦距,值得类型为floot类型NSNumber

  • CIDetectorAspectRatio用于设置矩形的长宽比,值得类型为floot类型NSNumber

  • CIDetectorReturnSubFeatures控制文本检测器是否应该检测子特征。默认值是否,值的类型为bool类型NSNumber

使用实例

进行识别的函数如下:

- (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];
    }
}

iOS图像处理(四)CIDetector特征识别(人脸识别)_第1张图片

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

实例代码

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

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

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

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

你可能感兴趣的:(iOS图像处理(四)CIDetector特征识别(人脸识别))