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 }