码迷,mamicode.com
首页 > 其他好文 > 详细

利用CIDetector来人脸识别

时间:2015-02-25 15:37:02      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:人脸识别   ios   cidetector   

利用CIDetector来人脸识别

by 伍雪颖

利用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(0, 0, 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 blueColorcolorWithAlphaComponent: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, faceWidth*0.3)];
       [leftEye setBackgroundColor:[[UIColor blueColorcolorWithAlphaComponent: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 greenColorcolorWithAlphaComponent:0.3]];
      [mouth setCenter:faceFeature.mouthPosition];
      mouth.layer.cornerRadius = faceWidth*0.2;
      [self.view  addSubview:mouth]; 
}

code


利用CIDetector来人脸识别

标签:人脸识别   ios   cidetector   

原文地址:http://blog.csdn.net/rainlesvio/article/details/43937663

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!