人脸识别

IOS5以上支持,很简单就能做出人脸识别
//放上一张照片
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    _imageView.image = [UIImage imageNamed:@"10_7.jpg"];
    [self.view addSubview:_imageView];
    //一张空白的view,和照片一样大,添加到照片上面
    _resultView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:_resultView];
   //人脸识别出得到的坐标是标准坐标系,我们要把标准的倒过来,和ios的坐标系一致
    [_resultView setTransform:CGAffineTransformMakeScale(1, -1)];
   //创建CIDetector,这个类是用来识别的
    NSDictionary* dic = @{CIDetectorAccuracy:CIDetectorAccuracyHigh};
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace context:[CIContext contextWithOptions:nil] options:dic];
   //识别一张图片CIIamge,并得到结果,可能有多个人,所以是数组
    NSArray* resultArray = [detector featuresInImage:[CIImage imageWithCGImage:_imageView.image.CGImage]];
   //便利数组,每一个人脸识别都是一个CIFaceFeature
    for (CIFaceFeature* feature in resultArray) {
        //人脸的区域
        CGRect rect = feature.bounds;
        UIView* view = [[UIView alloc] initWithFrame:rect];
        view.backgroundColor = [UIColor redColor];
        view.alpha = 0.3;
        [_resultView addSubview:view];
        //如果识别出左眼,我们也给显示出来,右眼也一样
        if (feature.hasLeftEyePosition) {
            UIView* eyeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
            eyeView.backgroundColor = [UIColor blueColor];
            eyeView.center = feature.leftEyePosition;
            [_resultView addSubview:eyeView];
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值