人脸识别 iOS开发

来源:互联网 发布:什么理财软件安全可靠 编辑:程序博客网 时间:2024/06/09 19:34
首先定义属性:

@property (strong,nonatomic)UIImage *ima;

@property (strong,nonatomic)UIImageView *iamgeView;


self.ima= [UIImageimageNamed:@"123.png"];

    self.iamgeView = [[UIImageViewalloc]initWithImage:self.ima];

    self.iamgeView.frame =CGRectMake(0,0self.ima.size.widthself.ima.size.height);

    [self.viewaddSubview:self.iamgeView];

    

    CIImage *image = [CIImageimageWithCGImage:self.ima.CGImage];

    NSDictionary  *opts = [NSDictionarydictionaryWithObject:CIDetectorAccuracyHighforKey:CIDetectorAccuracy];

    CIDetector* detector = [CIDetectordetectorOfType:CIDetectorTypeFacecontext:niloptions:opts];

    

    //得到面部数据

    NSArray* features = [detector featuresInImage:image];

    for (CIFaceFeature *fin features)

    {

        CGRect aRect = f.bounds;

        NSLog(@"%f, %f, %f, %f", aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);

       

        //眼睛和嘴的位置

        if(f.hasLeftEyePosition) {

            NSLog(@"Left eye %g %g\n", f.leftEyePosition.x, f.leftEyePosition.y);

            //添加一个标记


            UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(f.leftEyePosition.x,self.ima.size.height-f.leftEyePosition.y,5,5)];

            label.text = @"";

            label.textColor = [UIColorredColor];

            [self.iamgeViewaddSubview:label];

        }

        if(f.hasRightEyePosition) {

            NSLog(@"Right eye %g %g\n", f.rightEyePosition.x, f.rightEyePosition.y);

            //添加一个标记

            UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(f.rightEyePosition.x,self.ima.size.height-f.rightEyePosition.y,5,5)];

            label.text = @"";

            label.textColor = [UIColorredColor];

            [self.iamgeViewaddSubview:label];

        }

       if(f.hasMouthPosition)

        {

           NSLog(@"Mouth %g %g\n", f.mouthPosition.x, f.mouthPosition.y);

           //添加一个标记

            UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(f.mouthPosition.x,self.ima.size.height-f.mouthPosition.y,5,5)];

            label.text = @"";

            label.textColor = [UIColorredColor];

            [self.iamgeViewaddSubview:label];

        }

        if (f.hasSmile) {

            NSLog(@"笑了");

        }

        if (f.hasFaceAngle) {

            NSLog(@"FaceAngle %f ", f.faceAngle );

        }

    }

    

其中眼睛和嘴巴的位置,是在照片image上的位置而不是imageView,照片的scale也会影响位置的判断。(本文中scale为1)其中y值以照片底部为基准的(平时我们定义的frame是以顶部为基准),所以为获取正确的y位置,需要用照片的高度,减去获取的y值

0 0
原创粉丝点击