人臉識別過程一般分為以下3個步驟:
1.首先建立人臉的面紋數據庫.可以通過照相機或攝像機采集人臉的面相圖片,將這些面相圖片生成面紋編碼保存到數據庫中.
2.獲取當前人臉面相圖片.即通過照相機或攝像機采集人臉的面相圖片,將當前的面相文件生成面紋編碼
3.用當前的面紋編碼與數據庫中的面紋編碼進行對比
在iOS5之後提供人臉識別的API,通過提供的CIDetector類可以進行人臉特征識別,CIDetector是CoreImage框架中的一個特征識別濾鏡,CIDetector主要用於人臉特征識別.通過它還可以獲得眼睛和嘴的特征信息.但是CIDetector並不包括面紋編碼提取,面紋編碼處理還需要更為復雜的算法處理.也就是說使用CIDetector類可以找到一張圖片中的人臉,但這張臉是誰,CIDetector無法判斷,這需要有一個面紋數據庫,把當前人臉提取面紋編碼然後與數據庫進行對比.(openCV,FACE.COM)
CIContext *context = [CIContext contextWithOptions:nil];
UIImage *imageInput = [_inputImageView image];
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];
UIView *resultView = [[UIView alloc] initWithFrame:_inputImageView.frame];
[self.view addSubview:resultView];
for(CIFaceFeature* faceFeature in detectResult) {
//臉部
UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
faceView.layer.borderWidth = 1;
faceView.layer.borderColor = [UIColor orangeColor].CGColor;
[resultView addSubview:faceView];
[faceView release];
//左眼
if (faceFeature.hasLeftEyePosition) {
}
//右眼
if (faceFeature.hasRightEyePosition) {
}
//嘴巴
if (faceFeature.hasMouthPosition) {
}
}
[resultView setTransform:CGAffineTransformMakeScale(1, -1)];
[resultView release];