基于虹软人脸识别-iOS画框更改及前后摄像头的切换
2021-08-09 17:23 码仔很忙 阅读(438) 评论(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];
}
- 计算人脸框
(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、最终功能完成
最终呈现的样式如下图所示。
了解更多人脸识别产品相关内容请到虹软视觉开放平台哦