随着人工智能技术的不断发展,阿里体育等IT大厂,推出的“乐动力”、“天天跳绳”AI运动APP,让云上运动会、线上运动会、健身打卡、AI体育指导等概念空前火热。那么,能否将这些在APP成功应用的场景搬上小程序,分享这些概念的红利呢?本系列文章就带您一步一步从零开始开发一个AI运动小程序,本系列文章将使用“云智AI运动识别小程序插件”,请先行在微信服务市场或官网了解详情。

一、骨骼图绘制原理

人体骨骼图的绘制,是通过在camera组件上附一个同等大小的透明canvas组件,在上面绘制关键点达到与人体图像重合的目的。

二、绘制代码

<template>
<view class="human-detection">
<camera id="preview" class="preview" :style="videoStyles" flash="off" :device-position="deviceKey"
resolution="high" frame-size="low" @initdone="onCameraReady">
</camera>
<canvas v-if="poseDrawEnabled" class="preview graphs" type="2d" id="graphics" :style="videoStyles"></canvas>
</view>
</template> <script> const AiSports = requirePlugin("aiSport");
const PoseGraphs = AiSports.PoseGraphs;
const humanDetection = AiSports.humanDetection; export default {
data() {
return {
zoom: 1,
deviceKey: "back",
previewWidth: 480,
previewHeight: 640,
previewRate: 1, frameWidth: 480,
frameHeight: 640,
status: 'unknown',
fps: 0,
poseFps: 0, isHumanBody: false
};
},
computed: {
videoStyles() {
const style = `width:${this.previewWidth}px;height:${this.previewHeight}px;`; return style;
}
},
mounted() {
this.autoFitPreview(480, 640);
this.initCanvas();
},
methods: {
autoFitPreview(width, height) {
const sifno = uni.getSystemInfoSync();
let rate = sifno.windowWidth / width; this.previewWidth = width * rate;
this.previewHeight = height * rate;
this.previewRate = rate;
this.frameWidth = width;
this.frameHeight = height;
},
initCanvas() { const that = this;
const query = uni.createSelectorQuery().in(that);
query.select('#graphics')
.fields({
node: true,
size: true
})
.exec((res) => { if (utils.isEmptyArray(res))
return; const canvas = res[0].node;
const ctx = canvas.getContext('2d');
const dpr = uni.getSystemInfoSync().pixelRatio;
canvas.width = res[0].width * dpr;
canvas.height = res[0].height * dpr;
ctx.scale(dpr, dpr); that.canvas = canvas;
that.ctx = ctx; that.poseGraphs = new PoseGraphs(ctx, canvas.width, canvas.height, 1);
that.poseGraphs.lineColor = "#FF8E148C";//线条颜色 });
}, async detection(frame) { const human = await humanDetection.detectionAsync(frame);
//无结果
if (!human)
this.poseGraphs.clear();
else
this.poseGraphs.drawing(human.keypoints); }, initVideo() { if (this.camera)
return; const that = this;
this.camera = new CameraDevice();
this.camera.onFrame = frame => { that.fps = that.camera.fps; //重新自适应
if (frame.width != that.frameWidth || frame.height != that.frameHeight) {
that.autoFitPreview(frame.width, frame.height);
that.initCanvas();
} that.detection(frame);
};
}
}
}
</script> <style lang="scss">
.human-detection {
width: auto;
height: auto; .preview {
margin: auto;
width: 480px;
height: 640px;
} .graphs {
position: absolute;
top: 0;
left: 0;
z-index: 9999;
box-shadow: 0 0 14.4928rpx #CCC;
background-color: rgba(0, 0, 0, 0.01);
}
}
</style>

三、注意事项

小程序的抽帧图像大小与camera实时图像可能不一致(https://developers.weixin.qq.com/miniprogram/dev/component/camera.html#Bug-Tip),所以camera和canvas组件必须保持与帧图像保持同比缩放,否则可能导致骨骼与实时图像不一致。

下篇我们将为您介绍如何进行运动分析,敬请期待...

【一步步开发AI运动小程序】六、人体骨骼图绘制的更多相关文章

  1. Java可视化日历(Date类、DATe Format类、Calendar类综合运用),开发可视化日历小程序

    Java时间日期类综合运用,开发可视化日历小程序 由键盘输入指定格式的日期,打印这个月的日历 1.代码 import java.text.DateFormat; import java.text.Pa ...

  2. 开发一个微信小程序教程

    一.注册小程序账号 1.进入微信公众平台(https://mp.weixin.qq.com/),注册小程序账号,根据提示填写对应的信息即可. 2.注册成功后进入首页,在 小程序发布流程->小程序 ...

  3. 开发一个微信小程序项目教程

    一.注册小程序账号 1.进入微信公众平台(https://mp.weixin.qq.com/),注册小程序账号,根据提示填写对应的信息即可.2.注册成功后进入首页,在 小程序发布流程->小程序开 ...

  4. 如何快速地开发一个微信小程序

    如何快速地开发一个微信小程序呢?我觉得作为初学者,最好能有一个模板,然后改这个模板. 同样作为初学者,刚开始的时候我有下面的几个问题,后来通过问同学,我弄清楚了. 微信小程序可以连接MySQL或者Sq ...

  5. 全栈开发工程师微信小程序-中(下)

    全栈开发工程师微信小程序-中(下) 微信小程序视图层 wxml用于描述页面的结构,wxss用于描述页面的样式,组件用于视图的基本组成单元. // 绑定数据 index.wxml <view> ...

  6. 全栈开发工程师微信小程序-中(中)

    全栈开发工程师微信小程序-中(中) 开放能力 open-data 用于展示微信开放的数据 type 开放数据类型 open-gid 当 type="groupName" 时生效, ...

  7. 全栈开发工程师微信小程序-中

    全栈开发工程师微信小程序-中 多媒体及其他的组件 navigator 页面链接 target 在哪个目标上发生跳转,默认当前小程序,可选值self/miniProgram url 当前小程序内的跳转链 ...

  8. 全栈开发工程师微信小程序-上(下)

    全栈开发工程师微信小程序-上(下) icon 图标 success, success_no_circle, info, warn, waiting, cancel, download, search, ...

  9. 全栈开发工程师微信小程序-上(中)

    全栈开发工程师微信小程序-上(中) width: 750rpx; 750rpx代表与屏幕等宽,rpx的缩写responsive pixel,这个单位是可以根据屏幕大小进行自适应调整的像素单位. 小程序 ...

  10. 全栈开发工程师微信小程序 - 上

    全栈开发工程师微信小程序-上 实现swiper组件 swiper 滑块视图容器. indicator-dots 是否显示面板指示点 false indicator-color 指示点颜色 indica ...

随机推荐

  1. CMake构建学习笔记6-giflib库的构建

    前面构建的zlib.libpng.libjpeg和libtiff都提供了CMakeList.txt文件,因此都可以通过CMake进行构建.不过有的依赖库是并没有CMakeList.txt文件,也就是官 ...

  2. 安装vsftp服务器的时候遇到的问题

    安装vsftp服务器的时候遇到的问题 环境说明: 系统:阿里云centos7 面板:宝塔面板 问题描述: 在centos7中安装VSFTP的时候,使用命令行,ftp 然后输入用户名和密码,登陆之后,p ...

  3. 合合信息推出国央企智能文档处理解决方案,AI赋能信创国产化

    信息时代,数字化转型已成为推动经济高质量发展的关键力量.国央企是国民经济的重要支柱,其数字化转型进程关乎着自身与产业链上下游企业的共同发展.文档的智能化处理可有效提升信息流转的效率.促进知识的沉淀与传 ...

  4. SpringCloud入门(二)服务间调用和案例

    一.微服务拆分注意事项微服务拆分注意事项:1.单一职责:不同微服务,不要重复开发相同业务2.数据独立:不要访问其它微服务的数据库3.面向服务:将自己的业务暴露为接口,供其它微服务调用 1.微服务需要根 ...

  5. Angular 18+ 高级教程 – Reactive Forms

    前言 上一篇的 Ajax 和这一篇的表单 (Form) 都是前端最最最常见的需求. 为此,Angular 分别提供了两个小型库来帮助开发者实现这些需求: Ajax – HttpClient Form ...

  6. chatGPT能做职业规划?看完之后发现3年软测白做了!

    "每天都是重复.单调的工作,收入不理想,想跳槽无力,学习又没有动力和方向,不知道未来的发展在哪里,甚至想转行·····" 做测试久了,很多人都有诸如此类的疑惑,不想一直停留在测试需 ...

  7. foobar2000 v1.6.10 汉化版(2022.05.01)

    foobar2000 v1.6.10 汉化版 -----------------------[软件截图]---------------------- -----------------------[软 ...

  8. 全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用

    全网最适合入门的面向对象编程教程:56 Python 字符串与序列化-正则表达式和 re 模块应用 摘要: Python 的 re 模块提供了强大的正则表达式操作功能,用于在字符串中搜索.匹配.替换等 ...

  9. EAS之WALT算法介绍

    EAS调度器缘起 Linux内核的一直都使用完全公平调度器CFS(Completely Fair Scheduler)作为默认调度器,但是在使用中发现CFS如下几个问题. CFS主要是为了服务器性能优 ...

  10. 批量读取nii文件的shape

    import SimpleITK as sitk from glob import glob import os path = glob(r"D:\MyData\date\*") ...