CoreImage(三)CIDetector人脸识别和二维码扫描

CIDetector是用来分析CIImage,得到CIFeature。用来识别特定特征图片的类,主要可用来识别人脸、

属性和方法介绍

识别类型

//人脸
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);
//文本
CORE_IMAGE_EXPORT NSString* const CIDetectorTypeText 

使用方法 :

 作为type参数
// Types to be used for +[CIDetector detectorOfType:context:options:]
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:param];

参数设置

不同参数具有不同识别效果,参数通过字典进行设置
精度设置

//key
CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracy NS_AVAILABLE(10_7, 5_0);

//value
//精度低,速度快
CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyLow NS_AVAILABLE(10_7, 5_0); 
//精度高,速度慢
CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyHigh NS_AVAILABLE(10_7, 5_0); 

特征追踪

//key 用于视频中的特征追踪 value为@YES @NO
CORE_IMAGE_EXPORT NSString* const CIDetectorTracking NS_AVAILABLE(10_8, 6_0);

特征识别尺寸

//key  最小的特征识别尺寸 value为0.0 ~ 1.0 (NSnumber)
CORE_IMAGE_EXPORT NSString* const CIDetectorMinFeatureSize NS_AVAILABLE(10_8, 6_0);

 //key  最大的特征识别尺寸 value为1 ~ 256 (NSnumber)
CORE_IMAGE_EXPORT NSString* const CIDetectorMaxFeatureCount NS_AVAILABLE(10_12, 10_0);

使用方法:

//使用方法 作为Options参数传入
// Options that can be used with +[CIDetector detectorOfType:context:options:]
NSDictionary *options = @{CIDetectorAccuracy:CIDetectorAccuracyHigh};
CIDetector * faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:options];

具体特征识别

// 角度个数 value为1,3,5,7,9,11 (NSnumber)之一
CORE_IMAGE_EXPORT NSString* const CIDetectorNumberOfAngles NS_AVAILABLE(10_11, 9_0);

// 图片方向 value为1 ~ 8(NSnumber)之一
CORE_IMAGE_EXPORT NSString *const CIDetectorImageOrientation NS_AVAILABLE(10_8, 5_0);

//识别眨眼 value为@YES,@NO
CORE_IMAGE_EXPORT NSString *const CIDetectorEyeBlink NS_AVAILABLE(10_9, 7_0);

//识别微笑 value为@YES,@NO
CORE_IMAGE_EXPORT NSString *const CIDetectorSmile NS_AVAILABLE(10_9, 7_0);

//焦距 value为float (NSNumber)
CORE_IMAGE_EXPORT NSString* const CIDetectorFocalLength NS_AVAILABLE(10_10, 8_0);

//矩形宽高比 value为float (NSNumber)
CORE_IMAGE_EXPORT NSString* const CIDetectorAspectRatio NS_AVAILABLE(10_10, 8_0);

//识别子特征 value为@YES,@NO
CORE_IMAGE_EXPORT NSString* const CIDetectorReturnSubFeatures __OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0);

使用方法:

 //作为Options参数传入
 // Options that can be used with -[CIDetector featuresInImage:options:]
 NSDictionary *options = @{CIDetectorSmile: [NSNumber numberWithBool:true], CIDetectorEyeBlink:[NSNumber numberWithBool:true]};
 NSArray *detectResult = [faceDetector featuresInImage:cImage options:options];

具体使用

人脸识别

可以识别多人,区分人与动物的脸,判断人脸是否在笑和眨眼,但是不能做到区分人的脸,不能做面部解锁等功能。

CIImage *cImage = [CIImage imageWithCGImage:image.CGImage];
    
//设置人脸识别精度
NSDictionary *options = [NSDictionary dictionaryWithObject:
                          CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
//创建人脸探测器
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
                    context:nil options:options];
//获取人脸识别
NSArray *detectResult = [detector featuresInImage:cImage];
UIView * resultView = [[UIView alloc] initWithFrame:_imageView.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];
    }
    
        //具体特征识别
    if (faceFeature.hasSmile) {
        NSLog(@"再笑");
    }
    if (faceFeature.leftEyeClosed) {
        NSLog(@"左眼闭了");
    }
    if (faceFeature.rightEyeClosed) {
        NSLog(@"右眼闭了");
    }
}
    
    [resultView setTransform:CGAffineTransformMakeScale(1, -1)];
}
CoreImage(三)CIDetector人脸识别和二维码扫描_第1张图片
人脸识别

二维码识别

CIDetector *qrCodeDetector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
NSData *imageData = UIImagePNGRepresentation(_qrImage);
CIImage *ciImage = [CIImage imageWithData:imageData];
NSArray *features = [qrCodeDetector featuresInImage:ciImage];
CIQRCodeFeature *feature = [features objectAtIndex:0];
NSString *scannedResult = feature.messageString;
NSLog(@"%@",scannedResult);
CoreImage(三)CIDetector人脸识别和二维码扫描_第2张图片
二维码识别

注意事项

坐标位置
CI坐标系是以左下角为原点的,和我们以往UIkit中左上角为原点是不一样的,所以最后的view添加这个段代码

[resultView setTransform:CGAffineTransformMakeScale(1, -1)];

有时候我们使用图片,imageview和image的图片不一样大,默认照片的图片像素很高,我们要将获取的位置进行调整。将坐标乘以以下系数。

#define ConvertX (self.imageView.frame.size.width) / _image.size.width
#define ConvertY (self.imageView.frame.size.height) / _image.size.height

我们也可以在一开始进行特征识别时,将图片缩放至imageview的大小(但是图片识别能力会下降)

float xfactor = self.imageView.bounds.size.width/image.size.width;
float yfactor = self.imageView.bounds.size.height/image.size.height;
cImage = [cImage imageByApplyingTransform:CGAffineTransformMakeScale(xfactor, yfactor)];

使用方法
设置精度的方法和设置具体特征方法不要写在同一个字典中,两个设置的方法是不一样的

 //设置高精度人脸识别器
NSDictionary *param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:param];

//设置微笑和闭眼识别器
NSDictionary *options = @{CIDetectorSmile: [NSNumber numberWithBool:true], CIDetectorEyeBlink:[NSNumber numberWithBool:true]};
NSArray *detectResult = [faceDetector featuresInImage:cImage options:options];

Demo

Demo地址:人脸识别和二维码扫描

你可能感兴趣的:(CoreImage(三)CIDetector人脸识别和二维码扫描)