国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS原生人臉識別CIDetector使用

2019-11-09 14:38:38
字體:
來源:轉載
供稿:網友

本片博客版權歸黑馬程序員所有:黑馬程序員

蘋果原生人臉識別早在iOS5就已經有了,但是能夠識別的數據及其的少,所以用的人不是很多。

目前做的比較好的人臉識別就是Facebook的face++

人臉識別原理簡介:每一張圖片都是由每一個像素點組成,而每一個像素點中又有對應的顏色值(如RGB),人的面部特征中,不同的五官,顏色值肯定存在差異,而人臉識別技術就是通過對照片中每一個像素的識別進行大量的算法處理,最終得出五官的輪廓

性別、年齡、五官位置等都需要龐大的算法支持*

蘋果原生的人臉識別并不是一個獨立的框架,而是放在<CoreImage>框架中

可見蘋果并沒有在這個領域花精力

Demo效果演示

這里寫圖片描述

代碼 注意坐標的換算,CIFaceFeature計算出來的坐標的坐標系的Y軸與iOS的Y軸是相反的,需要自行處理#import "ViewController.h"@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>@PRoperty (weak, nonatomic) IBOutlet UIImageView *imageView;@property(nonatomic,strong)UIImagePickerController *picker;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}#pragma mark - 相冊- (IBAction)photoButtonClick:(id)sender { if (!self.picker) { //初始化uiimagepickercontroller self.picker = [[UIImagePickerController alloc] init]; //UIImagePickerController是UINavigationControllerDelegate的子類 所以設置代理的時候也要實現navigation的代理 //是否可以編輯 self.picker.allowsEditing = YES; //設置代理 self.picker.delegate = self; //設置來源類型 /**UIImagePickerControllerSourceType UIImagePickerControllerSourceTypePhotoLibrary:進入相冊(橫向排列) UIImagePickerControllerSourceTypeCamera:打開相機(必須要真機) UIImagePickerControllerSourceTypeSavedPhotosAlbum:進入相冊(豎向排列) */ self.picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } //先清除上一次人臉識別到的視圖 for (UIView *view in self.imageView.subviews) { [view removeFromSuperview]; } //彈出相機界面 [self presentViewController:self.picker animated:YES completion:nil];}#pragma mark UIImagePickerControllerDelegate//完成選取- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{ NSLog(@"image:%@",image); self.imageView.image = image; //開始人臉識別 [self beginDetectorFacewithImage:image]; [picker dismissViewControllerAnimated:YES completion:nil];}//取消選取- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil];}#pragma mark -開始人臉識別- (void)beginDetectorFacewithImage:(UIImage *)image{ //1 將UIImage轉換成CIImage CIImage* ciimage = [CIImage imageWithCGImage:image.CGImage]; //縮小圖片,默認照片的圖片像素很高,需要將圖片的大小縮小為我們現實的ImageView的大小,否則會出現識別五官過大的情況 float factor = self.imageView.bounds.size.width/image.size.width; ciimage = [ciimage imageByApplyingTransform:CGAffineTransformMakeScale(factor, factor)]; //2.設置人臉識別精度 NSDictionary* opts = [NSDictionary dictionaryWithObject: CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]; //3.創建人臉探測器 CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:opts]; //4.獲取人臉識別數據 NSArray* features = [detector featuresInImage:ciimage]; //5.分析人臉識別數據 for (CIFaceFeature *faceFeature in features){ //注意坐標的換算,CIFaceFeature計算出來的坐標的坐標系的Y軸與iOS的Y軸是相反的,需要自行處理 // 標出臉部 CGFloat faceWidth = faceFeature.bounds.size.width; UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds]; faceView.frame = CGRectMake(faceView.frame.origin.x, self.imageView.bounds.size.height-faceView.frame.origin.y - faceView.bounds.size.height, faceView.frame.size.width, faceView.frame.size.height); faceView.layer.borderWidth = 1; faceView.layer.borderColor = [[UIColor redColor] CGColor]; [self.imageView addSubview:faceView]; // 標出左眼 if(faceFeature.hasLeftEyePosition) { UIView* leftEyeView = [[UIView alloc] initWithFrame: CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15, self.imageView.bounds.size.height-(faceFeature.leftEyePosition.y-faceWidth*0.15)-faceWidth*0.3, faceWidth*0.3, faceWidth*0.3)]; [leftEyeView setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]];// [leftEyeView setCenter:faceFeature.leftEyePosition]; leftEyeView.layer.cornerRadius = faceWidth*0.15; [self.imageView addSubview:leftEyeView]; } // 標出右眼 if(faceFeature.hasRightEyePosition) { UIView* leftEye = [[UIView alloc] initWithFrame: CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15, self.imageView.bounds.size.height-(faceFeature.rightEyePosition.y-faceWidth*0.15)-faceWidth*0.3, faceWidth*0.3, faceWidth*0.3)]; [leftEye setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]]; leftEye.layer.cornerRadius = faceWidth*0.15; [self.imageView addSubview:leftEye]; } // 標出嘴部 if(faceFeature.hasMouthPosition) { UIView* mouth = [[UIView alloc] initWithFrame: CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2, self.imageView.bounds.size.height-(faceFeature.mouthPosition.y-faceWidth*0.2)-faceWidth*0.4, faceWidth*0.4, faceWidth*0.4)]; [mouth setBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0.3]]; mouth.layer.cornerRadius = faceWidth*0.2; [self.imageView addSubview:mouth]; } }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end從以上代碼可以看出,人臉的數據主要放在CIFaceFeature這一個類中,它的API也比較的少

這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀宁县| 太原市| 峡江县| 邵武市| 城步| 宜春市| 堆龙德庆县| 遵义县| 呼和浩特市| 揭西县| 垫江县| 宿州市| 芜湖县| 南通市| 萨嘎县| 新竹市| 定州市| 民乐县| 綦江县| 柳林县| 得荣县| 陕西省| 牡丹江市| 仁寿县| 西城区| 临汾市| 涞源县| 山阴县| 辽阳县| 章丘市| 老河口市| 当雄县| 合肥市| 太湖县| 修武县| 永丰县| 罗城| 监利县| 呼图壁县| 石楼县| 延长县|