紫罗藤 发表于 2023-5-26 14:04:11

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

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

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

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

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

每条检测规则都是一个Object对象,其中name和calc属性为必填,其它参数参考具体的检测规则,如:
//这是一个检查人体是否的检测
const rule = {
    name: '人体站立状态要求', //必填,本规则自定义名称
    calc: 'stand', //必填,要执行的规则计算器名称
    offset : 15 //选填,规则的允许偏差范围
};

//规则嵌套,利用$or和$and逻辑规则进行多规嵌套
const rules = {
name: '右侧站立检查',
calc: '$and',
rules: [{
        name: '侧面视角',
        calc: 'camera-view',
        position: 'right'
}, rule]
};三、执行检测规则运算

所有的人体检测规则,有calc.Calculator负责执行。
const AiSport = requirePlugin("aiSport");
const humanDetection = AiSport.humanDetection;
const calculator = AiSport.calc.Calculator;

const rule = {
    name: '人体站立状态要求',
    calc: 'stand',
    offset : 15
};
const calculator = new Calculator();

//抽帧
const context = wx.createCameraContext();
const listener = context.onCameraFrame((frame) => {
        const iamge = {
                width: Number(frame.width),
                height: Number(frame.height),
                rawData: frame.data
        };
       
        //人体识别
        humanDetection.detectionAsync(image).then(human=>{
               
                //对人体识别结果,进行单规则姿态检测
                console.log(calculator.calculating(human, rule));
               
                //返回值:true-通过;false-不通过
               
        });
});
listener.start();四、姿态检测实战

下面带您来做一个马步蹲的姿态检查实战,如下图所示:

const AiSport = requirePlugin("aiSport");
const humanDetection = AiSport.humanDetection;
const Calculator = AiSport.calc.Calculator;

//下肢要求
const foot = {
        name: '脚90度检测',
        calc: '$or',
        rules: [{
                name: '左脚90度弯曲',
                calc: 'match-angle',
                angleKey: 'left_knee',
                secondKey: 'left_hip',
                thirdKey: 'left_ankle',
                angle: 90,
                offset: 25
        }, {
                name: '右脚90度弯曲',
                calc: 'match-angle',
                angleKey: 'right_knee',
                secondKey: 'right_hip',
                thirdKey: 'right_ankle',
                angle: 90,
                offset: 25
        }]
};

const arm = {
        name: '手臂180度检测',
        calc: '$or',
        rules: [{
                name: '左手180度伸直',
                calc: 'match-angle',
                angleKey: 'left_elbow',
                secondKey: 'left_shoulder',
                thirdKey: 'left_wrist',
                angle: 180,
                offset: 25
        }, {
                name: '右手180度伸直',
                calc: 'match-angle',
                angleKey: 'right_elbow',
                secondKey: 'right_shoulder',
                thirdKey: 'right_wrist',
                angle: 180,
                offset: 25
        }]
};

const shoulder = {
        name: '腋下夹角90度检测',
        calc: '$or',
        rules: [{
                name: '左腋90度',
                calc: 'match-angle',
                angleKey: 'left_shoulder',
                secondKey: 'left_elbow',
                thirdKey: 'left_hip',
                angle: 90,
                offset: 25
        }, {
                name: '右腋90度',
                calc: 'match-angle',
                angleKey: 'right_shoulder',
                secondKey: 'right_elbow',
                thirdKey: 'right_hip',
                angle: 90,
                offset: 25
        }]
};

const rule = {
        name: '马步蹲姿势检查',
        calc: '$and',
        rules: [{
                name: '全身进入图像范围内检查',
                calc: 'whole'
        }, foot, arm, shoulder]
};
const calculator = new Calculator();

//抽帧
const context = wx.createCameraContext();
const listener = context.onCameraFrame((frame) => {
        const iamge = {
                width: Number(frame.width),
                height: Number(frame.height),
                rawData: frame.data
        };
       
        //人体识别
        humanDetection.detectionAsync(image).then(human=>{
               
                //执行检测
                console.log(calculator.calculating(human, rule));
               
        });
});
listener.start();五、后记

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

来源:https://www.cnblogs.com/alphaair/p/17432480.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【一步步开发AI运动小程序】八、利用body-calc进行姿态识别