利用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, 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];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,248评论 5 13
  • --绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益...
    韩七夏阅读 2,943评论 2 10
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,678评论 6 30
  • 学校后面有一大排破旧的老墙,墙上爬满郁郁葱葱的爬山虎,不知怎的,每次抬头看到这一望无际的爬山虎,我总会想起那个满脸...
    凤之子阅读 625评论 0 2
  • 开始
    闪闪发光的Lucky阅读 106评论 0 1

友情链接更多精彩内容