iOS 原生CoreImage人脸识别

首先定义属性:

@property (strong,nonatomic)UIImage *ima;

@property (strong,nonatomic)UIImageView *iamgeView;


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

    self.iamgeView = [[UIImageView alloc]initWithImage:self.ima];

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

    [self.view addSubview:self.iamgeView];

    

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

    NSDictionary  *opts = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey: CIDetectorAccuracy];

    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:opts];

    

    //得到面部数据

    NSArray* features = [detector featuresInImage:image];

    for (CIFaceFeature *f in 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 = [[UILabel alloc]initWithFrame:CGRectMake(f.leftEyePosition.x, self.ima.size.height-f.leftEyePosition.y, 5, 5)];

            label.text = @"";

            label.textColor = [UIColor redColor];

            [self.iamgeView addSubview:label];

        }

        if(f.hasRightEyePosition) {

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

            //添加一个标记

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

            label.text = @"";

            label.textColor = [UIColor redColor];

            [self.iamgeView addSubview:label];

        }

       if(f.hasMouthPosition)

        {

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

           //添加一个标记

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

            label.text = @"";

            label.textColor = [UIColor redColor];

            [self.iamgeView addSubview:label];

        }

        if (f.hasSmile) {

            NSLog(@"笑了");

        }

        if (f.hasFaceAngle) {

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

        }

    }

    

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值