Пример #1
0
        private Skeleton GetSkeletonForPlayer(int player)
        {
            Skeleton[] skeletons;

            using (SkeletonFrame skeletonFrame = this._skeletonFrameEventArgs.OpenSkeletonFrame())
            {
                if (skeletonFrame == null)
                {
                    throw new SkeletonFrameDataNotAvailableException();
                }

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

            var tracked = skeletons
                .Where(skeleton => skeleton.TrackingState == SkeletonTrackingState.Tracked);

            if (!tracked.Any())
            {
                throw new Exception("No players found!");
            }

            return tracked.ElementAtOrDefault(player);
        }
Пример #2
0
        static void Sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (var frame = e.OpenSkeletonFrame())
            {
                if (frame != null)
                {
                    Microsoft.Kinect.Skeleton[] skeletons = new Microsoft.Kinect.Skeleton[frame.SkeletonArrayLength];

                    frame.CopySkeletonDataTo(skeletons);

                    if (skeletons.Length > 0)
                    {
                        var user = skeletons.Where(u => u.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();

                        if (user != null)
                        {
                            if (!_skeleton)
                            {
                                Console.WriteLine("Skeleton detected");
                            }
                            _skeleton = true;
                            _mainGesture.Update(user);
                        }
                        else
                        {
                            if (_skeleton)
                            {
                                Console.WriteLine("No Skeleton detected");
                            }
                            _skeleton = false;
                        }
                    }
                }
            }
        }
        public static Skeleton ObterEsqueletoUsuario(this SkeletonFrame quadro)
        {
            Skeleton esqueletoUsuario = null;
            Skeleton[] esqueletos = new Skeleton[quadro.SkeletonArrayLength];
            quadro.CopySkeletonDataTo(esqueletos);
            IEnumerable<Skeleton> esqueletosRastreados = esqueletos.Where(esqueleto => esqueleto.TrackingState == SkeletonTrackingState.Tracked);
            if (esqueletosRastreados.Count() > 0)
                esqueletoUsuario = esqueletosRastreados.First();

            return esqueletoUsuario;
        }
Пример #4
0
        static void Main(string[] args)
        {
            var sensor = Microsoft.Kinect.KinectSensor.KinectSensors.FirstOrDefault(sens => sens.Status == KinectStatus.Connected);
            if (sensor == null)
                return;
            sensor.SkeletonStream.Enable();
            var skeletonData = new Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
            sensor.SkeletonFrameReady += (o, arg) =>
            {
                using (SkeletonFrame frame = arg.OpenSkeletonFrame())
                {
                    if (frame == null || skeletonData == null)
                        return;
                    frame.CopySkeletonDataTo(skeletonData);
                    foreach (var skeleton in skeletonData)
                    {

                    }
                }
            };
            sensor.Start();
            sensor.ElevationAngle = 0;
            sensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Default;

            Vector<double> p0 = null, p1 = null, p2 = null;
            Func<Vector<double>, Vector<double>> transform = null;
            Func<Vector<double>> readRightHand = () => skeletonData.Where(row => row.TrackingState == SkeletonTrackingState.Tracked).Select(row => row.Joints[JointType.HandRight]).Select(row => new DenseVector(new double[] { row.Position.X, row.Position.Y, row.Position.Z })).FirstOrDefault();
            while (true)
            {
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.D0)
                    p0 = readRightHand();
                if (key.Key == ConsoleKey.D1)
                    p1 = readRightHand();
                if (key.Key == ConsoleKey.D2)
                    p2 = readRightHand();
                if (p0 != null && p1 != null && p2 != null)
                {
                    transform = WorldTransform.GenerateTransform(p0, p1, p2);
                }
                if (transform != null)
                {
                    var transformed = transform(readRightHand());
                    Console.Clear();
                    Console.WriteLine("Transformed: " + PosToString(transformed));
                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("Untransformed: " + PosToString(readRightHand()));
                }
            }
        }
 /// <summary>
 /// Creates skeleton models for each of the tracked skeleton in the array
 /// </summary>
 /// <param name="skeletons">Array of all found skeletons</param>
 /// <param name="brush">Color of the skeleton</param>
 /// <param name="sensor">The sensor.</param>
 /// <param name="width">The width of the canvas.</param>
 /// <param name="height">The height of the canvas.</param>
 public void DrawSkeleton(Skeleton[] skeletons, Brush brush, KinectSensor sensor, double width, double height)
 {
     var skeletonModels = new ObservableCollection<Polyline>();
     foreach (var skeleton in skeletons.Where(skeleton => skeleton.TrackingState != SkeletonTrackingState.NotTracked))
     {
         skeletonModels.Add(CreateFigure(skeleton, brush, CreateBody(), sensor, width, height));
         skeletonModels.Add(CreateFigure(skeleton, brush, CreateLeftHand(), sensor, width, height));
         skeletonModels.Add(CreateFigure(skeleton, brush, CreateRightHand(), sensor, width, height));
         skeletonModels.Add(CreateFigure(skeleton, brush, CreateLeftLeg(), sensor, width, height));
         skeletonModels.Add(CreateFigure(skeleton, brush, CreateRightLeg(), sensor, width, height));
     }
     SkeletonParts = skeletonModels;
 }
Пример #6
0
        protected void onSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    Skeleton[] squelettes = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(squelettes);

                    if (canvas.Children.Count > 2)
                        canvas.Children.RemoveRange(1, canvas.Children.Count - 1);

                    foreach (Skeleton squelette in squelettes.Where(skel => skel.TrackingState == SkeletonTrackingState.Tracked))
                    {
                        drawBone(squelette, canvas, JointType.Head, JointType.ShoulderCenter, Brushes.AliceBlue);

                        drawBone(squelette, canvas, JointType.ShoulderCenter, JointType.ShoulderLeft, Brushes.AliceBlue);
                        drawBone(squelette, canvas, JointType.ShoulderCenter, JointType.ShoulderRight, Brushes.AliceBlue);

                        drawBone(squelette, canvas, JointType.ShoulderCenter, JointType.Spine, Brushes.AliceBlue);

                        drawBone(squelette, canvas, JointType.Spine, JointType.HipCenter, Brushes.AliceBlue);

                        drawBone(squelette, canvas, JointType.HipCenter, JointType.HipLeft, Brushes.AliceBlue);
                        drawBone(squelette, canvas, JointType.HipCenter, JointType.HipRight, Brushes.AliceBlue);

                        drawBone(squelette, canvas, JointType.ShoulderLeft, JointType.ElbowLeft, Brushes.AliceBlue);
                        drawBone(squelette, canvas, JointType.ElbowLeft, JointType.WristLeft, Brushes.AliceBlue);
                        drawBone(squelette, canvas, JointType.WristLeft, JointType.HandLeft, Brushes.AliceBlue);

                        drawBone(squelette, canvas, JointType.ShoulderRight, JointType.ElbowRight, Brushes.AliceBlue);
                        drawBone(squelette, canvas, JointType.ElbowRight, JointType.WristRight, Brushes.AliceBlue);
                        drawBone(squelette, canvas, JointType.WristRight, JointType.HandRight, Brushes.AliceBlue);

                        // Left Leg
                        this.drawBone(squelette, canvas, JointType.HipLeft, JointType.KneeLeft, Brushes.AliceBlue);
                        this.drawBone(squelette, canvas, JointType.KneeLeft, JointType.AnkleLeft, Brushes.AliceBlue);
                        this.drawBone(squelette, canvas, JointType.AnkleLeft, JointType.FootLeft, Brushes.AliceBlue);

                        // Right Leg
                        this.drawBone(squelette, canvas, JointType.HipRight, JointType.KneeRight, Brushes.AliceBlue);
                        this.drawBone(squelette, canvas, JointType.KneeRight, JointType.AnkleRight, Brushes.AliceBlue);
                        this.drawBone(squelette, canvas, JointType.AnkleRight, JointType.FootRight, Brushes.AliceBlue);
                    }
                }
            }
        }
        private void DesenharEsqueletoUsuario(SkeletonFrame quadro)
        {
            if (quadro == null) return;

            using (quadro)
            {
                Skeleton[] esqueletos = new Skeleton[quadro.SkeletonArrayLength];
                quadro.CopySkeletonDataTo(esqueletos);
                IEnumerable<Skeleton> esqueletosRastreados = esqueletos.Where(esqueleto => esqueleto.TrackingState == SkeletonTrackingState.Tracked);
                if (esqueletosRastreados.Count() > 0)
                {
                    Skeleton esqueleto = esqueletosRastreados.First();
                    EsqueletoUsuarioAuxiliar funcoesEsqueletos = new EsqueletoUsuarioAuxiliar(kinect);
                    funcoesEsqueletos.DesenharArticulacao(esqueleto.Joints[JointType.HandRight], canvasKinect);
                    funcoesEsqueletos.DesenharArticulacao(esqueleto.Joints[JointType.HandLeft], canvasKinect);
                }
            }
        }
Пример #8
0
        // Process the new skeleton data
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    // Copy the skeleton data to the "skeletons" array.
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);

                    // Cull off the non tracked skeletons.
                    skeletons = skeletons.Where(x => x.TrackingState == SkeletonTrackingState.Tracked).ToArray();

                    // Detect high fives between pairs of skeletons.
                    detectHighFives(skeletons);
                }
            }
        }
Пример #9
0
        static void Sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (var frame = e.OpenSkeletonFrame())
            {
                if (frame != null)
                {
                    Skeleton[] skeletons = new Skeleton[frame.SkeletonArrayLength];

                    frame.CopySkeletonDataTo(skeletons);

                    if (skeletons.Length > 0)
                    {
                        var user = skeletons.Where(u => u.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();

                        if (user != null)
                        {
                            //_gesture.Update(user);
                        }
                    }
                }
            }
        }
        private Skeleton FindClosestSkeleton(Skeleton[] skeletons)
        {
            var seed = new
            {
                Distance = float.PositiveInfinity,
                Skeleton = new Skeleton()
            };

            var result = skeletons.Where(skeleton => skeleton.TrackingState != SkeletonTrackingState.NotTracked)
                .Aggregate(seed, (running, cur) =>
            {
                if (cur.Position.Z >= running.Distance)
                    return running;

                return new
                {
                    Distance = cur.Position.Z,
                    Skeleton = cur
                };
            });

            return result.Skeleton;
        }
Пример #11
0
        public static void Run(string[] args)
        {
            var camfile = args.FirstOrDefault() ?? Calibration.KinectDefaultFileName;
            var projfile = args.Skip(1).FirstOrDefault() ?? Calibration.ProjectorDefaultFileName;
            if (!File.Exists(camfile) || !File.Exists(projfile))
            {
                Console.WriteLine("Either calib file could not be found.");
                return;
            }
            var cc = Utils.DeSerializeObject<CalibrationResult>(camfile);
            var pc = Utils.DeSerializeObject<CalibrationResult>(projfile);

            var window = ProgramWindow.OpenOnSecondary();

            var program = new PointCloudProgram(29f);
            window.SetProgram(program);
            program.Draw().All((xp, yp) =>
            {
                var x = 0.5 - xp;
                var y = 0.5 - yp;
                var i = Math.Sqrt(x * x + y * y) * 2.5;
                if (i > 1)
                    i = 1;
                i = Math.Pow(1 - i, 3);
                byte ii = (byte)(i * 255);
                return Color.FromArgb(ii, 255, 255, 255);
            }).Finish();

            KinectSensor sensor = KinectSensor.KinectSensors.First();
            var format = DepthImageFormat.Resolution80x60Fps30;
            DepthCamera depthCam = new DepthCamera(sensor, format);
            SkeletonCamera skeletonCam = new SkeletonCamera(sensor);
            TriplexCamera triplex = new TriplexCamera(depthCam, skeletonCam);
            KinectCalibrator kc = new KinectCalibrator(cc);
            sensor.Start();
            sensor.SkeletonStream.Enable();
            program.SetProjection(pc, kc.GetModelView());
            Rendering rend = new Rendering(program);

            float dt = 0;
            {
                float[] hand = new float[] { 0.124f, -0.9f, 1.7f, 1f };
                {
                    var t = program.SetLight0Pos(hand);
                    Console.Write("({0}, {1}, {2})  ", hand[0], hand[1], hand[2]);
                    Console.WriteLine("({0}, {1}, {2})", t.X, t.Y, t.Z);
                }
                window.KeyPress += (o, e) =>
                {
                    if (e.KeyChar == 'w')
                        hand[1] += 0.1f;
                    else if (e.KeyChar == 's')
                        hand[1] -= 0.1f;
                    if (e.KeyChar == 'a')
                        hand[0] += 0.1f;
                    else if (e.KeyChar == 'd')
                        hand[0] -= 0.1f;
                    if (e.KeyChar == 'r')
                        hand[2] += 0.1f;
                    else if (e.KeyChar == 'f')
                        hand[2] -= 0.1f;
                    var t = program.SetLight0Pos(hand);
                    Console.Write("({0}, {1}, {2})  ", hand[0], hand[1], hand[2]);
                    Console.WriteLine("({0}, {1}, {2})", t.X, t.Y, t.Z);
                };
            }

            while (true)
            {
                var players = triplex.Trigger(1000);
                if (players == null)
                    continue;
                rend.Render(players, sensor, format);
                window.RenderFrame();
                window.ProcessEvents();
            }

            //program.SetPositions(new Vector3[] { new Vector3(0.0f, -0.1f, 0) });
            //window.RenderFrame();

            Func<float[]> getHand = () =>
            {
                using (var frame = sensor.SkeletonStream.OpenNextFrame(100))
                {
                    if (frame == null)
                        return null;
                    Skeleton[] skeletons = new Skeleton[frame.SkeletonArrayLength];
                    frame.CopySkeletonDataTo(skeletons);
                    var rhand = skeletons.Where(sk => sk.TrackingState == SkeletonTrackingState.Tracked)
                        .SelectMany(sk => sk.Joints).Where(j => j.TrackingState != JointTrackingState.NotTracked)
                        .Where(j => j.JointType == JointType.HandRight)
                        .Select(j => j.Position).ToArray();
                    if (rhand.Length > 0)
                        return kc.ToGlobal(rhand.First());
                    else
                        return null;
                }
            };

            while (true)
            {
                var test = depthCam.GetDepth(10000);
                if (test == null)
                    continue;
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                var points = test.Select(f => sensor.CoordinateMapper.MapDepthPointToSkeletonPoint(format, f))
                    .Select(p => new Vector3(p.X, p.Y, p.Z)).ToArray();
                program.SetPositions(points);
                window.RenderFrame();
                sw.Stop();
                Console.WriteLine("Time: " + sw.ElapsedMilliseconds);
                window.ProcessEvents();

                //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                //sw.Start();
                //var sp = test.Select(f => sensor.CoordinateMapper.MapDepthPointToSkeletonPoint(format, f));
                //var globals = sp.Select(p => new float[] { p.X, p.Y, p.Z, 1 }).ToArray();
                ////var globals = kc.ToGlobal(sp).Where(g => g[2] > 0.1f).ToArray();
                //sw.Stop();
                //Console.WriteLine("Time: " + sw.ElapsedMilliseconds);
                //program.SetLight0Pos(new float[] { 0.16f, -0.14f, 0.24f, 1 });

                //var hand = getHand();
                //if (hand != null)
                //{
                //    var t = program.SetLight0Pos(hand);
                //    Console.Write("({0}, {1}, {2})  ", hand[0], hand[1], hand[2]);
                //    Console.WriteLine("({0}, {1}, {2})", t.X, t.Y, t.Z);
                //    //Console.Write("Done ");
                //}

                //program.SetPositions(globals);
                //window.RenderFrame();
                //window.ProcessEvents();
                //{
                //    var projected = pc.Transform(globals.Where(g => g[2] > -0.0f).ToArray());
                //    projector.DrawPoints(projected,2, Color.Gray);
                //}
            }
        }
Пример #12
0
        private void ExtractValidSkeletons(Skeleton[] rawSkeletons)
        {
            var validSkeletons = rawSkeletons.Where(
            skeleton => skeleton.TrackingState == SkeletonTrackingState.Tracked &&
            skeleton.Joints.Count(joint => joint.TrackingState == JointTrackingState.Tracked) > Constants.MINIMUM_JOINT_THRESHOLD
              );

              foreach (Skeleton skeleton in validSkeletons)
              {
            if (currentVisitors.ContainsKey(skeleton.TrackingId))
              currentVisitors[skeleton.TrackingId].Update(skeleton);
            else
              currentVisitors.Add(skeleton.TrackingId, new WagSkeleton(skeleton));

            WagFaceTracker wagFaceTracker = wagFaceTrackers.SingleOrDefault(tracker => tracker.SkeletonId == skeleton.TrackingId);
            if (wagFaceTracker == null)
            {
              wagFaceTracker = wagFaceTrackers.FirstOrDefault(tracker => tracker.IsUsing == false);
              wagFaceTracker.SkeletonId = skeleton.TrackingId;
              wagFaceTracker.IsUsing = true;
            }

            currentVisitors[skeleton.TrackingId].LastFrameSeen = currentFrame;

            Calibration.ApplyTransformations(currentVisitors[skeleton.TrackingId]);
              }

              foreach (WagSkeleton wagSkeleton in currentVisitors.Values)
              {
            wagSkeleton.FramesNotSeen = currentFrame - wagSkeleton.LastFrameSeen;
              }

              OnPropertyChanged("ClosestVisitor");
        }
Пример #13
0
        protected int TrackNearerSkeleton(Skeleton[] skeletonData)
        {
            float closestDistance = 10000f; // Start with a far enough distance
            int closestID = 0;

            foreach (Skeleton skeleton in skeletonData.Where(s => s.TrackingState != SkeletonTrackingState.NotTracked))
            {
                if (skeleton.Position.Z < closestDistance)
                {
                    closestID = skeleton.TrackingId;
                    closestDistance = skeleton.Position.Z;
                }
            }

            if (closestID > 0)
            {
                kinectSensor.SkeletonStream.ChooseSkeletons(closestID); // Track this skeleton
            }

            return closestID;
        }
        /// <summary>
        /// Looks for the closest skeleton
        /// </summary>
        /// <param name="skeletons">All skeletons recognised by Kinect</param>
        /// <returns>The skeleton closestto the sensor</returns>
        public static Skeleton GetPrimarySkeleton(Skeleton[] skeletons)
        {
            Skeleton skeleton = null;

            if (skeletons != null)
                foreach (Skeleton skelet in skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked))
                    if (skeleton == null || skelet.Position.Z < skeleton.Position.Z)
                        skeleton = skelet;

            return skeleton;
        }
Пример #15
0
        private Skeleton getSkeletonImage(SkeletonFrame frame)
        {
            Skeleton[] skeletonArray = new Skeleton[6];
            frame.CopySkeletonDataTo(skeletonArray);

            var trackedSkeletons = skeletonArray.Where(s => s.TrackingState == SkeletonTrackingState.Tracked);
            var skeleton = trackedSkeletons.FirstOrDefault();
            return skeleton;
        }
Пример #16
0
 private void SkeletonDataReadyHandler(object sender, SkeletonFrameReadyEventArgs e)
 {
     // no active user, ignore events
     if (_activeUser == -1 || e == null || _kinect == null) return;
     // not recording and no context, ignore events.
     var sFrame = e.OpenSkeletonFrame();
     if (sFrame == null) return;
     var skeletons = new Skeleton[_kinect.GetSensor().SkeletonStream.FrameSkeletonArrayLength];
     sFrame.CopySkeletonDataTo(skeletons);
     sFrame.Dispose();
     foreach (var skeleton in skeletons.Where(skeleton => skeleton != null && skeleton.TrackingId == _activeUser))
     {
         Skeleton2DDataExtract.ProcessData(skeleton, _defaultHand);
         if (!_isRecording)
             if (_pointer != null) _pointer.FindContext(skeleton);
         if (_addOnGesture)
         {
             AddOnGesture(_addOnGestureType, skeleton);
         }
     }
 }
Пример #17
0
        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;

                }
            }
        }
Пример #18
0
        private void processSkeleton(SkeletonFrame frame)
        {
            Skeleton[] skeletons = new Skeleton[frame.SkeletonArrayLength];
            frame.CopySkeletonDataTo(skeletons);

            //      Console.WriteLine("Skeletons:  " + skeletons.Where(S => S.TrackingState == SkeletonTrackingState.Tracked).Count() );

            Skeleton person = null;
            if ( (person = skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault()) != null)
            {
            Console.WriteLine("Doing it, doing it!!");
            double person_core_x = Math.Round(person.Position.X, 1);
            double person_core_y = Math.Round(person.Position.Y, 1);
            double person_hand_l_x = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandLeft).First().Position.X, 2);
            double person_hand_l_y = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandLeft).First().Position.Y, 2);
            double person_hand_r_x = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandRight).First().Position.X, 2);
            double person_hand_r_y = Math.Round(person.Joints.Where(Joint => Joint.JointType == JointType.HandRight).First().Position.Y, 2);

            label3.Content = (
                "Sensor: " + Math.Round(this.x_actual,2) + ", " + Math.Round(this.y_actual,2) +
                " Core: " + person_core_x + ", " + person_core_y +
                " LH: " + person_hand_l_x + ", " + person_hand_l_y +
                " RH: " + person_hand_r_x + ", " + person_hand_r_y
                );
            }
            else
            {
            Console.WriteLine("Totally gone wrong.");
            this.label3.Content = (
                "Sensor: " + Math.Round(this.x_actual,1) + ", " + Math.Round(this.y_actual,1) + " - no skeletons found"
                );
            }
        }
Пример #19
0
        private static void SkeletonFramesReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame())
            {
                if (skeletonFrameData != null)
                {
                    var allSkeletons = new Skeleton[skeletonFrameData.SkeletonArrayLength];

                    skeletonFrameData.CopySkeletonDataTo(allSkeletons);

                    List<Skeleton> tracked =
                        allSkeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).ToList();
                    if (tracked.Any())
                    {
                        // Find the closest skeleton
                        Skeleton closest = tracked.OrderBy(t => t.Position.Z).First();

                        // Get the head x position
                        float headX = closest.Joints
                            .Where(j => j.JointType == JointType.Head)
                            .Select(h => h.Position.X)
                            .FirstOrDefault();

                        // Broadcast to subject
                        HeadXSubject.OnNext(headX);
                    }
                }
            }
        }
Пример #20
0
        private static int TrackClosestSkeleton(KinectSensor sensor, Skeleton[] skeletons)
        {
            if (sensor != null && sensor.SkeletonStream != null)
            {
                if (!sensor.SkeletonStream.AppChoosesSkeletons)
                {
                    sensor.SkeletonStream.AppChoosesSkeletons = true; // Ensure AppChoosesSkeletons is set
                }

                SkeletonPoint camera = new SkeletonPoint();
                camera.X = camera.Y = camera.Z = 0;

                double closestDistance = 10000f; // Start with a far enough distance

                int closestID = 0;

                foreach (Skeleton skeleton in skeletons.Where(s => s.TrackingState != SkeletonTrackingState.NotTracked))
                {
                    double dist = Utilities.Distance(camera, skeleton.Position);
                    if (dist < closestDistance)
                    {
                        closestID = skeleton.TrackingId;
                        closestDistance = dist;
                    }
                }

                if (closestID > 0)
                {
                    sensor.SkeletonStream.ChooseSkeletons(closestID); // Track this skeleton
                }

                return closestID;
            }
            return -1;
        }
Пример #21
0
        void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    Skeleton[] skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];

                    //skeletonFrame.CopySkeletonDataTo(skeletons);
                    skeletonFrame.CopySkeletonDataTo(skeletonData);

                    trackedSkeleton = skeletonData.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
                    Skeleton playerSkeleton = (from s in skeletonData where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault();
                    if (skeletonData == null)
                    {
                        skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    }
                    if (playerSkeleton != null)
                    {
                        Joint body = playerSkeleton.Joints[JointType.Spine];
                        bodyPositionPixels = new Vector2((((0.5f * body.Position.X) + 0.5f) * (640)), (((-0.5f * body.Position.Y) + 0.5f) * (480)));

                        if (body.Position.X > 0.1)
                        {
                            Console.WriteLine("Moving Right");
                            scorpionduck = false;
                            Sleft = false;
                            Sright = true;
                        }
                        if (body.Position.X < -0.1)
                        {
                            Console.WriteLine("Moving Left");
                            scorpionduck = false;
                            Sright = false;
                            Sleft = true;
                        }
                        if (body.Position.Y < 0)
                        {
                            Console.WriteLine("Ducking");
                            Sright = false;
                            Sleft = false;
                            scorpionduck = true;
                        }
                        if (body.Position.X > -0.1 && body.Position.X < 0.1)
                        {
                            Sright = false;
                            Sleft = false;
                            scorpionduck = false;
                            Sidle = true;
                        }
                    }  
                }
            }
        }
        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});
                    }
                }
            }
        }
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

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

                        if (skeletons != null)
                        {
                            Skeleton firstTrackedSkeleton = skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
                            if (firstTrackedSkeleton != null)
                            {
                                //MessageBox.Show("Recording Gesture Now", "Result");

                                Joint j = firstTrackedSkeleton.Joints[JointType.HandRight];
                                Joint jleft = firstTrackedSkeleton.Joints[JointType.HandLeft];

                                if (j.TrackingState == JointTrackingState.Tracked && jleft.TrackingState == JointTrackingState.Tracked)
                                {

                                    if (trainmode == 0)
                                    {
                                        button1.Visibility = Visibility.Hidden;
                                        if (restarttesting == 0)
                                            return;

                                        int starttracking = 0;
                                        if (timerstarted == 0)
                                        {
                                            sw.Start(); // starts the stopwatch
                                            label3.Content = "";

                                            label3.Visibility = Visibility.Visible;
                                            timerstarted = 1;
                                        }
                                        else
                                        {
                                            long elapsed = sw.ElapsedMilliseconds;
                                            if (elapsed < 5000)
                                            {
                                                long remainingtime = 5000 - elapsed;
                                                label3.Content = "Recording in\n" + remainingtime / 1000 + " seconds";
                                            }
                                            else
                                            {
                                                //timerstarted = 0;
                                                starttracking = 1;
                                                label3.Visibility = Visibility.Hidden;
                                                sw.Stop();
                                                //sw.Reset();
                                            }

                                        }
                                        if (starttracking == 0)
                                        {
                                            return;
                                        }
                                        recordFlag = 1;
                                    }
                                    else
                                    {
                                        button1.Visibility = Visibility.Visible;
                                    }

                                   // Console.WriteLine("Right hand: " + j.Position.X + ", " + j.Position.Y + ", " + j.Position.Z);

                                    String lines = j.Position.X + ", " + j.Position.Y + ", " + j.Position.Z;
                                    String filename,filename2;
                                    Boolean fileappend = false ;
                                    if (trainmode == 1)
                                    {
                                        filename = "./train\\right\\" + trainname;
                                        filename2 = "./train\\left\\" + trainname;
                                        fileappend = true;
                                    }
                                    else
                                    {
                                        filename = "test";
                                        filename2 = "testleft";
                                        fileappend = false;
                                    }

                                   // textBox1.Text = "Right hand: " + lines;
                                    if (recordFlag == 1)
                                    {
                                        xString = xString + " " + j.Position.X*1000;
                                        yString = yString + " " + j.Position.Y*1000;
                                        zString = zString + " " + j.Position.Z*1000;
                                        xStringl = xStringl + " " + jleft.Position.X * 1000;
                                        yStringl = yStringl + " " + jleft.Position.Y * 1000;
                                        zStringl = zStringl + " " + jleft.Position.Z * 1000;
                                        frameCount++;
                                        label2.Content = frameCount * 2;

                                        if (frameCount == 60)
                                        {
                                            frameCount = 0;
                                            recordFlag = 0;
                                            System.IO.StreamWriter filex = new System.IO.StreamWriter( filename+"_x.txt", fileappend);
                                            filex.WriteLine(xString);
                                            filex.Close();

                                            System.IO.StreamWriter filey = new System.IO.StreamWriter(filename + "_y.txt", fileappend);
                                            filey.WriteLine(yString);
                                            filey.Close();

                                            System.IO.StreamWriter filez = new System.IO.StreamWriter(filename + "_z.txt", fileappend);
                                            filez.WriteLine(zString);
                                            filez.Close();

                                            System.IO.StreamWriter filex2 = new System.IO.StreamWriter(filename2 + "_x.txt", fileappend);
                                            filex2.WriteLine(xStringl);
                                            filex2.Close();

                                            System.IO.StreamWriter filey2 = new System.IO.StreamWriter(filename2 + "_y.txt", fileappend);
                                            filey2.WriteLine(yStringl);
                                            filey2.Close();

                                            System.IO.StreamWriter filez2 = new System.IO.StreamWriter(filename2 + "_z.txt", fileappend);
                                            filez2.WriteLine(zStringl);
                                            filez2.Close();

                                            xString = "";
                                            yString = "";
                                            zString = "";
                                            xStringl = "";
                                            yStringl = "";
                                            zStringl = "";

                                            testgesture();

                                        }
                                    }
                                }
                            }
                        }

                }
            }

            using (DrawingContext dc = this.drawingGroup.Open())
            {
                // Draw a transparent background to set the render size
                dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));

                if (skeletons.Length != 0)
                {
                    foreach (Skeleton skel in skeletons)
                    {
                        RenderClippedEdges(skel, dc);

                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            this.DrawBonesAndJoints(skel, dc);
                        }
                        else if (skel.TrackingState == SkeletonTrackingState.PositionOnly)
                        {
                            dc.DrawEllipse(
                            this.centerPointBrush,
                            null,
                            this.SkeletonPointToScreen(skel.Position),
                            BodyCenterThickness,
                            BodyCenterThickness);
                        }
                    }
                }

                // prevent drawing outside of our render area
                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));
            }
        }
Пример #24
0
        void Sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            using (var frame = e.OpenSkeletonFrame())
            {
                if (frame != null)
                {
                    Skeleton[] bodies = new Skeleton[frame.SkeletonArrayLength];

                    frame.CopySkeletonDataTo(bodies);

                    var body = bodies.Where(b => b.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();

                    if (body != null)
                    {
                        Joint handLeft = body.Joints[JointType.HandLeft];
                        Joint handRight = body.Joints[JointType.HandRight];

                        if (handLeft.TrackingState != JointTrackingState.NotTracked && handRight.TrackingState != JointTrackingState.NotTracked)
                        {
                            // Select the hand that is closer to the sensor.
                            var activeHand = handRight.Position.Z <= handLeft.Position.Z ? handRight : handLeft;
                            var position = _sensor.CoordinateMapper.MapSkeletonPointToColorPoint(activeHand.Position, ColorImageFormat.RgbResolution640x480Fps30);

                            cursor.Flip(activeHand);                            
                            cursor.Update(position);
                        }
                    }
                }
            }
        }
Пример #25
0
        private void KinectSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

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

                    Skeleton skel = skeletons.Where(s => s.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();
                    if (skel != null)
                    {
                        if (skeletonReady != null)
                        {
                            skeletonReady(skel);
                        }
                    }
                }
            }
        }
Пример #26
0
        private void TrackClosestSkeleton(Skeleton[] skeletons)
        {
            int closestID = 0;

            if (this.sensor != null && this.sensor.SkeletonStream != null)
            {
                if (!this.sensor.SkeletonStream.AppChoosesSkeletons)
                {
                    this.sensor.SkeletonStream.AppChoosesSkeletons = true; // Ensure AppChoosesSkeletons is set
                }

                float closestDistance = 10000f; // Start with a far enough distance
                Skeleton closestSkeleton = null;

                foreach (Skeleton skeleton in skeletons.Where(s => s.TrackingState != SkeletonTrackingState.NotTracked))
                {
                    if (skeleton.Position.Z < closestDistance)
                    {
                        closestID = skeleton.TrackingId;
                        closestDistance = skeleton.Position.Z;
                        closestSkeleton = skeleton;
                    }
                }

                if (closestID > 0)
                {
                    if (currentSkeleton == null || currentSkeleton.TrackingId != closestSkeleton.TrackingId)
                    {
                        startDelayTimer.Restart();
                        leftHandChecker.Initialize(closestSkeleton);
                        rightHandChecker.Initialize(closestSkeleton);
                    }
                    this.currentSkeleton = closestSkeleton;
                    this.sensor.SkeletonStream.ChooseSkeletons(closestID); // Track this skeleton

                }
                else
                {
                    this.currentSkeleton = null;
                    if (kinectStrategy != null)
                        kinectStrategy.CurrentAcceleration = Vector2.Zero;
                }
            }
        }
Пример #27
0
        private void ExtractValidSkeletons(Skeleton[] rawSkeletons)
        {
            var validSkeletons = rawSkeletons.Where(skeleton => skeleton.TrackingState == SkeletonTrackingState.Tracked);
            validSkeletons = validSkeletons.Where(skeleton => skeleton.Joints.Count(joint => joint.TrackingState == JointTrackingState.Tracked) > MINIMUM_JOINT_THRESHOLD);

            foreach (Skeleton skeleton in validSkeletons)
            {
                if (currentVisitors.ContainsKey(skeleton.TrackingId))
                    currentVisitors[skeleton.TrackingId].Update(skeleton);
                else
                {
                    currentVisitors.Add(skeleton.TrackingId, new WagSkeleton(skeleton));
                    faceTrackers.Add(skeleton.TrackingId, new FaceTracker(kinectSensor));
                }
                currentVisitors[skeleton.TrackingId].LastFrameSeen = currentFrame;
                ApplyTransformations(currentVisitors[skeleton.TrackingId]);
            }

            var oldSkeletons = currentVisitors.Values.Where(skeleton => skeleton.LastFrameSeen <= (currentFrame - 5)).ToArray();
            foreach (WagSkeleton skeleton in oldSkeletons)
            {
                currentVisitors.Remove(skeleton.TrackingId);
                faceTrackers.Remove(skeleton.TrackingId);
            }

            OnPropertyChanged("ClosestVisitor");
        }
Пример #28
0
        private void kinectSensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) {
            using (var skeletonFrame = e.OpenSkeletonFrame()) {
                if (skeletonFrame == null) { return; }

                var skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];

                skeletonFrame.CopySkeletonDataTo(skeletonData);

                foreach (var skelaton in skeletonData.Where(skelaton => skelaton.TrackingState == SkeletonTrackingState.Tracked)) {
                    foreach (var gestureCommand in gestureCommands.Where(gestureCommand => gestureCommand.ShouldHandle(skelaton.Joints))) {
                        gestureCommand.Execute();
                    }

                    return;
                }
            }
        }
Пример #29
0
        private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            Skeleton[] skeletons = new Skeleton[0];

            using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
            {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    if (skeletons.Length > 0)
                    {
                        var user = skeletons.Where(u => u.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();

                        if (user != null)
                        {
                            _gestureRight.Update(user);
                            _gestureLeft.Update(user);
                        }
                    }
                }
            }
        }
Пример #30
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())
            {
                if (skeletonFrame != null)
                {
                    skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                    skeletonFrame.CopySkeletonDataTo(skeletons);
                    if (skeletons.Length > 0)
                    {
                        var user = skeletons.Where(u => u.TrackingState == SkeletonTrackingState.Tracked).FirstOrDefault();

                        if (user != null)
                        {
                            _gestureRight.Update(user);
                            _gestureLeft.Update(user);
                        }
                    }
                }
            }

            using (DrawingContext dc = this.drawingGroup.Open())
            {
                // Draw a transparent background to set the render size
                dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));

                if (skeletons.Length != 0)
                {
                    foreach (Skeleton skel in skeletons)
                    {
                        RenderClippedEdges(skel, dc);

                        if (skel.TrackingState == SkeletonTrackingState.Tracked)
                        {
                            this.DrawBonesAndJoints(skel, dc);
                        }
                        else if (skel.TrackingState == SkeletonTrackingState.PositionOnly)
                        {
                            dc.DrawEllipse(
                            this.centerPointBrush,
                            null,
                            this.SkeletonPointToScreen(skel.Position),
                            BodyCenterThickness,
                            BodyCenterThickness);
                        }
                    }
                }

                // prevent drawing outside of our render area
                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));
                if(skeletons != null)
                {

                }
            }
        }
        void NewSensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs args)
        {
            var colors = new Dictionary<int, Color>();
            Skeleton[] skeletons;

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

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

            foreach (var skeleton in skeletons.Where(s => SkeletonTrackingState.Tracked.Equals(s.TrackingState)))
            {
                Random _rnd = new Random();
                var userId = skeleton.TrackingId;

                var crazyColors = Color.FromRgb((byte)_rnd.Next(255), (byte)_rnd.Next(255), (byte)_rnd.Next(255));

                colors.Add(userId, KinectRegion.PrimaryUserTrackingId == userId ?
                    crazyColors :
                    Colors.Blue);
            }

            this.KinectUserViewer.UserColors = colors;
        }