Core Image人脸检测(iOS5新特性学习之三)

此次iOS5的一个新特性就是提供了人脸检测的API,这也是被媒体关注的一个功能,基本上,我看到的报道都是说iOS5提供了人脸识别的功能,然后又是设想要通过人脸来实现解锁屏幕等等,如何如何的。一开始,我也以为iOS5确实提供了这样的功能,这意味着可能不用opencv等静态库来实现了,免去了一旦OS版本升级要重新编译静态库的麻烦。研究了几天,发现并不是这么回事:


首先,此次iOS5提供的人脸检测API是Core Image这个Framework中,Core Image实际上在Mac OS下早就有了,只不过这次才引入到iOS5中。但是,apple还是比较谨慎的,并没有引入Core Image所有的功能到iOS下,只是提供了少数的API而已,而且可以看出基本都是对系统性能要求不怎么高的功能,可能apple对手机上做图像处理的性能问题还是有些顾虑的。例如,Core Image下图形处理的上百个filter,到了iOS下就只剩下20来个了,关键的轮廓提取,高斯模糊等filter都没有提供,很多复杂的处理都无法直接用Core Image来实现。但我相信随着iOS设备硬件性能的不断提上,开放所有的功能应该是迟早的事。


其次,谈谈人脸识别。严格的说,此次提供的API并不能叫人脸识别(face recognition),只能叫人脸检测(face detection),这二者在图像处理领域是有很大差别的,作用和实现难度都是天差地别。简单的说,人脸检测就是检测出图像中是否包含人脸,此次apple提供的API就是这个功能,可以给你指出图像中每一个人脸的位置,还有人脸中眼见、嘴巴的位置。而人脸识别则是更加高级的技术,可以告诉你几张照片中的人是不是同一人。是不是觉得在哪见过,没错,就是iPhoto里提供的功能。对apple来说,这样的技术也应该不是什么难事,关键是什么时候可以开放给开发者的问题了。


最后,当然要谈谈怎么用了。具体的使用,其实很简单,看看后面的代码你就明白了,如果要实现一些高级的应用,目前来说可能还得结合opencv或image-processing等开源的处理库来用。但如果只是简单的人脸检测,Core Image的效率就我个人感觉来说,还是很不错的,不比opencv的差。


人脸检测代码(大部分代码都是参考国外2篇文章的,在此特别指出)


[cpp]  view plain copy
  1. -(void)DetectFace{  
  2.   
  3.     UIImage* image = [UIImage imageNamed:@"face.png"];  
  4.     UIImageView testImage = [[UIImageView alloc] initWithImage: image];  
  5.     [testImage setTransform:CGAffineTransformMakeScale(1, -1)];  
  6.     [[[UIApplication sharedApplication] delegate].window setTransform:  
  7. CGAffineTransformMakeScale(1, -1)];  
  8.     [testImage setFrame:CGRectMake(0, 0, testImage.image.size.width,  
  9. testImage.image.size.height)];  
  10.     [self.view addSubview:testImage];  
  11.   
  12.     CIImage* ciimage = [CIImage imageWithCGImage:image.CGImage];  
  13.     NSDictionary* opts = [NSDictionary dictionaryWithObject:  
  14. CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];  
  15.     CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace  
  16. context:nil options:opts];  
  17.     NSArray* features = [detector featuresInImage:ciimage];  
  18.   
  19.     for (CIFaceFeature *faceFeature in features){  
  20.   
  21.         CGFloat faceWidth = faceFeature.bounds.size.width;  
  22.   
  23.         // create a UIView using the bounds of the face  
  24.         UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];  
  25.   
  26.         // add a border around the newly created UIView  
  27.   
  28.         faceView.layer.borderWidth = 1;  
  29.         faceView.layer.borderColor = [[UIColor redColor] CGColor];  
  30.   
  31.         [self.view addSubview:faceView];  
  32.   
  33.         if(faceFeature.hasLeftEyePosition)  
  34.   
  35.         {  
  36.             // create a UIView with a size based on the width of the face  
  37.   
  38.             UIView* leftEyeView = [[UIView alloc] initWithFrame:  
  39. CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15,  
  40. faceFeature.leftEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];  
  41.   
  42.             // change the background color of the eye view  
  43.             [leftEyeView setBackgroundColor:[[UIColor blueColor]  
  44. colorWithAlphaComponent:0.3]];  
  45.   
  46.             // set the position of the leftEyeView based on the face  
  47.             [leftEyeView setCenter:faceFeature.leftEyePosition];  
  48.   
  49.             // round the corners  
  50.             leftEyeView.layer.cornerRadius = faceWidth*0.15;  
  51.   
  52.             // add the view to the window  
  53.             [self.view  addSubview:leftEyeView];  
  54.   
  55.         }  
  56.   
  57.         if(faceFeature.hasRightEyePosition)  
  58.   
  59.         {  
  60.             // create a UIView with a size based on the width of the face  
  61.             UIView* leftEye = [[UIView alloc] initWithFrame:  
  62. CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15,  
  63. faceFeature.rightEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];  
  64.   
  65.             // change the background color of the eye view  
  66.             [leftEye setBackgroundColor:[[UIColor blueColor]  
  67. colorWithAlphaComponent:0.3]];  
  68.   
  69.             // set the position of the rightEyeView based on the face  
  70.             [leftEye setCenter:faceFeature.rightEyePosition];  
  71.   
  72.             // round the corners  
  73.             leftEye.layer.cornerRadius = faceWidth*0.15;  
  74.   
  75.             // add the new view to the window  
  76.             [self.view  addSubview:leftEye];  
  77.         }  
  78.   
  79.         if(faceFeature.hasMouthPosition)  
  80.         {  
  81.   
  82.             // create a UIView with a size based on the width of the face  
  83.             UIView* mouth = [[UIView alloc] initWithFrame:  
  84. CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2,  
  85. faceFeature.mouthPosition.y-faceWidth*0.2, faceWidth*0.4, faceWidth*0.4)];  
  86.   
  87.           // change the background color for the mouth to green  
  88.             [mouth setBackgroundColor:[[UIColor greenColor]  
  89. colorWithAlphaComponent:0.3]];  
  90.   
  91.             // set the position of the mouthView based on the face  
  92.             [mouth setCenter:faceFeature.mouthPosition];  
  93.   
  94.             // round the corners  
  95.             mouth.layer.cornerRadius = faceWidth*0.2;  
  96.   
  97.             // add the new view to the window  
  98.             [self.view  addSubview:mouth];  
  99.         }         
  100.   
  101.     }  
  102. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS上进行人脸识别并抠取人脸的方法可以使用Core Image框架中的CIDetector类。通过以下代码可以实现人脸识别和抠取人脸的功能: ```swift func detectFace(withImage image: UIImage) { // 将图像转为CIImage,使用Core Image需要使用CIImage guard let personCIImg = CIImage(image: image) else { return } // 设置识别精度 let opts: \[String: Any\] = \[CIDetectorAccuracy: CIDetectorAccuracyHigh\] // 初始化识别器 let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: opts) let result: \[CIFaceFeature\] = (detector?.features(in: personCIImg, options: opts) as? \[CIFaceFeature\])! if result.count > 0 { for face in result { let faceBox = UIView(frame: face.bounds) // 画一个红框画出面部位置 faceBox.layer.borderWidth = 3 faceBox.layer.borderColor = UIColor.red.cgColor faceBox.backgroundColor = UIColor.clear // 添加红框到图片上 imgView.addSubview(faceBox) print("面部坐标------> %d ", faceBox.frame) } } } ``` 这段代码会将传入的UIImage对象转换为CIImage对象,然后使用CIDetector进行人脸识别。识别到的人脸会通过在UIImageView上添加红色边框的方式进行标记。你可以根据需要对这段代码进行修改和扩展,以满足你的具体需求。 #### 引用[.reference_title] - *1* *2* [ios人脸识别_适用于Android和iOS的10种最佳人脸识别应用程序](https://blog.csdn.net/cumian8165/article/details/108160585)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [iOS人脸识别Demo](https://blog.csdn.net/kangpengpeng1/article/details/79197201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值