private void ExecutaRegraMaoDireitaAcimaDaCabeca(SkeletonFrame quadroAtual)
        {
            Skeleton[] esqueletos = new Skeleton[6];
            quadroAtual.CopySkeletonDataTo(esqueletos);

            Skeleton usuario = esqueletos.FirstOrDefault(esqueleto => esqueleto.TrackingState == SkeletonTrackingState.Tracked);

            if(usuario != null)
            {
                Joint maoDireita = usuario.Joints[JointType.HandRight];
                Joint maoEsquerda = usuario.Joints[JointType.HandLeft];
                Joint cabeca = usuario.Joints[JointType.Head];

                LmaoDireita.Content = maoDireita.Position.X.ToString();
                LmaoEsquerda.Content = maoEsquerda.Position.X.ToString();
                Lcabeca.Content = cabeca.Position.X.ToString();

                LmaoEsquerdaY.Content = maoEsquerda.Position.Y.ToString();
                LmaoDireitaY .Content = maoDireita.Position.Y.ToString();
                LcabecaY.Content = cabeca.Position.Y.ToString();

                bool novoTesteMaoDireitaAcimaCabeca = maoDireita.Position.Y > cabeca.Position.Y;

                if(MaoDireitaAcimaCabeca != novoTesteMaoDireitaAcimaCabeca)
                {
                    MaoDireitaAcimaCabeca = novoTesteMaoDireitaAcimaCabeca;

                    if(MaoDireitaAcimaCabeca)
                    {
                        MessageBox.Show("Mao direita acima da cabeca.");
                    }
                }
            }
        }
Beispiel #2
0
        void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Thread.Sleep(200); // 意図的な負荷。

            using (var frame = e.OpenSkeletonFrame())
            {
                if (frame == null)
                {
                    PositionText = "";
                    return;
                }

                var skeletons = new Skeleton[frame.SkeletonArrayLength];
                frame.CopySkeletonDataTo(skeletons);

                var skeleton = skeletons.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked);
                if (skeleton == null)
                {
                    PositionText = "";
                    return;
                }

                var p = skeleton.Position;
                PositionText = string.Format("({0:N3}, {1:N3}, {2:N3})", p.X, p.Y, p.Z);
            }
        }
        /// <summary>
        /// Handles the Kinect AllFramesReady event
        /// </summary>
        private void Sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            ColorImageFrame colorFrame = null;
            DepthImageFrame depthFrame = null;
            Skeleton[] skeletonData;

            try
            {
                colorFrame = e.OpenColorImageFrame();
                depthFrame = e.OpenDepthImageFrame();

                using (var skeletonFrame = e.OpenSkeletonFrame())
                {
                    if (colorFrame == null || depthFrame == null || skeletonFrame == null)
                        return;

                    skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletonData);
                }

                // Find a skeleton to track.
                // First see if our old one is good.
                // When a skeleton is in PositionOnly tracking state, don't pick a new one
                // as it may become fully tracked again.
                Skeleton skeletonOfInterest = skeletonData.FirstOrDefault(s => s.TrackingId == this.trackedSkeletonId && s.TrackingState != SkeletonTrackingState.NotTracked);

                if (skeletonOfInterest == null)
                {
                    // Old one wasn't around.  Find any skeleton that is being tracked and use it.
                    skeletonOfInterest = skeletonData.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked);

                    if (skeletonOfInterest != null)
                        this.trackedSkeletonId = skeletonOfInterest.TrackingId;
                }

                if (this.FrameDataUpdated != null)
                    this.FrameDataUpdated(this, new FrameData(colorFrame, depthFrame, skeletonOfInterest));
            }
            finally
            {
                if (colorFrame != null)
                    colorFrame.Dispose();

                if (depthFrame != null)
                    depthFrame.Dispose();
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            var server = new HubConnection("http://localhost:8001/");
            var method = server.CreateHubProxy("kinect");
            var sensor = Microsoft.Kinect.KinectSensor.KinectSensors.First();

            try
            {
                server.Start().Wait();
                sensor.SkeletonFrameReady += (sender, eventArgs) =>
                                             {
                                                 using (var frame = eventArgs.OpenSkeletonFrame())
                                                 {
                                                     if (frame == null) return;
                                                     var skeletons = new Skeleton[frame.SkeletonArrayLength];
                                                     frame.CopySkeletonDataTo(skeletons);
                                                     var user = skeletons.FirstOrDefault(x => x.TrackingState == SkeletonTrackingState.Tracked);
                                                     if (user == null) return;
                                                     var m = user.Joints.
                                                         Where(x => x.TrackingState == JointTrackingState.Tracked)
                                                         .Select(s =>
                                                                 {
                                                                     var c = sensor.CoordinateMapper.MapSkeletonPointToColorPoint(s.Position, ColorImageFormat.RgbResolution640x480Fps30);
                                                                     return new JointPosition
                                                                            {
                                                                                x = c.X,
                                                                                y = c.Y
                                                                            };
                                                                 }).ToList();
                                                     Console.Write(".");
                                                     method.Invoke("Send",m);
                                                 }
                                             };

                sensor.SkeletonStream.Enable(new TransformSmoothParameters()
                                             {
                                                 Correction = 0.7f,
                                                 JitterRadius = 0.1f,
                                                 MaxDeviationRadius = 0.1f,
                                                 Prediction = 0.7f,
                                                 Smoothing = 0.8f
                                             });

                sensor.Start();
                Console.WriteLine("Started.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
            finally
            {
                sensor.Stop();
                sensor.Dispose();
                server.Stop();
            }
        }
 private void ExecutarRegraMaoDireitaAcimaDaCabeca(SkeletonFrame quadroAtual)
 {
     Skeleton[] esqueletos = new Skeleton[6];
     quadroAtual.CopySkeletonDataTo(esqueletos);
     Skeleton usuario = esqueletos.FirstOrDefault(esqueleto => esqueleto.TrackingState == SkeletonTrackingState.Tracked);
     if (usuario != null)
     {
         Joint maoDireita = usuario.Joints[JointType.HandRight];
         Joint cabeca = usuario.Joints[JointType.Head];
         bool novoTesteMaoDireitaAcimaCabeca = maoDireita.Position.Y > cabeca.Position.Y;
         if (MaoDireitaAcimaCabeca != novoTesteMaoDireitaAcimaCabeca)
         {
             MaoDireitaAcimaCabeca = novoTesteMaoDireitaAcimaCabeca;
             if (MaoDireitaAcimaCabeca)
             {
                 MessageBox.Show("A mão direita está acima da cabeça!");
             }
         }
     }
 }
        void sensorAllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            depthImagePixels = new DepthImagePixel[sensor.DepthStream.FramePixelDataLength];
            using (var frame = e.OpenDepthImageFrame())
            {
                if (frame == null)
                {
                    return;
                }

                frame.CopyDepthImagePixelDataTo(depthImagePixels);
            }

            using (var frame = e.OpenColorImageFrame())
            {
                if (frame == null)
                {
                    return;
                }

                var bitmap = CreateBitmap(frame);
                VideoCanvas.Background = new ImageBrush(bitmap);
            }

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

                var skeletons = new Skeleton[frame.SkeletonArrayLength];
                frame.CopySkeletonDataTo(skeletons);
                var skeleton = skeletons.FirstOrDefault(sk => sk.TrackingState == SkeletonTrackingState.Tracked);
                if (skeleton == null)
                {
                    return;
                }

                var rigthHandPosition = skeleton.Joints[JointType.HandRight].Position;
                var leftHandPosition = skeleton.Joints[JointType.HandLeft].Position;
                var headPosition = skeleton.Joints[JointType.Head].Position;
                var armsPosition = skeleton.Joints[JointType.ShoulderCenter].Position;
                var shoulderLeftPosition = skeleton.Joints[JointType.ShoulderLeft].Position;
                var shoulderRigthPosition = skeleton.Joints[JointType.ShoulderRight].Position;
                var hipCenterPosition = skeleton.Joints[JointType.HipCenter].Position;

                var mapper = new CoordinateMapper(sensor);

                var rightHandCoord = mapper.MapSkeletonPointToColorPoint(rigthHandPosition, ColorImageFormat.RgbResolution640x480Fps30);
                var headCoord = mapper.MapSkeletonPointToColorPoint(headPosition, ColorImageFormat.RgbResolution640x480Fps30);
                var armsCenterCoord = mapper.MapSkeletonPointToColorPoint(armsPosition, ColorImageFormat.RgbResolution640x480Fps30);
                var shoulderLeftCoord = mapper.MapSkeletonPointToColorPoint(shoulderLeftPosition, ColorImageFormat.RgbResolution640x480Fps30);
                var shoulderRightCoord = mapper.MapSkeletonPointToColorPoint(shoulderRigthPosition, ColorImageFormat.RgbResolution640x480Fps30);
                var leftHandCoord = mapper.MapSkeletonPointToColorPoint(leftHandPosition, ColorImageFormat.RgbResolution640x480Fps30);
                var hipCenterCoord = mapper.MapSkeletonPointToColorPoint(hipCenterPosition, ColorImageFormat.RgbResolution640x480Fps30);

                this.DetectGestures(headCoord, rightHandCoord, leftHandCoord, armsCenterCoord, shoulderLeftCoord, shoulderRightCoord, hipCenterCoord);
            }
        }
        private void SkeletonFrameReady_ToggleBackground_Editor(SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
              {
            if (skeletonFrame != null)
            {
              Skeleton[] skeletons = new Skeleton[kinect.SkeletonStream.FrameSkeletonArrayLength];
              skeletonFrame.CopySkeletonDataTo(skeletons);

              Skeleton skeleton = skeletons.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked);
              if (skeleton != null) //if there is a tracked skeleton
              {
            Set_ViewImages_Visibility(Visibility.Hidden); //hide the 2D view images (will show skeleton)
            return;
              }
            }
            Set_ViewImages_Visibility(Visibility.Visible); //if there is no skeletonFrame or tracked skeleton show the 2D view images
              }
        }
        //TODO: refactor into more methods
        private void SkeletonFrameReady_Draw3D(ModelVisual3D modelVisual3D, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
              {
            // Get tracked skeleton data from stream, return if no data
            if (skeletonFrame == null)
            {
              modelVisual3D.Content = null;
              return;
            }

            Skeleton[] skeletons = new Skeleton[kinect.SkeletonStream.FrameSkeletonArrayLength];
            skeletonFrame.CopySkeletonDataTo(skeletons);

            Skeleton skeleton = skeletons.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked);
            if (skeleton == null)
            {
              modelVisual3D.Content = null;
              return;
            }

            // Waist coordinates will be origin
            SkeletonPoint centroid = skeleton.Joints[JointType.HipCenter].Position;

            // Init 3D stuff
            Model3DGroup modelGroup = new Model3DGroup();
            MeshBuilder meshBuilder = new MeshBuilder(false, false);

            // Init dict to tidy up code
            Dictionary<JointType, Point3D> jd = new Dictionary<JointType, Point3D>();

            // Add joints to mesh while populating the dict
            foreach (Joint j in skeleton.Joints)
            {
              // Helix3D has a different coordinate system
              int y = (int)((j.Position.X - centroid.X) * 100);
              int z = (int)((j.Position.Y - centroid.Y) * 100);
              int x = (int)((centroid.Z - j.Position.Z) * 100);
              Point3D center = new Point3D { X = x, Y = y, Z = z };
              jd[j.JointType] = center;
              meshBuilder.AddSphere(center, 5);
            }

            // Add bones to mesh
            meshBuilder.AddCylinder(jd[JointType.Head], jd[JointType.ShoulderCenter], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.ShoulderCenter], jd[JointType.Spine], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.Spine], jd[JointType.HipCenter], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.HipCenter], jd[JointType.HipLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.HipLeft], jd[JointType.KneeLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.KneeLeft], jd[JointType.AnkleLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.AnkleLeft], jd[JointType.FootLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.HipCenter], jd[JointType.HipRight], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.HipRight], jd[JointType.KneeRight], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.KneeRight], jd[JointType.AnkleRight], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.AnkleRight], jd[JointType.FootRight], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.ShoulderCenter], jd[JointType.ShoulderLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.ShoulderLeft], jd[JointType.ElbowLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.ElbowLeft], jd[JointType.WristLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.WristLeft], jd[JointType.HandLeft], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.ShoulderCenter], jd[JointType.ShoulderRight], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.ShoulderRight], jd[JointType.ElbowRight], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.ElbowRight], jd[JointType.WristRight], 6, 10);
            meshBuilder.AddCylinder(jd[JointType.WristRight], jd[JointType.HandRight], 6, 10);

            var mesh = meshBuilder.ToMesh(true); // Create and freeze mesh
            Material blueMaterial = MaterialHelper.CreateMaterial(Colors.SteelBlue); // Create material
            modelGroup.Children.Add(new GeometryModel3D(mesh, blueMaterial)); // Create model

            // Draw
            modelVisual3D.Content = modelGroup;
              }
        }
        //TODO: refactor into smaller methods
        private void SkeletonFrameReady_Detect(SkeletonFrameReadyEventArgs e, HighlightFramesDelegate highlightFrames, DeHighlightFramesDelegate deHighlightFrames, bool keyboardEmulation)
        {
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
              {
            // Get data from stream
            if (skeletonFrame == null)
              return;
            Skeleton[] skeletons = new Skeleton[kinect.SkeletonStream.FrameSkeletonArrayLength];
            skeletonFrame.CopySkeletonDataTo(skeletons);

            // Mind only the tracked, return if none
            Skeleton skeleton = skeletons.FirstOrDefault(s => s.TrackingState == SkeletonTrackingState.Tracked);
            if (skeleton == null)
              return;

            // Init stuff
            SkeletonPoint centroid = skeleton.Joints[JointType.HipCenter].Position;

            // Get the data
            // Check each gesture in collection
            for (int gi = 0; gi < GestureCollection.Count; gi++)
            {
              Gesture g = GestureCollection[gi];
              // Get coordinates of tracked joint
              // SkeletonPoint structure contains X. Y, Z values in meters, relative to the sensor
              // We'll convert that to a Point3D that contains values in cm, relative to the centroid
              SkeletonPoint trackedJoint = skeleton.Joints[g.Joint].Position;
              int jX = (int)((trackedJoint.X - centroid.X) * 100);
              int jY = (int)((trackedJoint.Y - centroid.Y) * 100);
              int jZ = (int)((centroid.Z - trackedJoint.Z) * 100);
              // Check each frame in gesture for collision
              CollisionStates[gi][0] = CollisionStates[gi][1];
              CollisionStates[gi][1] = JointCollisionStates.OutThere;
              for (int fi = 0; fi < g.Frames.Count; fi++)
              {
            GestureFrame f = g.Frames[fi];
            // Check for collision
            foreach (GestureFrameCell fc in f.FrontCells.Where(c => c.IsHotspot))
            {
              if (jX <= fc.RightCM &&
                  jX >= fc.LeftCM &&
                  jY <= fc.TopCM &&
                  jY >= fc.BottomCM)
              {
                foreach (GestureFrameCell sc in f.SideCells.Where(c => c.IsHotspot))
                {
                  if (jZ <= sc.RightCM &&
                      jZ >= sc.LeftCM &&
                      jY <= sc.TopCM &&
                      jY >= sc.BottomCM)
                  {
                    // Record collision
                    CollisionTimes[gi][fi] = DateTime.Now;
                    if (fi + 1 == g.Frames.Count)
                      CollisionStates[gi][1] = JointCollisionStates.InUltimateHotspot;
                    else
                      CollisionStates[gi][1] = JointCollisionStates.InHotspot;
                  }
                }
              }
            }
              }

              // Handle 1-frame gestures
              // Keyboard emulation, List highlight and 3D highlights
              if (g.Frames.Count == 1)
              {
            if (CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot)
            {
              if (keyboardEmulation)
                KeyboardUtils.HitKey(g);
              HighlightGestureOnList(g);
              highlightFrames(g, new List<GestureFrame>() { g.Frames[0] });
            }
            else if (CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot && CollisionStates[gi][1] == JointCollisionStates.OutThere)
            {
              if (keyboardEmulation)
                KeyboardUtils.ReleaseKey(g);
              DeHighlightGestureOnList(g);
              deHighlightFrames(g);
            }
              }

              // Handle multi-frame gestures
              else
              {
            // Keyboard emulation and List highlight
            if (CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot && !(CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot))
            {
              if (keyboardEmulation)
                KeyboardUtils.ReleaseKey(g);
              DeHighlightGestureOnList(g);
            }
            else if (!(CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot) && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot)
            {
              for (int i = 1; i < CollisionTimes[gi].Count; i++)
              {
                if (CollisionTimes[gi][i] - CollisionTimes[gi][i - 1] > CollisionTimeout) break;
                if (i + 1 == CollisionTimes[gi].Count)
                {
                  if (keyboardEmulation)
                    KeyboardUtils.HitKey(g);
                  HighlightGestureOnList(g);
                }
              }
            }

            // 3D highlights
            List<GestureFrame> FramesToHighlight = new List<GestureFrame>();
            if (CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.OutThere)
              deHighlightFrames(g);
            else if (CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.InHotspot)
            {
              if (CollisionTimes[gi].IndexOf(CollisionTimes[gi].Max()) == 0)
                highlightFrames(g, new List<GestureFrame>() { g.Frames[0] });
              else
                deHighlightFrames(g);
            }
            else if ((CollisionStates[gi][0] == JointCollisionStates.OutThere && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot) ||
                     (CollisionStates[gi][0] == JointCollisionStates.InHotspot && CollisionStates[gi][1] == JointCollisionStates.OutThere))
              deHighlightFrames(g);
            else if (CollisionStates[gi][0] == JointCollisionStates.InHotspot && CollisionStates[gi][1] == JointCollisionStates.InHotspot)
              if (CollisionTimes[gi].IndexOf(CollisionTimes[gi].Max()) == 0)
                highlightFrames(g, new List<GestureFrame>() { g.Frames[0] });
              else
              {
                FramesToHighlight = new List<GestureFrame>();
                FramesToHighlight.Add(g.Frames[0]);
                for (int i = 1; i < CollisionTimes[gi].Count; i++)
                {
                  TimeSpan ts = CollisionTimes[gi][i] - CollisionTimes[gi][i - 1];
                  if (ts.Ticks < 0) break;
                  else if (ts > CollisionTimeout)
                  {
                    deHighlightFrames(g);
                    FramesToHighlight = new List<GestureFrame>();
                    break;
                  }
                  else FramesToHighlight.Add(g.Frames[i]);
                }
                highlightFrames(g, FramesToHighlight);
              }
            else if (CollisionStates[gi][0] == JointCollisionStates.InHotspot && CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot)
            {
              FramesToHighlight = new List<GestureFrame>();
              for (int i = 0; i < CollisionTimes[gi].Count - 1; i++)
              {
                TimeSpan ts = CollisionTimes[gi][i + 1] - CollisionTimes[gi][i];
                if (CollisionTimes[gi][i] > CollisionTimes[gi][i + 1] || ts > CollisionTimeout)
                {
                  FramesToHighlight = new List<GestureFrame>();
                  break;
                }
                FramesToHighlight.Add(g.Frames[i]);
                if (!FramesToHighlight.Contains(g.Frames.Last())) FramesToHighlight.Add(g.Frames.Last());
              }
              highlightFrames(g, FramesToHighlight);
            }
            else if (CollisionStates[gi][0] == JointCollisionStates.InUltimateHotspot && !(CollisionStates[gi][1] == JointCollisionStates.InUltimateHotspot))
              deHighlightFrames(g);
              }
            }
              } // using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        }
        private Skeleton FindSkeleton(Skeleton[] skeletons)
        {
            var current = skeletons.FirstOrDefault(skeleton => skeleton.TrackingId == _currentTrackingId);

            if (current != null)
                return current;

            var closest = FindClosestSkeleton(skeletons);

            _currentTrackingId = closest.TrackingId;
            _sensor.SkeletonStream.ChooseSkeletons(_currentTrackingId);

            return closest;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventSender"></param>
        /// <param name="skeletonFrameReadyEventArgs"></param>
        private void onSensorSkeletonFrameReady(object eventSender, SkeletonFrameReadyEventArgs skeletonFrameReadyEventArgs)
        {
            Skeleton[] skeletonArray = null;

            using (SkeletonFrame skeletonFrame = skeletonFrameReadyEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletonArray = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletonArray);

                    Skeleton firstTrackedSkeleton = skeletonArray.FirstOrDefault<Skeleton>(skeleton0 => skeleton0.TrackingState == SkeletonTrackingState.Tracked);

                    if (firstTrackedSkeleton != null)
                    {
                        //use this setter for thread safety
                        this.setTrackedSkeleton(firstTrackedSkeleton);
                    }

                }
            }
        }
        private void SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (var skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                    return;
                var skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);

                inactivityCounter = 0;
                if (skeletons.FirstOrDefault(x => x.TrackingId == player) == null)
                {
                    player = -1;
                    OnKinectLeave(new KinectLeaveEventHandlerArgs {Player = player});
                }
                foreach (var skeleton in skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked))
                {
                    foreach (var joint in skeleton.Joints.Where(j => j.JointType == JointType.HandRight))
                    {
                        // Neuer Spieler gekommen
                        if (player < 0)
                        {
                            player = skeleton.TrackingId;
                            OnKinectEnter(new KinectEnterEventHandlerArgs {Player = player});
                        }

                        // Wenn aktueller Spieler, dann Event auslösen
                        if (player != skeleton.TrackingId)
                            continue;

                        var newZ = joint.Position.Z;
                        var currentZ = (float)handZ.GetAverageValue(newZ);

                        Debug.WriteLine("{0}", newZ - currentZ);

                        if ((oldZ > 0f) && (newZ - currentZ) < -0.1)
                        {
                            handZ.Reset();
                            MessageBeep(0);
                        }

                        oldZ = newZ;

                        var screenPos = ReSizeForScreen(new Point(joint.Position.X, joint.Position.Y));
                        OnKinectPos(new KinectPosEventHandlerArgs {Player = player, Point = screenPos});
                    }
                }
            }
        }
        public Rectangle GetTargetRectangleForZoom(SkeletonFrame skeletonFrame)
        {
            if (skeletonFrame.SkeletonArrayLength < 1) return _fullscreen;
            var skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
            skeletonFrame.CopySkeletonDataTo(skeletons);
            var skeleton = skeletons.FirstOrDefault(x => x.TrackingState != SkeletonTrackingState.NotTracked);
            if (skeleton == null) return _fullscreen;

            var head = skeleton.Joints[JointType.Head];
            var leftHand = skeleton.Joints[JointType.HandLeft];
            var rightHand = skeleton.Joints[JointType.HandRight];
            var leftElbow = skeleton.Joints[JointType.ElbowLeft];
            var rightElbow = skeleton.Joints[JointType.ElbowRight];

            if (head.TrackingState == JointTrackingState.NotTracked &&
                leftHand.TrackingState == JointTrackingState.NotTracked &&
                rightHand.TrackingState == JointTrackingState.NotTracked &&
                leftElbow.TrackingState == JointTrackingState.NotTracked &&
                rightElbow.TrackingState == JointTrackingState.NotTracked)
            {
                return _fullscreen;
            }

            var headPosition = _coordinateMapper.MapSkeletonPointToColorPoint(head.Position, _colorFormat);
            var rightHandPosition = _coordinateMapper.MapSkeletonPointToColorPoint(rightHand.Position, _colorFormat);
            var rightElbowPosition = _coordinateMapper.MapSkeletonPointToColorPoint(rightElbow.Position, _colorFormat);
            var leftHandPosition = _coordinateMapper.MapSkeletonPointToColorPoint(leftHand.Position, _colorFormat);
            var leftElbowPosition = _coordinateMapper.MapSkeletonPointToColorPoint(leftElbow.Position, _colorFormat);
            headPosition.Y -= 50;

                        var targetRectangleFromLeftHand = GetTargetRectForHand(headPosition, leftHandPosition, leftElbowPosition,
                "Left");
            var targetRectangleFromRightHand = GetTargetRectForHand(headPosition, rightHandPosition, rightElbowPosition,
                "Right");

            if (targetRectangleFromRightHand == Rectangle.Empty && targetRectangleFromLeftHand == Rectangle.Empty)
            {
                return _fullscreen;
            }

            Rectangle targetRectangle;
            if (targetRectangleFromRightHand != Rectangle.Empty && targetRectangleFromLeftHand == Rectangle.Empty)
            {
                targetRectangle = targetRectangleFromRightHand;
            }
            else if (targetRectangleFromLeftHand != Rectangle.Empty && targetRectangleFromRightHand == Rectangle.Empty)
            {
                targetRectangle = targetRectangleFromLeftHand;
            }
            else
            {
                targetRectangle = Rectangle.FromLTRB(
                    Math.Min(targetRectangleFromLeftHand.Left, targetRectangleFromRightHand.Left),
                    Math.Min(targetRectangleFromLeftHand.Top, targetRectangleFromRightHand.Top),
                    Math.Max(targetRectangleFromLeftHand.Right, targetRectangleFromRightHand.Right),
                    Math.Max(targetRectangleFromLeftHand.Bottom, targetRectangleFromRightHand.Bottom));
            }

            if (targetRectangle.Width > targetRectangle.Height)
            {
                targetRectangle.Height = (int)Math.Floor(targetRectangle.Width * _aspectRatio);
            }
            else
            {
                targetRectangle.Width = (int)Math.Floor(targetRectangle.Height * _aspectRatio);
            }

            if (targetRectangle.X < 0) targetRectangle.X = 0;
            if (targetRectangle.Y < 0) targetRectangle.Y = 0;
            if (targetRectangle.Right > _fullscreen.Right)
                targetRectangle.X -= targetRectangle.Right - _fullscreen.Right;
            if (targetRectangle.Bottom > _fullscreen.Bottom)
                targetRectangle.Y -= targetRectangle.Bottom - _fullscreen.Bottom;

            return Rectangle.FromLTRB(
                Math.Max(targetRectangle.Left, _fullscreen.Left),
                Math.Max(targetRectangle.Top, _fullscreen.Top),
                Math.Min(targetRectangle.Right, _fullscreen.Right),
                Math.Min(targetRectangle.Bottom, _fullscreen.Bottom));
        }
Beispiel #14
0
        /// <summary>
        /// Store valid Skeletons with TrackingId as Key from last 20 Frames in
        /// a Dictionary. Newest Frame from a Skeleton is at Index 0.
        /// </summary>
        public void TrackSkeletons(Skeleton[] skeletons)
        {
            if (skeletons!=null){
                foreach (Skeleton skeleton in skeletons)
                {
                    if (skeleton.TrackingState != SkeletonTrackingState.NotTracked)
                    {
                        if (_skeletonsDict.ContainsKey(skeleton.TrackingId))
                        {
                            List<SkeletonTimestamp> skeletonList = _skeletonsDict[skeleton.TrackingId];
                            if (skeletonList.Count >= _maxNumberOfFramesInSkeletonList)
                            {
                                skeletonList.RemoveAt(skeletonList.Count - 1);
                            }
                            skeletonList.Insert(0, new SkeletonTimestamp(skeleton));
                        }
                        else
                        {
                            List<SkeletonTimestamp> skeletonList = new List<SkeletonTimestamp>();
                            skeletonList.Add(new SkeletonTimestamp(skeleton));
                            _skeletonsDict.Add(skeleton.TrackingId, skeletonList);
                        }
                    }
                }
            }

            // remove deprecated TrackingIds from Dictionary
            if (_skeletonsDict.Count > 0)
            {
                List<int> removeSkeletonList = new List<int>();
                foreach (KeyValuePair<int, List<SkeletonTimestamp>> dictEntry in _skeletonsDict)
                {
                    Skeleton s = skeletons.FirstOrDefault((skeleton) => skeleton.TrackingId == dictEntry.Key);
                    if (s == null)
                    {
                        removeSkeletonList.Add(dictEntry.Key);
                    }
                }
                foreach (int index in removeSkeletonList)
                {
                    _skeletonsDict.Remove(index);
                }
            }
        }