Exemple #1
0
        static void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                }

                var users = _skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).ToList();

                if (users.Count > 0)
                {
                    string json = users.Serialize(_coordinateMapper, _mode);

                    foreach (var socket in _clients)
                    {
                        socket.Send(json);
                    }
                }
            }
        }
Exemple #2
0
 private void OnSkeletonFrameReady(object o, SkeletonFrameReadyEventArgs sf)
 {
     // Console.WriteLine("Skeleton");
     {
         using (SkeletonFrame skeletonFrame = sf.OpenSkeletonFrame())
         {
             if (skeletonFrame == null)
             {
                 return;
             }
             try
             {
                 skeletonFrame.CopySkeletonDataTo(skels);
                 var accelerometerReading = ks.AccelerometerGetCurrentReading();
                 inter_stream.ProcessSkeleton(skels, accelerometerReading, skeletonFrame.Timestamp);
             }
             catch (InvalidOperationException)
             {
                 Console.WriteLine("Skeleton Problems");
                 // SkeletonFrame functions may throw w
                 // into a bad state.  Ignore the frame in that case.
             }
         }
     }
 }
        /// <summary>
        /// Event handler for Kinect sensor's SkeletonFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

            // Acquire data from the SkeletonFrameReadyEventArgs...
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];

                try
                {
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    var accelerometerReading = sensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // SkeletonFrame functions may throw when the sensor gets
                    // into a bad state.  Ignore the frame in that case.
                }
            }
        }
Exemple #4
0
        void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (var colorFrame = e.OpenColorImageFrame()) {
                if (colorFrame != null)
                {
                    var pixel = new byte[colorFrame.PixelDataLength];
                    colorFrame.CopyPixelDataTo(pixel);

                    ImageRgb.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                                                          PixelFormats.Bgr32, null, pixel, colorFrame.Width * 4);
                }
            }

            using (var depthFrame = e.OpenDepthImageFrame()) {
                if (depthFrame != null)
                {
                    // Depth情報を入れる
                    // GetRawPixelData()はインタラクションライブラリ内で実装された拡張メソッド
                    stream.ProcessDepth(depthFrame.GetRawPixelData(), depthFrame.Timestamp);
                }
            }

            using (var skeletonFrame = e.OpenSkeletonFrame()) {
                if (skeletonFrame != null)
                {
                    var skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);

                    // スケルトン情報を入れる
                    stream.ProcessSkeleton(skeletons, kinect.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
                }
            }
        }
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            if (_sensor != sender)
            {
                return;
            }

            using (var skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    try
                    {
                        skeletonFrame.CopySkeletonDataTo(_skeletons);
                        var accelerometerReading = _sensor.AccelerometerGetCurrentReading();

                        _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                    }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Evènement : A chaque nouvelle image de squelette
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EventSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            // Récupération des squelettes trouvés et envoie au interaction stream
            Skeleton[] skeletons = null;
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    interactionStream.ProcessSkeleton(skeletons, Sensor.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException) { }
            }

            // Vider les anciens os dessinés, donc rajout de l'image sur la canvas
            canvas.Children.Clear();
            canvas.Children.Add(image);
            // Si il n'y a pas encore le canvas de position, on l'ajoute
            if (!canvasDessin.Children.Contains(canvasPosition))
            {
                canvasDessin.Children.Add(canvasPosition);
            }

            if (skeletons.Length == 0) // Si pas de squelette
            {
                return;
            }

            var skels = skeletons.OrderBy(sk => sk.Position.Z);
            //var skels = skeletons.Where(sk => sk.TrackingState == SkeletonTrackingState.Tracked).OrderBy(sk => sk.Position.Z);
            bool skelDrawn = false;

            // Pour chaque squelette
            foreach (Skeleton skel in skels)
            {
                if (skel.TrackingState == SkeletonTrackingState.Tracked && !skelDrawn) // Si on trouve le squelette et on en a dessiné un
                {
                    // On dessine la main actuelle
                    if (actualHandType == InteractionHandType.Left)
                    {
                        DrawHand(skel, JointType.HandLeft);
                    }
                    else if (actualHandType == InteractionHandType.Right)
                    {
                        DrawHand(skel, JointType.HandRight);
                    }
                    skelDrawn = true;
                }
            }
        }
Exemple #7
0
        private void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skelFrame = e.OpenSkeletonFrame())
            {
                if (skelFrame != null && server.serverMasterOptions.kinectOptions[kinectID].trackSkeletons)
                {
                    Skeleton[] skeletons = new Skeleton[6];
                    skelFrame.CopySkeletonDataTo(skeletons);
                    Vector4 acceleration = kinect.AccelerometerGetCurrentReading();

                    if (interactStream != null)
                    {
                        interactStream.ProcessSkeleton(skeletons, acceleration, skelFrame.Timestamp);
                    }


                    //Generate the transformation matrix for the skeletons
                    double               kinectYaw                  = server.serverMasterOptions.kinectOptions[kinectID].kinectYaw;
                    Point3D              kinectPosition             = server.serverMasterOptions.kinectOptions[kinectID].kinectPosition;
                    Matrix3D             gravityBasedKinectRotation = findRotation(new Vector3D(acceleration.X, acceleration.Y, acceleration.Z), new Vector3D(0, -1, 0));
                    AxisAngleRotation3D  yawRotation                = new AxisAngleRotation3D(new Vector3D(0, 1, 0), -kinectYaw);
                    RotateTransform3D    tempTrans                  = new RotateTransform3D(yawRotation);
                    TranslateTransform3D transTrans                 = new TranslateTransform3D((Vector3D)kinectPosition);
                    Matrix3D             masterMatrix               = Matrix3D.Multiply(Matrix3D.Multiply(tempTrans.Value, gravityBasedKinectRotation), transTrans.Value);
                    skeletonTransformation = masterMatrix;  //This may need a lock on it, but that will require a seperate lock object

                    //Transform the skeletons based on the Kinect settings
                    for (int i = 0; i < skeletons.Length; i++)
                    {
                        if (skeletons[i].TrackingState != SkeletonTrackingState.NotTracked) //Don't bother to transform untracked skeletons
                        {
                            skeletons[i] = makeTransformedSkeleton(skeletons[i], masterMatrix);
                        }
                    }

                    //Add the skeletons to the list to be merged
                    lock (skeletonData.actualSkeletons)
                    {
                        //skeletonData.actualSkeletons.Clear();
                        for (int i = 0; i < skeletons.Length; i++)
                        {
                            skeletonData.actualSkeletons[i].skeleton = skeletons[i];
                        }
                    }

                    lock (skeletonData)
                    {
                        //The skeleton data is constaintly going to change, so lets just call the update everytime we get new data
                        skeletonData.needsUpdate = true;
                    }
                }
            }
        }
Exemple #8
0
 private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
 {
     using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
     {
         if (skeletonFrame != null)
         {
             skeletonList = new Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
             skeletonFrame.CopySkeletonDataTo(skeletonList);
             stream.ProcessSkeleton(skeletonList, sensor.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
         }
     }
 }
        private void KinectSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (var colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame == null)
                {
                    return;
                }

                var pixels = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(pixels);

                var stride = colorFrame.Width * 4;
                ImageSource = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
            }

            using (var depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame == null)
                {
                    return;
                }

                try
                {
                    interactionStream.ProcessDepth(depthFrame.GetRawPixelData(), depthFrame.Timestamp);
                }
                catch (InvalidOperationException ex)
                {
                    Console.Error.WriteLine(ex);
                }
            }

            using (var skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    var accelerometerReading = kinectSensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException ex)
                {
                    Console.Error.WriteLine(ex);
                }
            }
        }
        void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skf = e.OpenSkeletonFrame())
            {
                if (skf == null)
                {
                    return;
                }

                Skeleton[] skeletons = new Skeleton[skf.SkeletonArrayLength];
                skf.CopySkeletonDataTo(skeletons);
                var accelerometerReading = kinect.AccelerometerGetCurrentReading();
                interStream.ProcessSkeleton(skeletons, accelerometerReading, skf.Timestamp);
            }
        }
        /// <summary>
        /// Instantiates a new InteractionStream, feeds this InteractionStream with Skeleton- and DepthData and subscribes to the InteractionFrameReady event.
        /// </summary>
        /// <param name="kinectSensor">The Kinect sensor passed to the interaction stream instance.</param>
        /// <param name="interactionClient">The interaction client passed to the interaction stream instance.</param>
        /// <returns>An UserInfo stream that contains an action that disposes the interaction stream when the observable is disposed.</returns>
        public static IObservable <UserInfo[]> GetUserInfoObservable(this KinectSensor kinectSensor, IInteractionClient interactionClient)
        {
            if (kinectSensor == null)
            {
                throw new ArgumentNullException("kinect");
            }
            if (interactionClient == null)
            {
                throw new ArgumentNullException("interactionClient");
            }

            if (!kinectSensor.DepthStream.IsEnabled)
            {
                throw new InvalidOperationException("The depth stream is not enabled, but mandatory.");
            }
            if (!kinectSensor.SkeletonStream.IsEnabled)
            {
                throw new InvalidOperationException("The skeleton stream is not enabled, but mandatory.");
            }

            return(Observable.Create <UserInfo[]>(observer =>
            {
                var stream = new InteractionStream(kinectSensor, interactionClient);
                var obs = kinectSensor.GetAllFramesReadyObservable()
                          .SelectStreams((_, __) => Tuple.Create(_.Timestamp, __.Timestamp))
                          .Subscribe(_ =>
                {
                    stream.ProcessSkeleton(_.Item3, kinectSensor.AccelerometerGetCurrentReading(), _.Item4.Item1);
                    stream.ProcessDepth(_.Item2, _.Item4.Item2);
                });

                stream.GetInteractionFrameReadyObservable()
                .SelectUserInfo()
                .Subscribe(_ => observer.OnNext(_));

                return new Action(() =>
                {
                    obs.Dispose();
                    stream.Dispose();
                });
            }));
        }
Exemple #12
0
        private void FuncoesEsqueletoUsuario(SkeletonFrame quadro)
        {
            if (quadro == null)
            {
                return;
            }

            using (quadro)
            {
                Skeleton esqueletoUsuario = quadro.ObterEsqueletoUsuario();

                if (btnDesenhar.IsChecked)
                {
                    Skeleton[] esqueletos = new Skeleton[6];
                    quadro.CopySkeletonDataTo(esqueletos);
                    fluxoInteracao.ProcessSkeleton(esqueletos, kinect.AccelerometerGetCurrentReading(), quadro.Timestamp);
                    EsqueletoUsuarioAuxiliar esqueletoAuxiliar = new EsqueletoUsuarioAuxiliar(kinect);

                    if (configuracaoMaoDireita.DesenhoAtivo)
                    {
                        esqueletoAuxiliar.InteracaoDesenhar(esqueletoUsuario.Joints[JointType.HandRight], canvasDesenho, configuracaoMaoDireita);
                    }

                    if (configuracaoMaoEsquerda.DesenhoAtivo)
                    {
                        esqueletoAuxiliar.InteracaoDesenhar(esqueletoUsuario.Joints[JointType.HandLeft], canvasDesenho, configuracaoMaoEsquerda);
                    }
                }
                else
                {
                    foreach (IRastreador rastreador in rastreadores)
                    {
                        rastreador.Rastrear(esqueletoUsuario);
                    }

                    if (btnEsqueletoUsuario.IsChecked)
                    {
                        quadro.DesenharEsqueletoUsuario(kinect, canvasKinect);
                    }
                }
            }
        }
Exemple #13
0
        void runtime_SkeletonFrameReady(object sender, Microsoft.Kinect.SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skels = null;
            long       ts    = 0;

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skels = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skels);
                    ts = skeletonFrame.Timestamp;
                }
            }

            if (skels != null)
            {
                Vector4 accel = this.runtime.Runtime.AccelerometerGetCurrentReading();
                stream.ProcessSkeleton(skels, accel, ts);
            }
        }
Exemple #14
0
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // If exception skip frame
                }
            }
        }
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // SkeletonFrame functions may throw when the sensor gets
                    // into a bad state.  Ignore the frame in that case.
                }
            }
        }
        private void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
            //double handposition = 0;
            //onRaisedVoiceCommand(new VoiceCommandEventArgs("I'm in state" + state +" and I got skeleton data"));
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    inStream.ProcessSkeleton(skeletons, sensor.AccelerometerGetCurrentReading(), skeletonFrame.Timestamp);
                }

                /*
                 * foreach (Skeleton skeleton in skeletons)
                 * {
                 *  if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
                 *  {
                 *      Joint wristjoint = skeleton.Joints[JointType.WristLeft];
                 *      if (wristjoint.TrackingState == JointTrackingState.Tracked)
                 *      {
                 *          if (wristjoint.Position.Y > handposition)
                 *          {
                 *              handposition = wristjoint.Position.Y;
                 *          }
                 *      }
                 *      wristjoint = skeleton.Joints[JointType.WristRight];
                 *      if (wristjoint.TrackingState == JointTrackingState.Tracked)
                 *      {
                 *          if (wristjoint.Position.Y > handposition)
                 *          {
                 *              handposition = wristjoint.Position.Y;
                 *          }
                 *      }
                 *  }
                 * }
                 */
            }
        }
        //Listener del esqueleto
        private void SensorOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                //Si el eskeleto es null se sale del metodo
                if (skeletonFrame == null)
                {
                    return;
                }

                try
                {
                    //se le coloca el esqueleto al areglo
                    skeletonFrame.CopySkeletonDataTo(_skeletons);
                    var accelerometerReading = _sensor.AccelerometerGetCurrentReading();
                    _interactionStream.ProcessSkeleton(_skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
                catch (InvalidOperationException)
                {
                    // Error al obtener el esqueleto del sensor
                }
            }
        }
Exemple #18
0
        private void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skelFrame = e.OpenSkeletonFrame())
            {
                if (skelFrame != null && masterSettings.kinectOptionsList.Count > kinectID && (masterKinectSettings.mergeSkeletons || masterKinectSettings.sendRawSkeletons))
                {
                    DateTime   now       = DateTime.UtcNow;
                    Skeleton[] skeletons = new Skeleton[skelFrame.SkeletonArrayLength];
                    skelFrame.CopySkeletonDataTo(skeletons);


                    EigenWrapper.Matrix predAccel = new EigenWrapper.Matrix(3, 1);
                    predAccel[2, 0] = 1;
                    if (accelFilterStarted)
                    {
                        predAccel = accelerationFilter.PredictAndDiscard(0);
                    }

                    if (interactStream != null)
                    {
                        Vector4 filteredAccel = new Vector4();
                        filteredAccel.W = 0;
                        filteredAccel.X = (float)predAccel[0, 0];
                        filteredAccel.Y = (float)predAccel[1, 0];
                        filteredAccel.Z = (float)predAccel[2, 0];
                        interactStream.ProcessSkeleton(skeletons, filteredAccel, skelFrame.Timestamp);

                        //System.Diagnostics.Trace.WriteLine("[" + filteredAccel.X + ", " + filteredAccel.Y + ", " + filteredAccel.Z + "]");
                    }

                    //Generate the transformation matrix for the skeletons
                    double               kinectYaw                  = masterKinectSettings.kinectYaw;
                    Point3D              kinectPosition             = masterKinectSettings.kinectPosition;
                    Matrix3D             gravityBasedKinectRotation = findRotation(new Vector3D(predAccel[0, 0], predAccel[1, 0], predAccel[2, 0]), new Vector3D(0, -1, 0));
                    AxisAngleRotation3D  yawRotation                = new AxisAngleRotation3D(new Vector3D(0, 1, 0), -kinectYaw);
                    RotateTransform3D    tempYawTrans               = new RotateTransform3D(yawRotation);
                    TranslateTransform3D transTrans                 = new TranslateTransform3D((Vector3D)kinectPosition);
                    Matrix3D             rotationOnlyMatrix         = Matrix3D.Multiply(tempYawTrans.Value, gravityBasedKinectRotation);
                    Matrix3D             masterMatrix               = Matrix3D.Multiply(rotationOnlyMatrix, transTrans.Value);
                    skeletonTransformation = masterMatrix;
                    skeletonRotQuaternion  = KinectBase.HelperMethods.MatrixToQuaternion(rotationOnlyMatrix);

                    //Convert from Kinect v1 skeletons to KVR skeletons
                    KinectBase.KinectSkeleton[] kvrSkeletons = new KinectBase.KinectSkeleton[skeletons.Length];
                    for (int i = 0; i < kvrSkeletons.Length; i++)
                    {
                        //Set the tracking ID numbers for the hand grab data
                        int grabID = -1;
                        for (int j = 0; j < skeletonHandGrabData.Count; j++)
                        {
                            if (skeletonHandGrabData[j].skeletonTrackingID == skeletons[i].TrackingId)
                            {
                                grabID = j;
                                break;
                            }
                        }
                        if (grabID < 0)
                        {
                            skeletonHandGrabData.Add(new HandGrabInfo(skeletons[i].TrackingId));
                            grabID = skeletonHandGrabData.Count - 1;
                        }

                        kvrSkeletons[i]          = new KinectBase.KinectSkeleton();
                        kvrSkeletons[i].Position = new Point3D(skeletons[i].Position.X, skeletons[i].Position.Y, skeletons[i].Position.Z);
                        kvrSkeletons[i].SkeletonTrackingState = convertTrackingState(skeletons[i].TrackingState);
                        kvrSkeletons[i].TrackingId            = skeletons[i].TrackingId;
                        //kvrSkeletons[i].utcSampleTime = DateTime.UtcNow;
                        kvrSkeletons[i].sourceKinectID = kinectID;

                        for (int j = 0; j < skeletons[i].Joints.Count; j++)
                        {
                            KinectBase.Joint newJoint = new KinectBase.Joint();
                            newJoint.Confidence = KinectBase.TrackingConfidence.Unknown; //The Kinect 1 doesn't support the confidence property
                            newJoint.JointType  = convertJointType(skeletons[i].Joints[(JointType)j].JointType);
                            Vector4 tempQuat = skeletons[i].BoneOrientations[(JointType)j].AbsoluteRotation.Quaternion;
                            newJoint.Orientation.orientationQuaternion = new Quaternion(tempQuat.X, tempQuat.Y, tempQuat.Z, tempQuat.W);
                            Matrix4 tempMat = skeletons[i].BoneOrientations[(JointType)j].AbsoluteRotation.Matrix;
                            newJoint.Orientation.orientationMatrix = new Matrix3D(tempMat.M11, tempMat.M12, tempMat.M13, tempMat.M14,
                                                                                  tempMat.M21, tempMat.M22, tempMat.M23, tempMat.M24,
                                                                                  tempMat.M31, tempMat.M32, tempMat.M33, tempMat.M34,
                                                                                  tempMat.M41, tempMat.M42, tempMat.M43, tempMat.M44);
                            SkeletonPoint tempPos = skeletons[i].Joints[(JointType)j].Position;
                            newJoint.Position           = new Point3D(tempPos.X, tempPos.Y, tempPos.Z);
                            newJoint.TrackingState      = convertTrackingState(skeletons[i].Joints[(JointType)j].TrackingState);
                            newJoint.spatialErrorStdDev = getJointError(newJoint.Position, newJoint.TrackingState);
                            newJoint.utcTime            = now;
                            kvrSkeletons[i].skeleton[newJoint.JointType] = newJoint; //Skeleton doesn't need to be initialized because it is done in the KinectSkeleton constructor
                        }

                        //Get the hand states from the hand grab data array
                        kvrSkeletons[i].rightHandClosed = skeletonHandGrabData[grabID].rightHandClosed;
                        kvrSkeletons[i].leftHandClosed  = skeletonHandGrabData[grabID].leftHandClosed;
                    }

                    //Add the skeleton data to the event handler and throw the event
                    KinectBase.SkeletonEventArgs skelE = new KinectBase.SkeletonEventArgs();
                    skelE.skeletons = kvrSkeletons;
                    skelE.kinectID  = kinectID;

                    OnSkeletonChanged(skelE);
                }
            }
        }
        private void KinectOnSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs eventArgs)
        {
            using (SkeletonFrame skeletonFrame = eventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    int        i          = 0;
                    Skeleton[] squelettes = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(squelettes);

                    try
                    {
                        var accelerometerReading = kinect.AccelerometerGetCurrentReading();
                        interStream.ProcessSkeleton(squelettes, accelerometerReading, skeletonFrame.Timestamp);
                    }
                    catch (InvalidOperationException)
                    {}

                    bufSkel.Clear();

                    if (skeletonFrame.SkeletonArrayLength == 0)
                    {
                        return;
                    }

                    var trackedSquelettes = squelettes.Where(skel => skel.TrackingState == SkeletonTrackingState.Tracked).OrderBy <Skeleton, float>(skel => skel.Position.Z);

                    if (trackedSquelettes.Count() == 0)
                    {
                        return;
                    }

                    squelette = trackedSquelettes.First();

                    if (squelette != null)
                    {
                        players[i] = squelette;
                        drawBone(squelette, JointType.Head, JointType.ShoulderCenter, colorSkel);

                        drawBone(squelette, JointType.ShoulderCenter, JointType.ShoulderLeft, colorSkel);
                        drawBone(squelette, JointType.ShoulderCenter, JointType.ShoulderRight, colorSkel);

                        drawBone(squelette, JointType.ShoulderCenter, JointType.Spine, colorSkel);

                        drawBone(squelette, JointType.Spine, JointType.HipCenter, colorSkel);

                        drawBone(squelette, JointType.HipCenter, JointType.HipLeft, colorSkel);
                        drawBone(squelette, JointType.HipCenter, JointType.HipRight, colorSkel);

                        drawBone(squelette, JointType.ShoulderLeft, JointType.ElbowLeft, colorSkel);
                        drawBone(squelette, JointType.ElbowLeft, JointType.WristLeft, colorSkel);
                        drawBone(squelette, JointType.WristLeft, JointType.HandLeft, colorSkel);

                        drawBone(squelette, JointType.ShoulderRight, JointType.ElbowRight, colorSkel);
                        drawBone(squelette, JointType.ElbowRight, JointType.WristRight, colorSkel);
                        drawBone(squelette, JointType.WristRight, JointType.HandRight, colorSkel);

                        // Left Leg
                        drawBone(squelette, JointType.HipLeft, JointType.KneeLeft, colorSkel);
                        drawBone(squelette, JointType.KneeLeft, JointType.AnkleLeft, colorSkel);
                        drawBone(squelette, JointType.AnkleLeft, JointType.FootLeft, colorSkel);

                        // Right Leg
                        drawBone(squelette, JointType.HipRight, JointType.KneeRight, colorSkel);
                        drawBone(squelette, JointType.KneeRight, JointType.AnkleRight, colorSkel);
                        drawBone(squelette, JointType.AnkleRight, JointType.FootRight, colorSkel);

                        if (focusSquelette)
                        {
                            ViewPort.Camera = (PerspectiveCamera)squelette2CameraConverter.Convert(players[0], null, null, null);
                        }

                        ++i;
                    }

                    lignes.Points = bufSkel;
                }
            }
        }
Exemple #20
0
        /// <summary>
        /// Event handler for Kinect sensor's SkeletonFrameReady event
        /// </summary>
        /// <param name="sender">object sending the event</param>
        /// <param name="e">event arguments</param>
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                //Bloque usado por InteractionStream
                if (skeletonFrame != null)
                {
                    //Se hace una copia del esqueleto se asigna la lectura del accelerometro y procesa interactionstream el esqueleto usando lo anterior.
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);

                    //Este bloque está sacado de:
                    //////////////////////////////////////////////////////////////////////////////////////////////////
                    //http://dotneteers.net/blogs/vbandi/archive/2013/05/03/kinect-interactions-with-wpf-part-iii-demystifying-the-interaction-stream.aspx
                    //////////////////////////////////////////////////////////////////////////////////////////////////
                    Vector4 accelerometerReading = sensor.AccelerometerGetCurrentReading();
                    interactionStream.ProcessSkeleton(skeletons, accelerometerReading, skeletonFrame.Timestamp);
                }
            }

            using (DrawingContext dc = this.drawingGroup.Open())
            {
                //Dibuja los rectángulos que no están cogidos.
                puzzle.DrawPuzzle(dc);

                //Mostramos por pantalla el tiempo que nos queda.
                FormattedText t = new FormattedText(puzzle.getTiempo() + " s", CultureInfo.GetCultureInfo("es-es"), FlowDirection.LeftToRight, new Typeface("Verdana"),
                                                    24,
                                                    Brushes.Black);
                dc.DrawText(t, new Point(570, 10));

                //Comprobamos que kinect nos haya leido el esqueleto
                if (skeletons.Length != 0)
                {
                    // Coge cada esqueleto de cada frame para manejarlo dentro.
                    foreach (Skeleton skel in skeletons)
                    {
                        //Asignamos al esqueleto de este frame el estado del rastreo del esqueleto
                        //Si detecta los puntos de las articulaciones entra en este bloque.
                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            //Comprobamos si se ha acabado el tutorial.
                            if (!fin_tuto)
                            {
                                //Si el tutorial no se ha acabado dibujamos la imagen del tutorial y pintamos un circulo rojo para que al tocarlo se acabe el tutorial
                                dc.DrawImage(tutorial, new Rect(0, 0, RenderWidth, RenderHeight));
                                FormattedText ft = new FormattedText("Toque el\ncirculo para\nfin Tutorial", CultureInfo.GetCultureInfo("es-es"), FlowDirection.LeftToRight, new Typeface("Consola"),
                                                                     14,
                                                                     Brushes.Black);
                                dc.DrawText(ft, new Point(550, 60));
                                dc.DrawEllipse(Brushes.Red, null, new Point(600, 30), 30, 30);

                                //Si la mano toca el circulo.
                                if (SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).X > 560 &&
                                    SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).Y < 60)
                                {
                                    //Ponemos a true el final del tutorial y empieza a contar el tiempo.
                                    fin_tuto = true;
                                    puzzle.iniciarTiempo();
                                }
                            }
                            else
                            {   //Dibujamos la pieza que esté cogida.
                                puzzle.DrawPuzzleCogidos(dc);
                            }

                            //Si se ha acabado el tiempo para resolver el puzzle mostramos la puntuación.
                            if (puzzle.getFin())
                            {
                                FormattedText punt = new FormattedText("Puntuacion: " + puzzle.getPuntuacion() + "/100", CultureInfo.GetCultureInfo("es-es"), FlowDirection.LeftToRight, new Typeface("Verdana"),
                                                                       24,
                                                                       Brushes.Black);
                                dc.DrawText(punt, new Point(240, 240));
                            }

                            //Actualizamos el esqueleto del puzzle
                            puzzle.actualizarSkeleto(skel);

                            //Dibujamos las manos.
                            dc.DrawImage(manoDer, new Rect(this.SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).X - manoDer.Width / 2, this.SkeletonPointToScreen(skel.Joints[JointType.HandRight].Position).Y - manoDer.Height / 2, manoDer.Width, manoDer.Height));
                            dc.DrawImage(manoIzq, new Rect(this.SkeletonPointToScreen(skel.Joints[JointType.HandLeft].Position).X - manoIzq.Width / 2, this.SkeletonPointToScreen(skel.Joints[JointType.HandLeft].Position).Y - manoIzq.Height / 2, manoIzq.Width, manoIzq.Height));
                        }
                    }
                }

                // prevent drawing outside of our render area
                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));
            }
        }
Exemple #21
0
        private void KinectSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs args)
        {
            Skeleton[] skeletons = new Skeleton[0];
            using (SkeletonFrame skeletonFrame = args.OpenSkeletonFrame()) {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    try {
                        skeletonFrame.CopySkeletonDataTo(skeletons);
#if MOUSE_CONTROL
                        Vector4 accelReading = sensor.AccelerometerGetCurrentReading();
                        _interactionStream.ProcessSkeleton(skeletons, accelReading, skeletonFrame.Timestamp);
#endif
                    }
                    catch (InvalidOperationException) {
                        //Ignoramos el frame
                        activeSkeleton = null;
                        return;
                    }
                }
            }

            if (skeletons.Length == 0)
            {
                activeSkeleton = null;
                return;
            }

            //Retorna el primer jugador que tenga tracking del Kinect.
            Skeleton[] trackedSkeletons = (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).ToArray();
            if (trackedSkeletons.Length == 0)
            {
                activeSkeleton = null;
                return;
            }

            //Para este punto ya tenemos por lo menos un candidato para activeSkeleton
            activeSkeleton = trackedSkeletons[0];
            if (trackedSkeletons.Length > 1)
            {
                for (int i = 1; i < trackedSkeletons.Length; i++)
                {
                    if (KinectDistanceTools.FirstSkeletonIsCloserToSensor(ref trackedSkeletons[i], ref activeSkeleton, JointType.HipCenter))
                    {
                        activeSkeleton = trackedSkeletons[i];
                    }
                }
            }

#if MOUSE_CONTROL
            /*
             * El sistema de referencia que ocupa el kinect tiene como origen la posición
             * del sensor, es decir que eje Z apunta al frente del kinect, el eje X el positivo estád
             * a la derecha del kinect y el eje Y
             * */

            //A partir de ahora ya sabemos que hay un jugador detectado por el Kinect y podemos obtener alguna
            //parte del cuerpo de referencia.
            Joint rightHand = activeSkeleton.Joints[JointType.WristRight];
            Joint leftHand  = activeSkeleton.Joints[JointType.WristLeft];

            XValueRight.Text = rightHand.Position.X.ToString("F", CultureInfo.InvariantCulture);
            YValueRight.Text = rightHand.Position.Y.ToString("F", CultureInfo.InvariantCulture);
            ZValueRight.Text = rightHand.Position.Z.ToString("F", CultureInfo.InvariantCulture);

            XValueLeft.Text = leftHand.Position.X.ToString("F", CultureInfo.InvariantCulture);
            YValueLeft.Text = leftHand.Position.Y.ToString("F", CultureInfo.InvariantCulture);
            ZValueLeft.Text = leftHand.Position.Z.ToString("F", CultureInfo.InvariantCulture);

            //Queremos la mano más cercana al sensor para que controle el mouse.
            if (KinectDistanceTools.FirstIsCloserToSensor(ref rightHand, ref leftHand))
            {
                RightRaised.Text = "Activada";
                LeftRaised.Text  = "Desactivado";
                Vector2 result = mouseController.Move(ref rightHand, isClicks[(int)HandType.Right]);
                MousePos.Text = string.Format("X: {0}, Y: {1}, Click: {2}", result.x, result.y, isClicks[(int)HandType.Right] ? "Sí" : "No");
            }
            else
            {
                LeftRaised.Text  = "Activada";
                RightRaised.Text = "Desactivado";
                Vector2 result = mouseController.Move(ref leftHand, isClicks[(int)HandType.Left]);
                MousePos.Text = string.Format("X: {0}, Y: {1}, Click: {2}", result.x, result.y, isClicks[(int)HandType.Left] ? "Sí" : "No");
            }
#endif
        }