private void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) { UpdateBodyFrame(e); //なぜかbodiesやbodyがnullのまま処理に入ることがあるため、「bodies(body)がnullでない」かつ「配列に要素が1つでない」時だけ処理 int bodycount = 0; foreach (var body in bodies) { if (body == null) { Console.WriteLine(bodies); Console.WriteLine("null body"); return; } if (body.IsTracked) { bodycount++; user = body; filter.UpdateFilter(user); } } if (bodycount > 1) { Console.WriteLine("Recognize too many people"); return; } //処理を記述 if (user != null) { Processing(); } }
// Get body data from the body manager and track the joint for the active body void Update() { if (_bodyManager == null) { return; } Body[] data = _bodyManager.GetData(); if (data == null) { return; } // Use for actual multi-player environments! if ((data.Length >= ActiveBodyNumber) && (data[ActiveBodyNumber] != null) && (data[ActiveBodyNumber].IsTracked)) { GetComponent <Rigidbody>().isKinematic = true; m_jointFilter.UpdateFilter(data[ActiveBodyNumber]); var Joints = m_jointFilter.GetFilteredJoints(); m_leftHandState = data[ActiveBodyNumber].HandLeftState; m_rightHandState = data[ActiveBodyNumber].HandRightState; // Grab the mid spine position, we'll use this to make all other joint movement relative to the spine (this way we can limit the Y position of the character) var midSpinePosition = Joints[(int)JointType.SpineMid]; var jointPos = Joints[(int)JointToUse]; //jointPos.X -= midSpinePosition.X; //jointPos.Y -= midSpinePosition.Y; jointPos.Z -= midSpinePosition.Z; float zValue = (useZValue == true) ? jointPos.Z : 0f; Vector3 targetPosition = new Vector3((midSpinePosition.X + jointPos.X) * scale, ((yOffset + jointPos.Y) * scale), zValue); this.transform.position = targetPosition; } else { // Hide the object by moving it far away from the camera. this.transform.position = new Vector3(-100f, -100f, 0f); // Attempt to find the active body number by iterating through the current bodies, finding a relevant body, and then assigning the active body. Once we have one // the user will be reacting to it from that point forward. int bodyIndex = 0; foreach (Body body in data) { if ((body != null) && (body.IsTracked)) { ActiveBodyNumber = bodyIndex; break; } bodyIndex++; } } }
void BodyFrameReaderOnFrameArrived(object sender, BodyFrameArrivedEventArgs e) { using (var frame = e.FrameReference.AcquireFrame()) { if (bodies == null) { bodies = new Body[frame.BodyCount]; } frame.GetAndRefreshBodyData(bodies); var trackedBody = bodies.FirstOrDefault(body => body.IsTracked); if (trackedBody == null) { return; } jointFilter.UpdateFilter(trackedBody); var filteredJoints = jointFilter.GetFilteredJoints(); var joints = new Dictionary <BodyJointType, BodyJoint>(); for (var index = 0; index < filteredJoints.Length; index++) { var joint = filteredJoints[index]; joints[(BodyJointType)index] = new BodyJoint { Position = new Vec3 { X = joint.X, Y = joint.Y, Z = joint.Z } }; } foreach (var jointOrientation in trackedBody.JointOrientations.Values) { joints[(BodyJointType)jointOrientation.JointType].Orientation = new Vec4 { X = jointOrientation.Orientation.X, Y = jointOrientation.Orientation.Y, Z = jointOrientation.Orientation.Z, W = jointOrientation.Orientation.W } } ; listener.OnBodyFrameReceived(joints); } } }
private void Update() { this.IsInitialized = LocalKinectController.IsInitialized; currentInstance = this; if (this.IsInitialized) { if (LocalKinectController.HasBodyData()) { // Get Tracked Bodies var trackedBodies = LocalKinectController.GetTrackedBodies(); if (!IsTrackingHuman) { IsTrackingHuman = true; trackedBodyId = trackedBodies.First().TrackingId; } else { //TODO: Make sure it's the same tracked body as before. If not, do we stop updating the positions? positionSmoothingfilter.UpdateFilter(trackedBody); } } else { if (IsTrackingHuman) { //TODO: clear the skeleton here } IsTrackingHuman = false; } if (IsTrackingHuman) { var trackedBodies = LocalKinectController.GetTrackedBodies(); //Get the tracked body we've originally captured if (trackedBodies != null && trackedBodies.Length > 0) { //Note this can get expensive - figure out a way to avoid reassigning this all the time? trackedBody = trackedBodies.First(x => x.TrackingId == trackedBodyId); } } } }
private void Kinect_OnTrackedBody(object sender, BodyEventArgs e) { Body body = e.BodyData; if (Mode == ControlMode.Disabled) { return; } for (int i = 1; i >= 0; i--) // Starts looking from right hand. { bool isLeft = (i == 0); if (body.IsHandLiftForward(isLeft)) { if (usedHandIndex == -1) { usedHandIndex = i; } else if (usedHandIndex != i) { // In two-hand control mode, non-used hand would be used for pressing/releasing mouse button. if (Mode == ControlMode.MoveGripPressing) { DoMouseControlByHandState(i, body.GetHandState(isLeft)); } continue; } if (body.IsHandLiftUpward(isLeft) && body.IsHandLiftUpward(!isLeft)) { HandDist.Add(body.TwoHandsDistance()); //System.Diagnostics.Trace.WriteLine(HandDist.Count); if (HandDist.Count == 2) { if (HandDist[1] - HandDist[0] >= 0.25) { MouseControl.Wheel(120); //System.Diagnostics.Trace.WriteLine(1); HandDist[0] = HandDist[1]; HandDist.RemoveAt(1); } else if (HandDist[0] - HandDist[1] >= 0.25) { MouseControl.Wheel(-120); //System.Diagnostics.Trace.WriteLine(-1); HandDist[0] = HandDist[1]; HandDist.RemoveAt(1); } else { HandDist.RemoveAt(1); } } //System.Diagnostics.Trace.WriteLine(body.TwoHandsDistance()); } else { HandDist.Clear(); kinectJointFilter.UpdateFilter(body); //MVector2 handPos = body.GetHandRelativePosition(isLeft); MVector2 handPos = KinectBodyHelper.GetHandSmoothedRelativePosition(kinectJointFilter.GetFilteredJoints(), isLeft); MVector2 targetPos = cursorMapper.GetSmoothedOutputPosition(handPos); //System.Diagnostics.Trace.WriteLine(handPos.ToString()); MouseControl.MoveTo(targetPos.X, targetPos.Y); if (Mode == ControlMode.GripToPress) { DoMouseControlByHandState(i, body.GetHandState(isLeft)); } else if (Mode == ControlMode.HoverToClick) { if ((targetPos - lastCursorPos).Length() > HoverRange) { ToggleHoverTimer(false); hoverClicked = false; } lastCursorPos = targetPos; } } } else { if (usedHandIndex == i) { // Reset to none. usedHandIndex = NONE_USED; ReleaseGrip(i); } else if (Mode == ControlMode.MoveLiftClicking) { DoMouseClickByHandLifting(i, body.GetHandRelativePosition(isLeft)); //System.Diagnostics.Trace.WriteLine(body.GetHandRelativePosition(isLeft).Y); } else // Release mouse button when it's not regularly released, such as hand tracking lost. { ReleaseGrip(i); } } } ToggleHoverTimer(Mode == ControlMode.HoverToClick && usedHandIndex != -1); }
void Update() { if (_BodyManager == null) { return; } Body activeBody = null; Body[] data = _BodyManager.GetData(); if (data == null) { return; } // get all bodies in latest frame List <ulong> trackedIds = new List <ulong>(); foreach (Body b in data) { if (b == null) { continue; } if (b.IsTracked) { trackedIds.Add(b.TrackingId); } } // remove all untracked bodies List <ulong> knownIds = new List <ulong>(_Bodies.Keys); foreach (ulong id in knownIds) { if (!trackedIds.Contains(id)) { // tracked body lost _Bodies.Remove(id); if (id == _ActiveBodyIndex) { Destroy(_ActiveBodyObject); _ActiveBodyIndex = 0; OnBodyLost?.Invoke(); } } } // add new bodies foreach (Body b in data) { if (b == null) { continue; } if (b.IsTracked) { if (!_Bodies.ContainsKey(b.TrackingId)) { _Bodies[b.TrackingId] = b; } } } // find the body that is closest to the camera if (_ActiveBodyIndex == 0) { bool newBodyFound = false; // first take a look in the list of bodies that remained in sight if (_Bodies.Count > 0) { float min_z = MaxRecognitionDistance; ulong min_id = 0; foreach (ulong id in _Bodies.Keys) { _Bodies.TryGetValue(id, out Body b); float z = b.Joints[JointType.Head].Position.Z; if (z < MaxRecognitionDistance && z < min_z) { min_z = z; min_id = id; } } if (min_z < MaxRecognitionDistance) { newBodyFound = true; foreach (Body b in data) { if (b.TrackingId == min_id) { RegisterNewActiveBody(min_id, b); break; } } } } // then take a look in the list of all bodies that are new in the latest frame if (!newBodyFound) { float min_z = MaxRecognitionDistance; ulong min_id = 0; foreach (Body b in data) { if (b == null) { continue; } if (b.IsTracked) { float z = b.Joints[JointType.Head].Position.Z; if (z < MaxRecognitionDistance && z < min_z) { min_z = z; min_id = b.TrackingId; } } } if (min_z < MaxRecognitionDistance) { newBodyFound = true; foreach (Body b in data) { if (b.TrackingId == min_id) { RegisterNewActiveBody(min_id, b); break; } } } } } else { foreach (Body b in data) { if (_ActiveBodyIndex != 0 && b.TrackingId == _ActiveBodyIndex) { activeBody = b; break; } } } if (activeBody != null) { RefreshBodyObject(activeBody, _ActiveBodyObject); filter.UpdateFilter(activeBody); CameraSpacePoint c = filter.GetFilteredJoint(JointType.Head); } }
//update body from filtered data private void RefreshBodyObject(Kinect.Body body, KinectJointFilter bodyFilter, GameObject bodyObject, ulong id) { bodyFilter.UpdateFilter(body); filteredJoints = bodyFilter.GetFilteredJoints(); orientJoints.Clear(); orientJoints = CalculateJointRotations(body.JointOrientations); for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++) { filteredJointPos = GetVector3FromCameraSpacePoint(filteredJoints[(int)jt]); sourceJoint = body.Joints[jt]; targetJoint = null; filteredTargetJointPos = null; if (_BoneMap.ContainsKey(jt)) { targetJoint = body.Joints[_BoneMap[jt]]; filteredTargetJointPos = GetVector3FromCameraSpacePoint(filteredJoints[(int)_BoneMap[jt]]); } //jointObj = bodyObject.transform.Find(jt.ToString()); jointObj = jointTransforms[id][jt]; //calculate orientations of end joints that are not captured by the kinect if (zeroQuaternion.Equals(orientJoints[jt]) && filteredTargetJointPos.HasValue) { Vector3 direction = filteredJointPos - filteredTargetJointPos.Value; if (jt == Kinect.JointType.AnkleLeft || jt == Kinect.JointType.AnkleRight) //the ankle roations have to be pointed at the foot to match the mesh { if (jt == Kinect.JointType.AnkleLeft) { direction = GetVector3FromCameraSpacePoint(filteredJoints[(int)Kinect.JointType.FootLeft]) - filteredJointPos; } else { direction = GetVector3FromCameraSpacePoint(filteredJoints[(int)Kinect.JointType.FootRight]) - filteredJointPos; } Vector3 perpendicular = Vector3.Cross(direction, Vector3.up); Vector3 normal = Vector3.Cross(direction, perpendicular); if (normal.sqrMagnitude != 0 && direction.sqrMagnitude != 0) { orientJoints[jt] = Quaternion.LookRotation(normal, direction); //normal, direction } else { orientJoints[jt] = Quaternion.identity; } } else if (jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight) //the thumbs are along their parents forward vector so they are calculated { Vector3 perpendicular = Vector3.Cross(direction, Vector3.up); Vector3 normal = Vector3.Cross(perpendicular, direction); if (normal.sqrMagnitude != 0 && direction.sqrMagnitude != 0) { orientJoints[jt] = Quaternion.LookRotation(normal, direction); } else { orientJoints[jt] = Quaternion.identity; } } else if (jt == Kinect.JointType.Neck) //rotate the neck from side to side while keeping the Z axis pointing forwards { Vector3 forward = orientJoints[_BoneMap[jt]] * Vector3.forward; Vector3 childFilteredJointPos = GetVector3FromCameraSpacePoint(filteredJoints[(int)_JointChildMap[jt]]); Vector3 y = childFilteredJointPos - filteredJointPos; orientJoints[jt] = Quaternion.LookRotation(forward, y); } else //by default set the up axis to point away from the joint and forward axis towards the parent's forward axis { Vector3 forward = orientJoints[_BoneMap[jt]] * Vector3.forward; // calculate a rotation, Y forward for Kinect if (direction.sqrMagnitude != 0) { orientJoints[jt] = Quaternion.LookRotation(forward, direction); } else { orientJoints[jt] = Quaternion.identity; } } } else //if joints are not computed in above, then point their Y value at the next joint while keeping their current Z value { if (_JointChildMap.ContainsKey(jt) && rotateToNextJoint) { Vector3 childFilteredJointPos = GetVector3FromCameraSpacePoint(filteredJoints[(int)_JointChildMap[jt]]); Vector3 y = childFilteredJointPos - filteredJointPos; orientJoints[jt] = Quaternion.LookRotation(orientJoints[jt] * Vector3.forward, y); } } //check tracking state of joint to make sure it is tracked, turn off rendered objects if not. if (debugJoints) { switch (sourceJoint.TrackingState) { case Kinect.TrackingState.NotTracked: if (jointObj.GetChild(0).gameObject.activeSelf) { jointObj.GetChild(0).gameObject.SetActive(useUntrackedJoints); } JointTracked[body.TrackingId][jt.ToString()] = useUntrackedJoints; break; case Kinect.TrackingState.Inferred: if (jointObj.GetChild(0).gameObject.activeSelf) { jointObj.GetChild(0).gameObject.SetActive(useInferredJoints); } jointObj.localPosition = filteredJointPos; jointObj.localRotation = orientJoints[jt]; JointTracked[body.TrackingId][jt.ToString()] = useInferredJoints; break; case Kinect.TrackingState.Tracked: if (!jointObj.GetChild(0).gameObject.activeSelf) { jointObj.GetChild(0).gameObject.SetActive(true); } jointObj.localPosition = filteredJointPos; jointObj.localRotation = orientJoints[jt]; JointTracked[body.TrackingId][jt.ToString()] = true; break; default: break; } LineRenderer lr = jointObj.GetComponent <LineRenderer>(); if (targetJoint.HasValue) { lr.useWorldSpace = false; lr.SetPosition(0, Vector3.zero); lr.SetPosition(1, Quaternion.Inverse(jointObj.localRotation) * (filteredTargetJointPos.Value - filteredJointPos)); lr.startColor = GetColorForState(sourceJoint.TrackingState); lr.endColor = GetColorForState(targetJoint.Value.TrackingState); } else { lr.enabled = false; } } else { switch (sourceJoint.TrackingState) { case Kinect.TrackingState.NotTracked: JointTracked[body.TrackingId][jt.ToString()] = useUntrackedJoints; break; case Kinect.TrackingState.Inferred: JointTracked[body.TrackingId][jt.ToString()] = useInferredJoints; break; case Kinect.TrackingState.Tracked: JointTracked[body.TrackingId][jt.ToString()] = true; break; default: break; } jointObj.localPosition = filteredJointPos; jointObj.localRotation = orientJoints[jt]; } /*if(jointObj.localRotation == zeroQuaternion) * { * Vector3 perpendicular = Vector3.Cross(jointObj.localPosition, Vector3.up); * Vector3 normal = Vector3.Cross(perpendicular, jointObj.localPosition); * * // calculate a rotation * jointObj.rotation.SetLookRotation(normal, jointObj.localPosition); * }*/ } }