利用CIDetector来人脸识别

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

利用CIDetector来人脸识别

添加图片:

UIImage* image = [UIImage imageNamed:@"face.jpg"];
UIImageView *testimage = [[UIImageView alloc] initWithImage: image];
[testimage setTransform:CGAffineTransformMakeScale(1,-1)];
[[[UIApplication sharedApplication] delegate].window setTransform:
     CGAffineTransformMakeScale(1,-1)];
 [testimage setFrame:CGRectMake(, ,testimage.image.size.width,
                                   testimage.image.size.height)];
[self.view addSubview:testimage];

识别图片:

CIImage* ciimage = [CIImage imageWithCGImage:image.CGImage];
NSDictionary* opts = [NSDictionary dictionaryWithObject:
                          CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:nil options:opts];
NSArray* features = [detector featuresInImage:ciimage];

标出脸部,眼睛和嘴:

for (CIFaceFeature *faceFeature in features){
}

// 标出脸部

CGFloat faceWidth = faceFeature.bounds.size.width;
UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
faceView.layer.borderWidth = 1;
faceView.layer.borderColor = [[UIColor redColor] CGColor];
[self.view addSubview:faceView];

// 标出左眼

if(faceFeature.hasLeftEyePosition) {
      UIView* leftEyeView = [[UIView alloc] initWithFrame:
                                   CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15,
                                              faceFeature.leftEyePosition.y-faceWidth*0.15,faceWidth*0.3,faceWidth*0.3)];
     [leftEyeView setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]];
     [leftEyeView setCenter:faceFeature.leftEyePosition];
     leftEyeView.layer.cornerRadius = faceWidth*0.15;
     [self.view  addSubview:leftEyeView];
}

// 标出右眼

if(faceFeature.hasRightEyePosition) {
       UIView* leftEye = [[UIView alloc] initWithFrame:
                               CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15,
                                          faceFeature.rightEyePosition.y-faceWidth*0.15,faceWidth*0.3)];
       [leftEye setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]];
       [leftEye setCenter:faceFeature.rightEyePosition];
       leftEye.layer.cornerRadius = faceWidth*0.15;
       [self.view  addSubview:leftEye];
}

// 标出嘴部

if(faceFeature.hasMouthPosition) {
    UIView* mouth = [[UIView alloc] initWithFrame:
                             CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2,
                                        faceFeature.mouthPosition.y-faceWidth*0.2,faceWidth*0.4,faceWidth*0.4)];
     [mouth setBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0.3]];
      [mouth setCenter:faceFeature.mouthPosition];
      mouth.layer.cornerRadius = faceWidth*0.2;
      [self.view  addSubview:mouth]; 
}

code


以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...