/**
* 注意:CIFaceFeature 提供的坐标都是和CGContext一样是反方向的,所以以下的画图操作都是用了CGContext的方式
*/
- (void)faceRegonized {
CIContext *context = [CIContext contextWithOptions:nil];
UIImage *imageInput = [UIImage imageNamed:@"example"];
CIImage *image = [CIImage imageWithCGImage:imageInput.CGImage];
//设置识别参数
NSDictionary *param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh
forKey:CIDetectorAccuracy];
//声明一个CIDetector,并设定识别类型
CIDetector* faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace
context:context options:param];//取得识别结果
NSArray *detectResult = [faceDetector featuresInImage:image];
//开始一个画布,并将画布原图画到画布中去
UIGraphicsBeginImageContext(image.size);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
for(CIFaceFeature* faceFeature in detectResult) {
//脸部
UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
faceView.layer.borderWidth = 1;
faceView.layer.borderColor = [UIColor orangeColor].CGColor;
CGFloat faceWidth = faceFeature.bounds.size.width;
//CIFaceFeature 所有的属性
NSLog(@"-------------------------------------\n");
NSLog(@"faceBounds = %@",NSStringFromCGRect(faceFeature.bounds));
NSLog(@"face hasLeftEyePosition = %d",faceFeature.hasLeftEyePosition);
NSLog(@"face leftEyePosition = %@",NSStringFromCGPoint(faceFeature.leftEyePosition));
NSLog(@"face hasRightEyePosition = %d",faceFeature.hasRightEyePosition);
NSLog(@"face rightEyePosition = %@",NSStringFromCGPoint(faceFeature.rightEyePosition));
NSLog(@"face hasMouthPosition = %d",faceFeature.hasMouthPosition);
NSLog(@"face mouthPosition = %@",NSStringFromCGPoint(faceFeature.mouthPosition));
NSLog(@"face hasTrackingID = %d",faceFeature.hasTrackingID);
NSLog(@"face trackingID = %d",faceFeature.trackingID);
NSLog(@"face hasTrackingFrameCount = %d",faceFeature.hasTrackingFrameCount);
NSLog(@"face trackingFrameCount = %d",faceFeature.trackingFrameCount);
NSLog(@"face hasFaceAngle = %d",faceFeature.hasFaceAngle);
NSLog(@"face faceAngle = %f",faceFeature.faceAngle);
NSLog(@"face hasSmile = %d",faceFeature.hasSmile);
NSLog(@"face leftEyeClosed = %d",faceFeature.leftEyeClosed);
NSLog(@"face rightEyeClosed = %d",faceFeature.rightEyeClosed);
NSLog(@"\n-------------------------------------");
//画一个矩形出识别出来的脸部的方位
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint point = faceFeature.bounds.origin;
CGSize size = faceFeature.bounds.size;
[path moveToPoint:point];
[path addLineToPoint:CGPointMake(point.x + size.width, point.y)];
[path addLineToPoint:CGPointMake(point.x + size.width, point.y + size.height)];
[path addLineToPoint:CGPointMake(point.x, point.y+ size.height)];
[path addLineToPoint:point];
[[UIColor redColor] setStroke];
[path stroke];
[[UIColor redColor] setStroke];
[path stroke];
//左眼
if (faceFeature.hasLeftEyePosition) {
[self drawCycleAtPoint:faceFeature.leftEyePosition withRadius:faceWidth*0.1];
}
//右眼
if (faceFeature.hasRightEyePosition) {
[self drawCycleAtPoint:faceFeature.rightEyePosition withRadius:faceWidth*0.1];
}
//嘴巴
if (faceFeature.hasMouthPosition) {
[self drawCycleAtpoint:faceFeature.mouthPosition withRadius:faceWidth*0.2];
}
}
//获得所有在原图中圈出来脸之后最后的图片,此时的图片是反方向的
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
//将画布清空,并把放方向的图片用CG的方式画到原图中去(CG画出来的东西都是原来的反方向,并不是个好方法,实际开发中用别的方法)
CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height));
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), tempImage.CGImage);
//获得重画之后的图片,并结束画布
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//将最终的图片放到View中去
[self.imageView setImage:tempImage];
}
- (void)drawCycleAtPoint:(CGPoint)point withRadius:(CGFloat)radius {
/*画圆*/
//边框圆
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1,0,0,1.0);//画笔线的颜色
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);//线的宽度
//void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
// x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。
CGContextAddArc(UIGraphicsGetCurrentContext(), point.x, point.y, radius, 0, 2*M_PI, 0); //添加一个圆
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathStroke); //绘制路径
}
系统人脸识别:iOS 官方人脸检测Demo
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 近几年随着移动设备硬件设备越来越优各种美颜相机App应运而生,美颜、瘦脸、添加挂件等等一系列的功能,这其中的原理一...
- 上两篇文章介绍了用iOS原生的人脸检测 与 Dlib的关键点检测来实现的 人脸关键点识别,相信已经实现的小伙伴会感...
- 上篇文章 人脸框检测 已经介绍了我们如果从视频流中检测出人脸位置的信息,基于这些内容,我们继续向下扩展,获取人脸6...
- 在冬季很多人往往都会选择黑白灰三色单品或者大地色系单品来混搭,其实除了这几个百搭色,还是有不少好搭又好看的色调可以...