基于虹软人脸识别-iOS画框更改及前后摄像头的切换
2021-08-09 17:23 码仔很忙 阅读(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];
}
- 计算人脸框
(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、最终功能完成
最终呈现的样式如下图所示。
了解更多人脸识别产品相关内容请到虹软视觉开放平台哦
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~