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

首頁 > 系統(tǒng) > iOS > 正文

iOS原生人臉識別CIDetector使用

2019-11-09 13:52:48
字體:
供稿:網(wǎng)友

本片博客版權(quán)歸黑馬程序員所有:黑馬程序員

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

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

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

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

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

可見蘋果并沒有在這個領(lǐng)域花精力

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的子類 所以設(shè)置代理的時候也要實現(xiàn)navigation的代理 //是否可以編輯 self.picker.allowsEditing = YES; //設(shè)置代理 self.picker.delegate = self; //設(shè)置來源類型 /**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轉(zhuǎn)換成CIImage CIImage* ciimage = [CIImage imageWithCGImage:image.CGImage]; //縮小圖片,默認照片的圖片像素很高,需要將圖片的大小縮小為我們現(xiàn)實的ImageView的大小,否則會出現(xiàn)識別五官過大的情況 float factor = self.imageView.bounds.size.width/image.size.width; ciimage = [ciimage imageByApplyingTransform:CGAffineTransformMakeScale(factor, factor)]; //2.設(shè)置人臉識別精度 NSDictionary* opts = [NSDictionary dictionaryWithObject: CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]; //3.創(chuàng)建人臉探測器 CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:opts]; //4.獲取人臉識別數(shù)據(jù) NSArray* features = [detector featuresInImage:ciimage]; //5.分析人臉識別數(shù)據(jù) 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從以上代碼可以看出,人臉的數(shù)據(jù)主要放在CIFaceFeature這一個類中,它的API也比較的少

這里寫圖片描述


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大理市| 无为县| 东城区| 富顺县| 金秀| 罗定市| 连江县| 崇礼县| 珠海市| 堆龙德庆县| 临夏县| 孟津县| 旌德县| 怀远县| 阜平县| 三台县| 浦东新区| 武强县| 德江县| 安丘市| 湾仔区| 昭通市| 庄河市| 云安县| 天等县| 墨江| 汶上县| 扶沟县| 普定县| 罗定市| 平顶山市| 张家界市| 加查县| 沐川县| 博爱县| 本溪市| 平陆县| 芜湖县| 五大连池市| 涞源县| 黄平县|