ios人脸识别


- (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

iOS上进行人脸识别并抠取人脸的方法可以使用Core Image框架中的CIDetector类。通过以下代码可以实现人脸识别和抠取人脸的功能: ```swift func detectFace(withImage image: UIImage) { // 将图像转为CIImage,使用Core Image需要使用CIImage guard let personCIImg = CIImage(image: image) else { return } // 设置识别精度 let opts: \[String: Any\] = \[CIDetectorAccuracy: CIDetectorAccuracyHigh\] // 初始化识别器 let detector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: opts) let result: \[CIFaceFeature\] = (detector?.features(in: personCIImg, options: opts) as? \[CIFaceFeature\])! if result.count > 0 { for face in result { let faceBox = UIView(frame: face.bounds) // 画一个红框画出面部位置 faceBox.layer.borderWidth = 3 faceBox.layer.borderColor = UIColor.red.cgColor faceBox.backgroundColor = UIColor.clear // 添加红框到图片上 imgView.addSubview(faceBox) print("面部坐标------> %d ", faceBox.frame) } } } ``` 这段代码会将传入的UIImage对象转换为CIImage对象,然后使用CIDetector进行人脸识别。识别到的人脸会通过在UIImageView上添加红色边框的方式进行标记。你可以根据需要对这段代码进行修改和扩展,以满足你的具体需求。 #### 引用[.reference_title] - *1* *2* [ios人脸识别_适用于Android和iOS的10种最佳人脸识别应用程序](https://blog.csdn.net/cumian8165/article/details/108160585)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [iOS人脸识别Demo](https://blog.csdn.net/kangpengpeng1/article/details/79197201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值