系统人脸识别:iOS 官方人脸检测Demo
1 /** 2 * 注意:CIFaceFeature 提供的坐标都是和CGContext一样是反方向的,所以以下的画图操作都是用了CGContext的方式 3 */ 4 - (void)faceRegonized { 5 CIContext *context = [CIContext contextWithOptions:nil]; 6 UIImage *imageInput = [UIImage imageNamed:@"example"]; 7 CIImage *image = [CIImage imageWithCGImage:imageInput.CGImage]; 8 9 //设置识别参数 10 NSDictionary *param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh 11 forKey:CIDetectorAccuracy]; 12 //声明一个CIDetector,并设定识别类型 13 CIDetector* faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace 14 context:context options:param];//取得识别结果 15 NSArray *detectResult = [faceDetector featuresInImage:image]; 16 17 //开始一个画布,并将画布原图画到画布中去 18 UIGraphicsBeginImageContext(image.size); 19 CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage); 20 21 for(CIFaceFeature* faceFeature in detectResult) { 22 //脸部 23 UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds]; 24 faceView.layer.borderWidth = 1; 25 faceView.layer.borderColor = [UIColor orangeColor].CGColor; 26 27 CGFloat faceWidth = faceFeature.bounds.size.width; 28 29 30 //CIFaceFeature 所有的属性 31 NSLog(@"-------------------------------------\n"); 32 NSLog(@"faceBounds = %@",NSStringFromCGRect(faceFeature.bounds)); 33 NSLog(@"face hasLeftEyePosition = %d",faceFeature.hasLeftEyePosition); 34 NSLog(@"face leftEyePosition = %@",NSStringFromCGPoint(faceFeature.leftEyePosition)); 35 NSLog(@"face hasRightEyePosition = %d",faceFeature.hasRightEyePosition); 36 NSLog(@"face rightEyePosition = %@",NSStringFromCGPoint(faceFeature.rightEyePosition)); 37 NSLog(@"face hasMouthPosition = %d",faceFeature.hasMouthPosition); 38 NSLog(@"face mouthPosition = %@",NSStringFromCGPoint(faceFeature.mouthPosition)); 39 NSLog(@"face hasTrackingID = %d",faceFeature.hasTrackingID); 40 NSLog(@"face trackingID = %d",faceFeature.trackingID); 41 NSLog(@"face hasTrackingFrameCount = %d",faceFeature.hasTrackingFrameCount); 42 NSLog(@"face trackingFrameCount = %d",faceFeature.trackingFrameCount); 43 NSLog(@"face hasFaceAngle = %d",faceFeature.hasFaceAngle); 44 NSLog(@"face faceAngle = %f",faceFeature.faceAngle); 45 NSLog(@"face hasSmile = %d",faceFeature.hasSmile); 46 NSLog(@"face leftEyeClosed = %d",faceFeature.leftEyeClosed); 47 NSLog(@"face rightEyeClosed = %d",faceFeature.rightEyeClosed); 48 NSLog(@"\n-------------------------------------"); 49 50 //画一个矩形出识别出来的脸部的方位 51 UIBezierPath *path = [UIBezierPath bezierPath]; 52 CGPoint point = faceFeature.bounds.origin; 53 CGSize size = faceFeature.bounds.size; 54 [path moveToPoint:point]; 55 [path addLineToPoint:CGPointMake(point.x + size.width, point.y)]; 56 [path addLineToPoint:CGPointMake(point.x + size.width, point.y + size.height)]; 57 [path addLineToPoint:CGPointMake(point.x, point.y+ size.height)]; 58 [path addLineToPoint:point]; 59 [[UIColor redColor] setStroke]; 60 [path stroke]; 61 62 [[UIColor redColor] setStroke]; 63 [path stroke]; 64 65 //左眼 66 if (faceFeature.hasLeftEyePosition) { 67 [self drawCycleAtPoint:faceFeature.leftEyePosition withRadius:faceWidth*0.1]; 68 } 69 70 //右眼 71 if (faceFeature.hasRightEyePosition) { 72 [self drawCycleAtPoint:faceFeature.rightEyePosition withRadius:faceWidth*0.1]; 73 } 74 //嘴巴 75 if (faceFeature.hasMouthPosition) { 76 [self drawCycleAtpoint:faceFeature.mouthPosition withRadius:faceWidth*0.2]; 77 } 78 } 79 80 //获得所有在原图中圈出来脸之后最后的图片,此时的图片是反方向的 81 UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext(); 82 83 //将画布清空,并把放方向的图片用CG的方式画到原图中去(CG画出来的东西都是原来的反方向,并不是个好方法,实际开发中用别的方法) 84 CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height)); 85 CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), tempImage.CGImage); 86 87 //获得重画之后的图片,并结束画布 88 tempImage = UIGraphicsGetImageFromCurrentImageContext(); 89 UIGraphicsEndImageContext(); 90 91 //将最终的图片放到View中去 92 [self.imageView setImage:tempImage]; 93 } 94 95 - (void)drawCycleAtPoint:(CGPoint)point withRadius:(CGFloat)radius { 96 /*画圆*/ 97 //边框圆 98 CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1,0,0,1.0);//画笔线的颜色 99 CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);//线的宽度 100 //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π 弧度 101 // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。 102 CGContextAddArc(UIGraphicsGetCurrentContext(), point.x, point.y, radius, 0, 2*M_PI, 0); //添加一个圆 103 CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathStroke); //绘制路径 104 }
posted on 2017-04-10 18:08 Pierce-lph 阅读(466) 评论(0) 编辑 收藏 举报