iOS: 检测拍摄的图片中是否有人脸
2019-04-23 本文已影响4人
Sxiaobai
最近开发的app中有个实名认证的环节,需要上传一张自拍照,为了检测用
户上传审核的是人脸照片而非其他的东西,就用到了系统自带的检测人脸的方
法,废话不多说直接上代码:
- (BOOL)detectionOfFaceWithImage:(UIImage *)image {
NSDictionary *imageOptions = [NSDictionary dictionaryWithObject:@(5) forKey:CIDetectorImageOrientation];
CIImage *personciImage = [CIImage imageWithCGImage:image.CGImage];
NSDictionary *opts = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:opts];
NSArray *features = [faceDetector featuresInImage:personciImage options:imageOptions];
if (features.count > 0) return YES; else return NO;
}
检测到人脸.png