前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用CIDetector来人脸识别

利用CIDetector来人脸识别

原创
作者头像
大师级码师
修改2021-10-29 10:17:38
5180
修改2021-10-29 10:17:38
举报
文章被收录于专栏:大师级码师

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

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 利用CIDetector来人脸识别
  • 添加图片:
  • 识别图片:
  • 标出脸部,眼睛和嘴:
  • // 标出脸部
  • // 标出左眼
  • // 标出右眼
  • // 标出嘴部
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档