人脸识别(简单代码)

#import "ViewController.h"

#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width

#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height


@interface ViewController ()

{

    //原来的图像

    UIImageView * _originleImagView;

    //识别出来的图像

    UIImageView * newfaceImageView;

    

}

@property(nonatomic,assign)NSInteger featureCount;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    _featureCount = 0;

    UIImage * oldFaceImg = [UIImage imageNamed:@"faces1"];

    _originleImagView = [[UIImageView allocinitWithFrame:CGRectMake(020, oldFaceImg.size.width, oldFaceImg.size.height)];

    [_originleImagView setImage:oldFaceImg];

    _originleImagView.center = CGPointMake(SCREENWIDTH/2, oldFaceImg.size

                                           .height/2+30);

    

    [self.view addSubview:_originleImagView];

    

    UIButton * featureButton = [UIButton buttonWithType:UIButtonTypeCustom];

    featureButton.frame = CGRectMake(0012050);

    featureButton.center = CGPointMake(SCREENWIDTH/2, oldFaceImg.size.height + 30 + 30);

    [featureButton setTitle:[NSString stringWithFormat:@"识别人脸%ld",(long)_featureCount]forState:UIControlStateNormal];

    [featureButton setTitle:@"识别的图像为" forState:UIControlStateSelected];

    featureButton.backgroundColor = [UIColor grayColor];

    [featureButton setTitleColor:[UIColor whiteColorforState:UIControlStateNormal];

    [featureButton setTitleColor:[UIColor whiteColorforState:UIControlStateSelected];

    [featureButton addTarget:self action:@selector(faceFetureButtonAction:)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:featureButton];

    

    newfaceImageView = [[UIImageView allocinitWithFrame:CGRectMake(SCREENWIDTH/2, oldFaceImg.size.height + 3060 , 5050)];

    [self.view addSubview:newfaceImageView];

    

}

-(void)faceFetureButtonAction:(UIButton *)button

{

    //核心代码进行人脸识别

    CIContext * context = [CIContext contextWithOptions:nil];

    UIImage * imageInput = [_originleImagView image];

    CIImage * imageChange = [CIImage imageWithCGImage:imageInput.CGImage];

    // 设置识别的参数

    NSDictionary * param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHighforKey:CIDetectorAccuracy];

    //声明CIDetector

    CIDetector * faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:param];

    //取得识别结果

    NSArray * detectResult = [faceDetector featuresInImage:imageChange];

    UIView * resultView = [[UIView allocinitWithFrame:_originleImagView.frame];

    [self.view addSubview:resultView];

    for (CIFaceFeature * faceFeature in detectResult) {

        //脸部

        UIView * faceView = [[UIView allocinitWithFrame:faceFeature.bounds];

        faceView.layer.borderWidth = 1;

        faceView.layer.borderColor = [UIColor orangeColor].CGColor;

        [resultView addSubview:faceView];

        

        //左眼

        if (faceFeature.hasLeftEyePosition) {

            UIView * leftEyeView = [[UIView allocinitWithFrame:CGRectMake(0055)];

            [leftEyeView setCenter:faceFeature.leftEyePosition];

            leftEyeView.layer.borderWidth = 1;

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

            [resultView addSubview:leftEyeView];

        }

        

        //右眼

        if (faceFeature.hasRightEyePosition) {

            UIView * rightEyeView = [[UIView allocinitWithFrame:CGRectMake(0055)];

            [rightEyeView setCenter:faceFeature.rightEyePosition];

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

            rightEyeView.layer.borderWidth = 1;

            [resultView addSubview:rightEyeView];

        }

        

        //嘴巴

        if (faceFeature.hasMouthPosition) {

            UIView * mouthView = [[UIView allocinitWithFrame:CGRectMake(00105)];

            [mouthView setCenter:faceFeature.rightEyePosition];

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

            mouthView.layer.borderWidth = 1;

            [resultView addSubview:mouthView];

        }

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

        if ([detectResult count] > 0) {

            {

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

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

                newfaceImageView.image = face;

                newfaceImageView.frame = CGRectMake(SCREENWIDTH/2-403003060 ,80 , 80);

                _featureCount +=1;

                [button setTitle:[NSString stringWithFormat:@"识别次数%ld",(long)_featureCount]forState:UIControlStateNormal];

                

            }

        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值