翼度科技»论坛 编程开发 JavaScript 查看内容

【一步步开发AI运动小程序】八、利用body-calc进行姿态识别

7

主题

7

帖子

21

积分

新手上路

Rank: 1

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

人体姿态检测能力是插件的核心功能之一,插件为您封装好了基本的人体检测及逻辑运算检测规则。
1.1、基本人体检测规则

插件提供了:肢体角度匹配、平行检测、垂直检测、视角检查、站立检查、卧躺检查、人体范围检查、点位碰撞等检测规则,详情参考api-docs文档。
1.1、逻辑运算规则

插件提供了:$or、$and两个逻辑运算器,详情参考api-docs文档。
二、规则编写语法

每条检测规则都是一个Object对象,其中name和calc属性为必填,其它参数参考具体的检测规则,如:
  1. //这是一个检查人体是否的检测
  2. const rule = {
  3.     name: '人体站立状态要求', //必填,本规则自定义名称
  4.     calc: 'stand', //必填,要执行的规则计算器名称
  5.     offset : 15 //选填,规则的允许偏差范围
  6. };
  7. //规则嵌套,利用$or和$and逻辑规则进行多规嵌套
  8. const rules = {
  9.   name: '右侧站立检查',
  10.   calc: '$and',
  11.   rules: [{
  12.           name: '侧面视角',
  13.         calc: 'camera-view',
  14.         position: 'right'
  15.   }, rule]
  16. };
复制代码
三、执行检测规则运算

所有的人体检测规则,有calc.Calculator负责执行。
  1. const AiSport = requirePlugin("aiSport");
  2. const humanDetection = AiSport.humanDetection;
  3. const calculator = AiSport.calc.Calculator;
  4. const rule = {
  5.     name: '人体站立状态要求',
  6.     calc: 'stand',
  7.     offset : 15
  8. };
  9. const calculator = new Calculator();
  10. //抽帧
  11. const context = wx.createCameraContext();
  12. const listener = context.onCameraFrame((frame) => {
  13.         const iamge = {
  14.                 width: Number(frame.width),
  15.                 height: Number(frame.height),
  16.                 rawData: frame.data
  17.         };
  18.        
  19.         //人体识别
  20.         humanDetection.detectionAsync(image).then(human=>{
  21.                
  22.                 //对人体识别结果,进行单规则姿态检测
  23.                 console.log(calculator.calculating(human, rule));
  24.                
  25.                 //返回值:true-通过;false-不通过
  26.                
  27.         });
  28. });
  29. listener.start();
复制代码
四、姿态检测实战

下面带您来做一个马步蹲的姿态检查实战,如下图所示:
  1. const AiSport = requirePlugin("aiSport");
  2. const humanDetection = AiSport.humanDetection;
  3. const Calculator = AiSport.calc.Calculator;
  4. //下肢要求
  5. const foot = {
  6.         name: '脚90度检测',
  7.         calc: '$or',
  8.         rules: [{
  9.                 name: '左脚90度弯曲',
  10.                 calc: 'match-angle',
  11.                 angleKey: 'left_knee',
  12.                 secondKey: 'left_hip',
  13.                 thirdKey: 'left_ankle',
  14.                 angle: 90,
  15.                 offset: 25
  16.         }, {
  17.                 name: '右脚90度弯曲',
  18.                 calc: 'match-angle',
  19.                 angleKey: 'right_knee',
  20.                 secondKey: 'right_hip',
  21.                 thirdKey: 'right_ankle',
  22.                 angle: 90,
  23.                 offset: 25
  24.         }]
  25. };
  26. const arm = {
  27.         name: '手臂180度检测',
  28.         calc: '$or',
  29.         rules: [{
  30.                 name: '左手180度伸直',
  31.                 calc: 'match-angle',
  32.                 angleKey: 'left_elbow',
  33.                 secondKey: 'left_shoulder',
  34.                 thirdKey: 'left_wrist',
  35.                 angle: 180,
  36.                 offset: 25
  37.         }, {
  38.                 name: '右手180度伸直',
  39.                 calc: 'match-angle',
  40.                 angleKey: 'right_elbow',
  41.                 secondKey: 'right_shoulder',
  42.                 thirdKey: 'right_wrist',
  43.                 angle: 180,
  44.                 offset: 25
  45.         }]
  46. };
  47. const shoulder = {
  48.         name: '腋下夹角90度检测',
  49.         calc: '$or',
  50.         rules: [{
  51.                 name: '左腋90度',
  52.                 calc: 'match-angle',
  53.                 angleKey: 'left_shoulder',
  54.                 secondKey: 'left_elbow',
  55.                 thirdKey: 'left_hip',
  56.                 angle: 90,
  57.                 offset: 25
  58.         }, {
  59.                 name: '右腋90度',
  60.                 calc: 'match-angle',
  61.                 angleKey: 'right_shoulder',
  62.                 secondKey: 'right_elbow',
  63.                 thirdKey: 'right_hip',
  64.                 angle: 90,
  65.                 offset: 25
  66.         }]
  67. };
  68. const rule = {
  69.         name: '马步蹲姿势检查',
  70.         calc: '$and',
  71.         rules: [{
  72.                 name: '全身进入图像范围内检查',
  73.                 calc: 'whole'
  74.         }, foot, arm, shoulder]
  75. };
  76. const calculator = new Calculator();
  77. //抽帧
  78. const context = wx.createCameraContext();
  79. const listener = context.onCameraFrame((frame) => {
  80.         const iamge = {
  81.                 width: Number(frame.width),
  82.                 height: Number(frame.height),
  83.                 rawData: frame.data
  84.         };
  85.        
  86.         //人体识别
  87.         humanDetection.detectionAsync(image).then(human=>{
  88.                
  89.                 //执行检测
  90.                 console.log(calculator.calculating(human, rule));
  91.                
  92.         });
  93. });
  94. listener.start();
复制代码
五、后记

body-calc的检测规则都是对象化,所以开发者实际应用中可以考虑采用JSON方式持久化,放置在后端,便于运动、姿态的检测更新、配置化等。
下篇将为您介运动调试分析工具的使用,敬请期待...

来源:https://www.cnblogs.com/alphaair/p/17432480.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具