博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios人脸识别
阅读量:3635 次
发布时间:2019-05-21

本文共 2664 字,大约阅读时间需要 8 分钟。

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    UIImage *image = [UIImage imageNamed:@"faces1.png"];

    _inputImageView.image = image;

    

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    

}

- (void)dealloc {

    [_inputImageView release];

    [_outputImageView release];

    [_button release];

    [super dealloc];

}

- (IBAction)detect:(id)sender {

    

    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) {

            UIView* leftEyeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];

            [leftEyeView setCenter:faceFeature.leftEyePosition];

            leftEyeView.layer.borderWidth = 1;

            leftEyeView.layer.borderColor = [UIColor redColor].CGColor;

            [resultView addSubview:leftEyeView];

            [leftEyeView release];

        }

        

        //右眼

        if (faceFeature.hasRightEyePosition) {

            UIView* rightEyeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];

            [rightEyeView setCenter:faceFeature.rightEyePosition];

            rightEyeView.layer.borderWidth = 1;

            rightEyeView.layer.borderColor = [UIColor redColor].CGColor;

            [resultView addSubview:rightEyeView];

            [rightEyeView release];

        }

        

        //嘴巴

        if (faceFeature.hasMouthPosition) {

            UIView* mouthView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 5)];

            [mouthView setCenter:faceFeature.mouthPosition];

            mouthView.layer.borderWidth = 1;

            mouthView.layer.borderColor = [UIColor redColor].CGColor;

            [resultView addSubview:mouthView];

            [mouthView release];

        }

        

    }

    

    [resultView setTransform:CGAffineTransformMakeScale(1, -1)];

    

    [resultView release];

    

    if ([detectResult count] > 0)

    {

        CIImage *faceImage = [image imageByCroppingToRect:[[detectResult objectAtIndex:0] bounds]];

        

        UIImage *face = [UIImage imageWithCGImage:[context createCGImage:faceImage fromRect:faceImage.extent]];

        self.outputImageView.image = face;

        

        [self.button setTitle:[NSString stringWithFormat:@"识别 人脸数 %i",

                               [detectResult count]] forState:UIControlStateNormal];

    }

    

}

@end

转载地址:http://afrun.baihongyu.com/

你可能感兴趣的文章
python关于print中数据传输的用法
查看>>
sublime text3的快捷键总结
查看>>
gdal学习笔记2-数据读写
查看>>
python中动态生成变量名及赋值
查看>>
python识别数据结构
查看>>
python bisect序列二分法详解
查看>>
python学习笔记字典排序,
查看>>
python内置类 set
查看>>
python getatrra()
查看>>
thinkpython2的扑克牌系列练习最终解读
查看>>
matlab复色光夫琅禾费衍射
查看>>
Java中线程的基本操作以及Thread和Runnable两种实现的比较
查看>>
MongoDbRepository的常用AP操作和易错点
查看>>
MongDBRepository和MongDBOperator和MongTemplate的方法比较
查看>>
IntelliJ IDEA中关于Maven构建复杂的聚合工程的管理和打包问题
查看>>
错误记录关于Model 的Not a managed type: class,无法找到Model
查看>>
关于JPA中Specification接口的问题,记录一下
查看>>
IntelliJ IDEA中GIT,已经 commit and push成功,但并未 push 到远程库的问题
查看>>
关于光盘刻录,重洗的一些知识
查看>>
default_Keyword
查看>>