Exemple #1
0
        void uTracker_onNewData(UserTracker uTracker)
        {
            if (!uTracker.isValid)
                return;
            using (UserTrackerFrameRef frame = uTracker.readFrame())
            {
                if (!frame.isValid)
                    return;
                lock (image)
                {
                    if (image.Width != frame.UserMap.FrameSize.Width || image.Height != frame.UserMap.FrameSize.Height)
                        image = new Bitmap(frame.UserMap.FrameSize.Width, frame.UserMap.FrameSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    using (Graphics g = Graphics.FromImage(image))
                    {
                        g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), image.Size));
                        foreach (UserData user in frame.Users)
                        {
                            if (user.isNew && user.isVisible)
                                uTracker.StartSkeletonTracking(user.UserId);
                            if (user.isVisible && user.Skeleton.State == Skeleton.SkeletonState.TRACKED)
                            {
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_HAND, SkeletonJoint.JointType.RIGHT_ELBOW);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_HAND, SkeletonJoint.JointType.LEFT_ELBOW);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_ELBOW, SkeletonJoint.JointType.RIGHT_SHOULDER);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_ELBOW, SkeletonJoint.JointType.LEFT_SHOULDER);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_FOOT, SkeletonJoint.JointType.RIGHT_KNEE);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_FOOT, SkeletonJoint.JointType.LEFT_KNEE);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_KNEE, SkeletonJoint.JointType.RIGHT_HIP);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_KNEE, SkeletonJoint.JointType.LEFT_HIP);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_SHOULDER, SkeletonJoint.JointType.LEFT_SHOULDER);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_HIP, SkeletonJoint.JointType.LEFT_HIP);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.RIGHT_SHOULDER, SkeletonJoint.JointType.RIGHT_HIP);
                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.LEFT_SHOULDER, SkeletonJoint.JointType.LEFT_HIP);

                                DrawLineBetweenJoints(g, user.Skeleton, SkeletonJoint.JointType.HEAD, SkeletonJoint.JointType.NECK);
                            }
                        }
                        g.Save();
                    }
                }
                this.Invoke(new MethodInvoker(delegate()
                {
                    fps = ((1000000 / (frame.Timestamp - lastTime)) + (fps * 4)) / 5;
                    lastTime = frame.Timestamp;
                    this.Text = "Frame #" + frame.FrameIndex.ToString() + " - Time: " + frame.Timestamp.ToString() + " - FPS: " + fps.ToString();
                    pb_preview.Image = image.Clone(new Rectangle(new Point(0, 0), image.Size), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                }));
            }
        }
        // ReSharper disable once ParameterHidesMember
        private void UserTrackerOnNewData(UserTracker userTracker)
        {
            if (!userTracker.IsValid)
            {
                return;
            }

            UserTrackerFrameRef frame = userTracker.ReadFrame();

            if (frame == null || !frame.IsValid)
            {
                return;
            }

            lock (this.image)
            {
                if (this.image.Width != frame.UserMap.FrameSize.Width
                    || this.image.Height != frame.UserMap.FrameSize.Height)
                {
                    this.image = new Bitmap(
                        frame.UserMap.FrameSize.Width,
                        frame.UserMap.FrameSize.Height,
                        PixelFormat.Format24bppRgb);
                }

                using (Graphics g = Graphics.FromImage(this.image))
                {
                    g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), this.image.Size));
                    foreach (UserData user in frame.Users)
                    {
                        if (user.IsNew && user.IsVisible)
                        {
                            userTracker.StartSkeletonTracking(user.UserId);
                        }

                        if (user.IsVisible && user.Skeleton.State == Skeleton.SkeletonState.Tracked)
                        {
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightHand,
                                SkeletonJoint.JointType.RightElbow);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftHand,
                                SkeletonJoint.JointType.LeftElbow);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightElbow,
                                SkeletonJoint.JointType.RightShoulder);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftElbow,
                                SkeletonJoint.JointType.LeftShoulder);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightFoot,
                                SkeletonJoint.JointType.RightKnee);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftFoot,
                                SkeletonJoint.JointType.LeftKnee);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightKnee,
                                SkeletonJoint.JointType.RightHip);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftKnee,
                                SkeletonJoint.JointType.LeftHip);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightShoulder,
                                SkeletonJoint.JointType.LeftShoulder);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightHip,
                                SkeletonJoint.JointType.LeftHip);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.RightShoulder,
                                SkeletonJoint.JointType.RightHip);
                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.LeftShoulder,
                                SkeletonJoint.JointType.LeftHip);

                            this.DrawLineBetweenJoints(
                                g,
                                user.Skeleton,
                                SkeletonJoint.JointType.Head,
                                SkeletonJoint.JointType.Neck);
                        }
                    }

                    g.Save();
                }
            }

            this.Invoke(
                new MethodInvoker(
                    delegate
                        {
                            this.fps = ((1000000 / (frame.Timestamp - this.lastTime)) + (this.fps * 4)) / 5;
                            this.lastTime = frame.Timestamp;
                            this.Text = string.Format(
                                "Frame #{0} - Time: {1} - FPS: {2}",
                                frame.FrameIndex,
                                frame.Timestamp,
                                this.fps);
                            this.pb_preview.Image = this.image.Clone(
                                new Rectangle(new Point(0, 0), this.image.Size),
                                PixelFormat.Format24bppRgb);
                            frame.Release();
                        }));
        }
        private void SkeletonSensor_OnNewData(UserTracker userTracker)
        {
            if (!userTracker.IsValid)
                return;

            ulong currentTimeTicks = 0;
            Skeleton validSkeleton = null;
            using (UserTrackerFrameRef frame = userTracker.ReadFrame())
            {
                if (frame == null || !frame.IsValid)
                    return;

                foreach (UserData user in frame.Users)
                {
                    if (user.IsNew && user.IsVisible)
                    {
                        userTracker.StartSkeletonTracking(user.UserId);
                    }
                    if (user.IsVisible && user.Skeleton.State == Skeleton.SkeletonState.Tracked)
                    {
                        validSkeleton = user.Skeleton;
                        break;
                    }
                }
                currentTimeTicks = frame.Timestamp;
            }

            if (validSkeleton != null && IsEngineStart)
            {
                double deltaTimeTicks = (currentTimeTicks - lastUpdate);
                if (lastUpdate == 0)
                    deltaTimeTicks = 0;
                lastUpdate = currentTimeTicks;
                RunFatigueEngine(validSkeleton, deltaTimeTicks / 1000000);
            }

            Dispatcher.Invoke((Action)delegate
            {
                iKinectCapture.Source = kinectSensor.RawImageSource;
                if (validSkeleton != null)
                    iSkeleton.Source = DrawSkeleton(validSkeleton, Brushes.Transparent);
                else
                    iSkeleton.Source = null;
            });
        }