Unity Kinect添加自定义姿势识别
前言
文中使用插件为KinectForUnity
百度云 https://pan.baidu.com/s/1dAEhJgXbXL0_ZtQdWd-ATA
提取码 3nsv
如要支持Unity2019或更高需自行升级插件请相关参考 https://blog.csdn.net/a71468293a/article/details/117084439
自定义姿势识别
添加自定义姿势枚举
打开KinectGestures
脚本,找到Gestures
枚举。
添加自己的动作,本文为MoveLeft
。
添加自定义姿势的逻辑
找到CheckForGesture
方法,在switch
中增加以下代码
case Gestures.MoveLeft:
switch (gestureData.state)
{
case 0: // gesture detection - phase 1
//判断保持直立
if (jointsTracked[shoulderCenterIndex] && jointsTracked[hipCenterIndex] &&
Mathf.Abs(jointsPos[shoulderCenterIndex].x - jointsPos[hipCenterIndex].x) < 0.1f )
{
SetGestureJoint(ref gestureData, timestamp, shoulderCenterIndex, jointsPos[shoulderCenterIndex]);
gestureData.progress = 0.5f;
}
break;
case 1:
// gesture phase 2 = complete 手势阶段2=完成
//timestamp 当前时间 gestureData 为记录参数
//gestureData.timestamp 记录时间
//当前时间-记录时间 < 1.5s 进行判断,如果超出时间则动作失败
if ((timestamp - gestureData.timestamp) < 1.5f)
{
//jointsPos[shoulderCenterIndex].x 是最新数据
//gestureData.jointPos.x 是1.5s之前的数据
bool isInPose = jointsTracked[shoulderCenterIndex] &&
//(jointsPos[shoulderCenterIndex].x - gestureData.jointPos.x) > 0.2f &&
(gestureData.jointPos.x - jointsPos[shoulderCenterIndex].x) > 0.2f &&
Mathf.Abs(jointsPos[shoulderCenterIndex].y - gestureData.jointPos.y) < 0.1f;
if (isInPose)
{
Vector3 jointPos = jointsPos[gestureData.joint];
CheckPoseComplete(ref gestureData, timestamp, jointPos, isInPose, 0f);
}
}
else
{
// cancel the gesture 取消手势
SetGestureCancelled(ref gestureData);
}
break;
}
break;
创建监听脚本并挂载
创建GestureController
脚本,使其继承自GestureListenerInterface
。
在KinectManager预制体上挂载KinectGestures
脚本与GestureController
脚本。
添加动作监听
在GestureController
中的GestureCompleted
方法内判断动作是否完成。
参考链接: https://lgxtvt.blog.csdn.net/article/details/79952033
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。