飞道的博客

程序员为女友开发网络直播利器,手势切换视频背景

262人阅读  评论(0)

有一个程序员男友是一种什么样的体验?
知乎上这道题里堆满了狗粮,我厂阿强的女友阿珍,也贡献了一个有技术含量的答案——因为程序员男友,作为人民教师的阿珍,荣登学校最受欢迎直播网课榜。
事情是这样的,去年疫情期间,学校推出停课不停学的直播课程,于是新晋主播阿珍,为直播课绞尽脑汁:怎样让宅家小朋友“身临其境”感受世界地理历史知识,轻松“抵达”东京、纽约和巴黎,览遍故宫、叶宫、卢浮宫?

就在阿珍百思不得其解之时,阿强举手表示:这题我会!

程序员阿强仔细分析了女友的需求,为她量身打造一款“身临其境”的线上课堂App,这款App,通过手势就可以快速自如地切换直播背景,并且逼真到每个像素点都可标签化,阿珍的一根头发丝都可以完整保留!


效果示例

实现原理

通过手势在直播中更换背景是通过机器学习图像分割手部关键点识别两大技术来实现的。

图像分割功能可以将图片中相同元素(如人体、植物、天空等)的部分从图像整体中“分割”出来,当前支持人像、天空、植物、美食、猫狗、花朵、水面、沙面、建筑、山峰、其他等11大类元素的分割,支持静态图片分割和动态视频流分割。

手部关键点识别功能支持识别21个手部关键点(包括每个手指指尖、关节点,以及手腕点),并返回关键点的位置数据。手势识别能力能够检测并返回图片或视频中的所有手部矩形框位置,以及手势的类别和置信度,支持识别14种手势,包括点赞、差评、OK、握拳、单手比心、数 字1-9。手部关键点识别能力与手势识别能力都支持静态图片识别、实时视频流识别。

开发步骤

1.添加HUAWEI agcp插件以及Maven代码库。


  
  1. buildscript {
  2. repositories {
  3. google()
  4. jcenter()
  5. maven {url 'https://developer.huawei.com/repo/'}
  6. }
  7. dependencies {
  8. ...
  9. classpath 'com.huawei.agconnect:agcp:1.4.1.300'
  10. }
  11. }
  12. allprojects {
  13. repositories {
  14. google()
  15. jcenter()
  16. maven {url 'https://developer.huawei.com/repo/'}
  17. }
  18. }

2.Full SDK方式集成


  
  1. dependencies{
  2. // 引入图像分割基础SDK
  3. implementation 'com.huawei.hms:ml-computer-vision-segmentation:2.0.4.300'
  4. // 引入多类别分割模型包
  5. implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-multiclass-model:2.0.4.300'
  6. // 引入人像分割模型包
  7. implementation 'com.huawei.hms:ml-computer-vision-image-segmentation-body-model:2.0.4.300'
  8. // 引入手势识别基础SDK
  9. implementation 'com.huawei.hms:ml-computer-vision-handkeypoint:2.0.4.300'
  10. // 引入手部关键点检测模型包
  11. implementation 'com.huawei.hms:ml-computer-vision-handkeypoint-model:2.0.4.300'
  12. }

3.在文件头添加配置。

在apply plugin: 'com.android.application'后添加apply plugin: 'com.huawei.agconnect'

4.自动更新机器学习模型

在AndroidManifest.xml文件中添加


  
  1. <manifest
  2. ...
  3. < meta-data
  4. android:name= "com.huawei.hms.ml.DEPENDENCY"
  5. android:value= "imgseg,handkeypoint" />
  6. ...
  7. </manifest>

5.创建图像分割检测器。


  
  1. MLImageSegmentationAnalyzer imageSegmentationAnalyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer(); //图像分割分析器
  2. MLHandKeypointAnalyzer handKeypointAnalyzer = MLHandKeypointAnalyzerFactory.getInstance().getHandKeypointAnalyzer(); //手势识别分析器
  3. MLCompositeAnalyzer analyzer = new MLCompositeAnalyzer.Creator()
  4. . add(imageSegmentationAnalyzer)
  5. . add(handKeypointAnalyzer)
  6. .create();

6.创建识别结果处理类


  
  1. public class ImageSegmentAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLImageSegmentation> {
  2. @Override
  3. public void transactResult(MLAnalyzer.Result<MLImageSegmentation> results) {
  4. SparseArray<MLImageSegmentation> items = results.getAnalyseList();
  5. // 开发者根据需要处理识别结果,需要注意,这里只对检测结果进行处理。
  6. // 不可调用ML Kit提供的其他检测相关接口。
  7. }
  8. @Override
  9. public void destroy() {
  10. // 检测结束回调方法,用于释放资源等。
  11. }
  12. }
  13. public class HandKeypointTransactor implements MLAnalyzer.MLTransactor<List<MLHandKeypoints>> {
  14. @Override
  15. public void transactResult(MLAnalyzer.Result<List<MLHandKeypoints>> results) {
  16. SparseArray<List<MLHandKeypoints>> analyseList = results.getAnalyseList();
  17. // 开发者根据需要处理识别结果,需要注意,这里只对检测结果进行处理。
  18. // 不可调用ML Kit提供的其他检测相关接口。
  19. }
  20. @Override
  21. public void destroy() {
  22. // 检测结束回调方法,用于释放资源等。
  23. }
  24. }

7.设置识别结果处理器,实现分析器与结果处理器的绑定


  
  1. imageSegmentationAnalyzer.setTransactor( new ImageSegmentAnalyzerTransactor());
  2. handKeypointAnalyzer.setTransactor( new HandKeypointTransactor());

8.创建LensEngine


  
  1. Context context = this.getApplicationContext();
  2. LensEngine lensEngine = new LensEngine.Creator(context,analyzer)
  3. // 设置摄像头前后置模式,LensEngine.BACK_LENS为后置,LensEngine.FRONT_LENS为前置。
  4. .setLensType(LensEngine.FRONT_LENS)
  5. .applyDisplayDimension( 1280, 720)
  6. .applyFps( 20.0f)
  7. .enableAutomaticFocus( true)
  8. .create();

 

9.启动相机,读取视频流,进行识别


  
  1. // 请自行实现SurfaceView控件的其他逻辑。
  2. SurfaceView mSurfaceView = new SurfaceView( this);
  3. try {
  4. lensEngine.run(mSurfaceView.getHolder());
  5. } catch (IOException e) {
  6. // 异常处理逻辑。
  7. }

10.检测完成,停止分析器,释放检测资源


  
  1. if (analyzer != null) {
  2. try {
  3. analyzer .stop();
  4. } catch (IOException e) {
  5. // 异常处理。
  6. }
  7. }
  8. if (lensEngine != null) {
  9. lensEngine .release();
  10. }

>>访问机器学习服务开源仓库地址:GitHub Gitee

>>访问华为开发者联盟官网,了解更多相关内容

>>获取开发指导文档

>>华为移动服务开源仓库地址:GitHubGitee

关注我们,第一时间了解华为移动服务最新技术资讯~


转载:https://blog.csdn.net/HUAWEI_HMSCore/article/details/116048428
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场