zoukankan      html  css  js  c++  java
  • 基于虹软人脸识别-iOS画框更改及前后摄像头的切换

    公司项目使用过程中,为了配合市场需要,需要增加人脸识别+活体检测的功能。并且要求人脸识别的样式接近于主流产品的样式。所以选择了方便快捷的虹软人脸识别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、最终功能完成

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

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

  • 相关阅读:
    ABP WebApi的请求类型
    对服务器的文件或文件夹压缩
    VS运行遇到的那些坑
    asp.net core Api集成Swagger
    通过文件路径获取文件名
    读取服务器或者本地的文件夹下面的文件
    计算出 3 至 1000 范围内最大的十个素数,放入数组中,并计算出其累加和。
    LINUX 无法登入系统(2017-1-16)
    zynq里面的AXI总线(2017-1-11)
    学习随笔(2017-1-10)
  • 原文地址:https://www.cnblogs.com/ccLqqy/p/15119669.html
Copyright © 2011-2022 走看看