代码改变世界

基于虹软人脸识别-iOS画框更改及前后摄像头的切换

  码仔很忙  阅读(454)  评论(0编辑  收藏  举报

公司项目使用过程中,为了配合市场需要,需要增加人脸识别+活体检测的功能。并且要求人脸识别的样式接近于主流产品的样式。所以选择了方便快捷的虹软人脸识别SDK。

1、项目逻辑流程图

根据下面的逻辑梳理,可以清晰的了解到需要哪些数据。

在这里插入图片描述

2、虹软SDK配置

1、登录虹软开发者。选择新建应用,填写相关信息之后,点击立即创建。如下,可以看到APP_ID。

在这里插入图片描述

还有一个重要的参数是SDK_Key。点击添加SDK,选择平台、SDK版本、语言、应用。创建新的版本。

在这里插入图片描述

将SDK下载之后,里面有虹软人脸识别demo、人脸识别SDK、开发说明明档等。将SDK导入需要开发的项目里面。然后将demo里面相关的文件添加到项目

在这里插入图片描述

3、主要代码

1、切换前后摄像头功能主要代码。

- (BOOL) setupCaptureSession:(AVCaptureVideoOrientation)videoOrientation isFront:(BOOL)isFront
{
    self.captureSession = [[AVCaptureSession alloc] init];
    
    [self.captureSession beginConfiguration];

    AVCaptureDevice *videoDevice = [self videoDeviceWithPosition:isFront ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack];// 前、后摄像头
    // 创建输入流
    AVCaptureDeviceInput *videoIn = [[AVCaptureDeviceInput alloc] initWithDevice:videoDevice error:nil];
    if ([self.captureSession canAddInput:videoIn])
        [self.captureSession addInput:videoIn];
    // 创建输出流
    AVCaptureVideoDataOutput *videoOut = [[AVCaptureVideoDataOutput alloc] init];
    [videoOut setAlwaysDiscardsLateVideoFrames:YES];
    
#ifdef __OUTPUT_BGRA__
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
#else
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
#endif
    [videoOut setVideoSettings:dic];
    
    dispatch_queue_t videoCaptureQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
    [videoOut setSampleBufferDelegate:self queue:videoCaptureQueue];
    
    if ([self.captureSession canAddOutput:videoOut])
        [self.captureSession addOutput:videoOut];
    videoConnection = [videoOut connectionWithMediaType:AVMediaTypeVideo];
    
    if (videoConnection.supportsVideoMirroring) {
        [videoConnection setVideoMirrored:TRUE];
    }
    
    if ([videoConnection isVideoOrientationSupported]) {
        [videoConnection setVideoOrientation:videoOrientation];
    }
    
    if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
        [self.captureSession setSessionPreset:AVCaptureSessionPreset1280x720];
    }
    
    [self.captureSession commitConfiguration];
    
    return YES;
}
#pragma mark -
- (IBAction)btnChangeCamera:(UIButton *)sender {
    isback =! isback;
    [self stopCaptureSession];
    
    [self setupCaptureSession:(AVCaptureVideoOrientation)[[UIApplication sharedApplication] statusBarOrientation] isFront:isback];
    [self startCaptureSession];
}

  1. 计算人脸框
(CGRect)dataFaceRect2ViewFaceRect:(MRECT)faceRect // 画框
{
    // 获取的图像的宽 高度
     CGFloat faceimgeW = faceRect.right-faceRect.left;
     CGFloat faceimgeH = faceRect.bottom-faceRect.top;
    
    // 视图的位置 大小
    CGRect frameGLView = self.glView.frame;
    
    // 计算后的人脸捕捉位置 大小
    CGRect frameFaceRect = {0};
    frameFaceRect.size.width = CGRectGetWidth(frameGLView)*faceimgeW/imgeWidth;
    frameFaceRect.size.height = CGRectGetHeight(frameGLView)*faceimgeH/imgeHight;
    frameFaceRect.origin.x = CGRectGetWidth(frameGLView)*faceRect.left/imgeWidth;
    frameFaceRect.origin.y = CGRectGetHeight(frameGLView)*faceRect.top/imgeHight;

    return frameFaceRect;
    
}

3、蓝色边框的动效

// angle  默认为0
- (void)startAnimation{
CGAffineTransform endAngle = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
[UIView animateWithDuration:0.03 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
 self.imgFaceC.transform = endAngle;
} completion:^(BOOL finished) {
    self->angle += 10;
    [self startAnimation];                  
}];

}

4、源码地址

完整的功能代码可以从这里下载:https://github.com/cymInSHRelese/ChFacedemo.git

5、最终功能完成

最终呈现的样式如下图所示。
在这里插入图片描述

了解更多人脸识别产品相关内容请到虹软视觉开放平台

编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示