首先了解一下iOS里的CoreImage,CoreImage是一種圖像處理和分析技術,旨在為靜態和視頻圖像提供近實時處理。它使用GPU或CPU渲染路徑,對Core Graphics,Core Video和Image I / O框架中的圖像數據類型進行操作。Core Image通過提供易於使用的應用程序編程接口(API)隱藏了低級圖形處理的細節。您不需要了解OpenGL,OpenGL ES或Metal的細節來充分利用GPU的強大功能,也無需了解Grand Central Dispatch(GCD)以獲得多核處理的優勢。Core Image為您處理細節。
Core Image框架提供:
-
訪問內置圖像處理過濾器
-
特征檢測功能
-
支持自動圖像增強
-
將多個過濾器鏈接在一起以創建自定義效果的功能
-
支持創建在GPU上運行的自定義過濾器
-
基於反饋的圖像處理功能
顯然這個框架十分強悍!文檔上也說了CoreImage所做的是人臉檢測而非人臉識別,寫了一個小demo學習一下基於CIDetector的人臉檢測。
//檢測的圖片,注意調整一下尺寸否則檢測出來的位置對不上 UIImageView * imgView = [[UIImageView alloc] initWithFrame:self.view.bounds]; [imgView setImage:[UIImage imageNamed:@"myImage"] ]; [self.view addSubview:imgView]; //檢測出來的結果位置Y軸和圖像本身的Y軸是相反的,這里我就拿一個view裝載結果將它反轉就能看出效果了 UIView * view = [[UIView alloc] initWithFrame:self.view.bounds]; [view setTransform:CGAffineTransformMakeScale(1, -1)]; [self.view addSubview:view]; //CoreImage用於處理或生成的圖像類型 CIImage * myImage = [[CIImage alloc] initWithCGImage:imgView.image.CGImage options:nil]; //用於渲染圖像處理結果和執行圖像分析的評估上下文 CIContext * context = [CIContext context]; //檢測器的設置 NSDictionary * opts = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh , //檢測精度 // CIDetectorEyeBlink : [NSNumber numberWithBool:true], //眼睛是否閉着 // CIDetectorSmile : [NSNumber numberWithBool:true] //是否微笑 }; //檢測器 CIDetector * detector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:opts]; //CoreImage返回一個CIFeature對象數組,每個對象代表圖像中的一個面 NSArray * features = [detector featuresInImage:myImage options:opts]; //遍歷檢測結果 for (CIFaceFeature * f in features) { NSLog(@"%@,%@,左眼%@,右眼%@", NSStringFromCGRect(f.bounds),f.hasSmile?@"笑":@"沒笑",f.leftEyeClosed?@"閉着的":@"瞪着你",f.rightEyeClosed?@"閉着的":@"瞪着你"); UIView *faceView = [[UIView alloc] initWithFrame:f.bounds]; faceView.layer.borderColor = [UIColor redColor].CGColor; faceView.layer.borderWidth = 1; [view addSubview:faceView]; if (f.hasLeftEyePosition) { NSLog(@"左眼 %g %g", f.leftEyePosition.x, f.leftEyePosition.y); UIView * leftEyeView = [[UIView alloc] initWithFrame:CGRectMake(f.leftEyePosition.x, f.leftEyePosition.y, 5, 5)]; leftEyeView.layer.borderColor = [UIColor yellowColor].CGColor; leftEyeView.layer.borderWidth = 5; [view addSubview:leftEyeView]; } if (f.hasRightEyePosition) { NSLog(@"右眼 %g %g", f.rightEyePosition.x, f.rightEyePosition.y); UIView * rightEyeView = [[UIView alloc] initWithFrame:CGRectMake(f.rightEyePosition.x, f.rightEyePosition.y, 5, 5)]; rightEyeView.layer.borderColor = [UIColor yellowColor].CGColor; rightEyeView.layer.borderWidth = 5; [view addSubview:rightEyeView]; } if (f.hasMouthPosition) { NSLog(@"嘴 %g %g", f.mouthPosition.x, f.mouthPosition.y); UIView * mouthView = [[UIView alloc] initWithFrame:CGRectMake(f.mouthPosition.x, f.mouthPosition.y, 5, 5)]; mouthView.layer.borderColor = [UIColor greenColor].CGColor; mouthView.layer.borderWidth = 5; [view addSubview:mouthView]; } }
CIImage是CoreImage用來處理圖像的類型可以配合CIContext、CIFilter、CIVector、CIColor等來處理圖像過濾顏色等。
檢測器的設置
NSDictionary * opts = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh //檢測精度 };
CIDetectorImageOrientation
用於要檢測其功能的圖像的顯示方向的選項。
CIDetectorEyeBlink
Core Image是否執行其他處理以識別檢測到的面部中的閉眼的選項。
CIDetectorSmile
Core Image是否執行其他處理以識別檢測到的面部中的微笑的選項。
CIDetectorFocalLength
識別用於捕獲要由檢測器處理的圖像的像素的焦距的選項。
CIDetectorAspectRatio
一個選項,指定要搜索的矩形的寬高比(寬度除以高度)。
CIDetectorReturnSubFeatures
一個選項,指定是否返回檢測到的要素的組件的要素信息。
還有其他配置。
創建一個CIDetector用於檢測圖像
CIDetector * detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:context
options:opts];
CIDetectorTypeFace
用於搜索靜止圖像或視頻中的面部,返回提供有關檢測到的面部信息的對象。
CIDetectorTypeRectangle
用於搜索靜止圖像或視頻中的矩形區域,返回提供有關檢測到的區域的信息的對象。
CIDetectorTypeQRCode
用於在靜止圖像或視頻中搜索快速響應代碼(一種2D條形碼),返回提供有關檢測到的條形碼信息的對象。
CIDetectorTypeText
用於搜索靜止圖像或視頻中的文本,返回提供有關檢測到的區域的信息的對象。
用CIFaceFeature接收CIDetector的檢測結果
@property (readonly, assign) CGRect bounds; //面部的位置和尺寸 @property (readonly, assign) BOOL hasLeftEyePosition; @property (readonly, assign) CGPoint leftEyePosition; //左眼的位置 @property (readonly, assign) BOOL hasRightEyePosition; @property (readonly, assign) CGPoint rightEyePosition; //右眼的位置 @property (readonly, assign) BOOL hasMouthPosition; @property (readonly, assign) CGPoint mouthPosition; //嘴巴的位置 /** 視頻跟蹤ID */ @property (readonly, assign) BOOL hasTrackingID; @property (readonly, assign) int trackingID; @property (readonly, assign) BOOL hasTrackingFrameCount; @property (readonly, assign) int trackingFrameCount; @property (readonly, assign) BOOL hasFaceAngle;// @property (readonly, assign) float faceAngle;// @property (readonly, assign) BOOL hasSmile;//是否微笑 @property (readonly, assign) BOOL leftEyeClosed;//左眼是否閉着 @property (readonly, assign) BOOL rightEyeClosed;//右眼是否閉着
很榮幸請來祖哥(網上下的圖片)做模特測試了一下demo!!測試結果一般,有時候檢測不到臉,檢測到的臉部如果比較小像素比較低檢測的微笑、眼睛是否閉着幾乎都是錯的!