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
继承自GestureListenerInterface

KinectManager预制体上挂载KinectGestures脚本与GestureController脚本。
挂载脚本

添加动作监听

GestureController中的GestureCompleted方法内判断动作是否完成。
添加动作监听

参考链接: https://lgxtvt.blog.csdn.net/article/details/79952033

  • 5
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值