「有一个程序员男友是一种什么样的体验?」
知乎上这道题里堆满了狗粮,我厂阿强的女友阿珍,也贡献了一个有技术含量的答案——因为程序员男友,作为人民教师的阿珍,荣登学校最受欢迎直播网课榜。
事情是这样的,去年疫情期间,学校推出停课不停学的直播课程,于是新晋主播阿珍,为直播课绞尽脑汁:怎样让宅家小朋友“身临其境”感受世界地理历史知识,轻松“抵达”东京、纽约和巴黎,览遍故宫、叶宫、卢浮宫?
就在阿珍百思不得其解之时,阿强举手表示:这题我会!
程序员阿强仔细分析了女友的需求,为她量身打造一款“身临其境”的线上课堂App,这款App,通过手势就可以快速自如地切换直播背景,并且逼真到每个像素点都可标签化,阿珍的一根头发丝都可以完整保留!
效果示例
实现原理
通过手势在直播中更换背景是通过机器学习图像分割和手部关键点识别两大技术来实现的。
图像分割功能可以将图片中相同元素(如人体、植物、天空等)的部分从图像整体中“分割”出来,当前支持人像、天空、植物、美食、猫狗、花朵、水面、沙面、建筑、山峰、其他等11大类元素的分割,支持静态图片分割和动态视频流分割。
手部关键点识别功能支持识别21个手部关键点(包括每个手指指尖、关节点,以及手腕点),并返回关键点的位置数据。手势识别能力能够检测并返回图片或视频中的所有手部矩形框位置,以及手势的类别和置信度,支持识别14种手势,包括点赞、差评、OK、握拳、单手比心、数 字1-9。手部关键点识别能力与手势识别能力都支持静态图片识别、实时视频流识别。
开发步骤
1.添加HUAWEI agcp插件以及Maven代码库。
-
buildscript
{
-
repositories
{
-
google()
-
jcenter()
-
maven
{url 'https://developer.huawei.com/repo/'}
-
}
-
dependencies
{
-
...
-
classpath
'com.huawei.agconnect:agcp:1.4.1.300'
-
}
-
}
-
-
allprojects
{
-
repositories
{
-
google()
-
jcenter()
-
maven
{url 'https://developer.huawei.com/repo/'}
-
}
-
}
2.Full SDK方式集成
-
dependencies{
-
// 引入图像分割基础SDK
-
implementation
'com.huawei.hms:ml-computer-vision-segmentation:2.0.4.300'
-
// 引入多类别分割模型包
-
implementation
'com.huawei.hms:ml-computer-vision-image-segmentation-multiclass-model:2.0.4.300'
-
// 引入人像分割模型包
-
implementation
'com.huawei.hms:ml-computer-vision-image-segmentation-body-model:2.0.4.300'
-
// 引入手势识别基础SDK
-
implementation
'com.huawei.hms:ml-computer-vision-handkeypoint:2.0.4.300'
-
// 引入手部关键点检测模型包
-
implementation
'com.huawei.hms:ml-computer-vision-handkeypoint-model:2.0.4.300'
-
}
3.在文件头添加配置。
在apply plugin: 'com.android.application'后添加apply plugin: 'com.huawei.agconnect'
4.自动更新机器学习模型
在AndroidManifest.xml文件中添加
-
<manifest
-
...
-
<
meta-data
-
android:name=
"com.huawei.hms.ml.DEPENDENCY"
-
android:value=
"imgseg,handkeypoint" />
-
...
-
</manifest>
5.创建图像分割检测器。
-
MLImageSegmentationAnalyzer imageSegmentationAnalyzer = MLAnalyzerFactory.getInstance().getImageSegmentationAnalyzer();
//图像分割分析器
-
MLHandKeypointAnalyzer handKeypointAnalyzer = MLHandKeypointAnalyzerFactory.getInstance().getHandKeypointAnalyzer();
//手势识别分析器
-
-
MLCompositeAnalyzer analyzer =
new MLCompositeAnalyzer.Creator()
-
.
add(imageSegmentationAnalyzer)
-
.
add(handKeypointAnalyzer)
-
.create();
6.创建识别结果处理类
-
public
class ImageSegmentAnalyzerTransactor implements MLAnalyzer.MLTransactor<MLImageSegmentation> {
-
@Override
-
public void transactResult(MLAnalyzer.Result<MLImageSegmentation> results) {
-
SparseArray<MLImageSegmentation> items = results.getAnalyseList();
-
// 开发者根据需要处理识别结果,需要注意,这里只对检测结果进行处理。
-
// 不可调用ML Kit提供的其他检测相关接口。
-
}
-
@Override
-
public void destroy() {
-
// 检测结束回调方法,用于释放资源等。
-
}
-
}
-
-
public
class HandKeypointTransactor implements MLAnalyzer.MLTransactor<List<MLHandKeypoints>> {
-
@Override
-
public void transactResult(MLAnalyzer.Result<List<MLHandKeypoints>> results) {
-
SparseArray<List<MLHandKeypoints>> analyseList = results.getAnalyseList();
-
// 开发者根据需要处理识别结果,需要注意,这里只对检测结果进行处理。
-
// 不可调用ML Kit提供的其他检测相关接口。
-
}
-
@Override
-
public void destroy() {
-
// 检测结束回调方法,用于释放资源等。
-
}
-
}
7.设置识别结果处理器,实现分析器与结果处理器的绑定
-
imageSegmentationAnalyzer.setTransactor(
new ImageSegmentAnalyzerTransactor());
-
handKeypointAnalyzer.setTransactor(
new HandKeypointTransactor());
8.创建LensEngine
-
Context context =
this.getApplicationContext();
-
LensEngine lensEngine = new LensEngine.Creator(context,analyzer)
-
// 设置摄像头前后置模式,LensEngine.BACK_LENS为后置,LensEngine.FRONT_LENS为前置。
-
.setLensType(LensEngine.FRONT_LENS)
-
.applyDisplayDimension(
1280,
720)
-
.applyFps(
20.0f)
-
.enableAutomaticFocus(
true)
-
.create();
9.启动相机,读取视频流,进行识别
-
// 请自行实现SurfaceView控件的其他逻辑。
-
SurfaceView mSurfaceView = new SurfaceView(
this);
-
try {
-
lensEngine.run(mSurfaceView.getHolder());
-
}
catch (IOException e) {
-
// 异常处理逻辑。
-
}
10.检测完成,停止分析器,释放检测资源
-
if (analyzer != null) {
-
try {
-
analyzer
.stop();
-
}
catch (IOException e) {
-
// 异常处理。
-
}
-
}
-
if (lensEngine != null) {
-
lensEngine
.release();
-
}
>>访问机器学习服务开源仓库地址:GitHub、 Gitee
>>访问华为开发者联盟官网,了解更多相关内容
>>获取开发指导文档
关注我们,第一时间了解华为移动服务最新技术资讯~
转载:https://blog.csdn.net/HUAWEI_HMSCore/article/details/116048428