예제 #1
0
        // ReSharper disable once ParameterHidesMember
        private void HandTrackerOnNewData(HandTracker handTracker)
        {
            if (!handTracker.IsValid)
            {
                return;
            }

            using (var frame = handTracker.ReadFrame())
            {
                if (frame == null || !frame.IsValid)
                {
                    return;
                }

                lock (this)
                {
                    using (var depthFrame = frame.DepthFrame)
                    {
                        if (image?.Width != depthFrame.FrameSize.Width ||
                            image?.Height != depthFrame.FrameSize.Height)
                        {
                            image?.Dispose();
                            image = new Bitmap(
                                depthFrame.FrameSize.Width,
                                depthFrame.FrameSize.Height,
                                PixelFormat.Format24bppRgb);
                        }
                    }

                    using (var g = Graphics.FromImage(image))
                    {
                        g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), image.Size));

                        foreach (var gesture in frame.Gestures)
                        {
                            if (gesture.IsComplete)
                            {
                                handTracker.StartHandTracking(gesture.CurrentPosition);
                            }
                        }

                        if (frame.Hands.Length == 0)
                        {
                            g.DrawString("Raise your hand", SystemFonts.DefaultFont, Brushes.White, 10, 10);
                        }
                        else
                        {
                            foreach (var hand in frame.Hands)
                            {
                                if (hand.IsTracking)
                                {
                                    var handPosEllipse = new Point();
                                    var handPos        = handTracker.ConvertHandCoordinatesToDepth(hand.Position);

                                    if (!handPos.IsEmpty && !float.IsNaN(handPos.X) && !float.IsNaN(handPos.Y))
                                    {
                                        handPosEllipse.X = (int)handPos.X - 5;
                                        handPosEllipse.Y = (int)handPos.Y - 5;
                                        g.DrawEllipse(new Pen(Brushes.White, 5),
                                                      new Rectangle(handPosEllipse, new Size(5, 5)));
                                    }
                                }
                            }
                        }

                        g.Save();
                    }
                }

                Invoke(
                    new MethodInvoker(
                        delegate
                {
                    try
                    {
                        // ReSharper disable AccessToDisposedClosure
                        fps      = (1000000 / (frame.Timestamp - lastTime) + fps * 4) / 5;
                        lastTime = frame.Timestamp;
                        Text     = @"Frame #" +
                                   frame.FrameIndex +
                                   @" - Time: " +
                                   frame.Timestamp +
                                   @" - FPS: " +
                                   fps;
                        // ReSharper restore AccessToDisposedClosure
                        pb_preview.Image?.Dispose();
                        pb_preview.Image = image.Clone(
                            new Rectangle(new Point(0, 0), image.Size),
                            PixelFormat.Format24bppRgb);
                    }
                    catch
                    {
                    }
                }));
            }
        }
예제 #2
0
        // ReSharper disable once ParameterHidesMember
        private void HandTrackerOnNewData(HandTracker handTracker)
        {
            if (!handTracker.IsValid)
            {
                return;
            }

            HandTrackerFrameRef frame = handTracker.ReadFrame();

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

            lock (this.image)
            {
                using (VideoFrameRef depthFrame = frame.DepthFrame)
                {
                    if (this.image.Width != depthFrame.FrameSize.Width ||
                        this.image.Height != depthFrame.FrameSize.Height)
                    {
                        this.image = new Bitmap(
                            depthFrame.FrameSize.Width,
                            depthFrame.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 (GestureData gesture in frame.Gestures)
                    {
                        if (gesture.IsComplete)
                        {
                            handTracker.StartHandTracking(gesture.CurrentPosition);
                        }
                    }

                    if (frame.Hands.Length == 0)
                    {
                        g.DrawString("Raise your hand", SystemFonts.DefaultFont, Brushes.White, 10, 10);
                    }
                    else
                    {
                        foreach (HandData hand in frame.Hands)
                        {
                            if (hand.IsTracking)
                            {
                                Point  handPosEllipse = new Point();
                                PointF handPos        = handTracker.ConvertHandCoordinatesToDepth(hand.Position);
                                handPosEllipse.X = (int)handPos.X - 5;
                                handPosEllipse.Y = (int)handPos.Y - 5;
                                g.DrawEllipse(new Pen(Brushes.White, 5), new Rectangle(handPosEllipse, new Size(5, 5)));
                            }
                        }
                    }

                    g.Save();
                }
            }

            this.Invoke(
                new MethodInvoker(
                    delegate
            {
                this.fps      = ((1000000 / (frame.Timestamp - this.lastTime)) + (this.fps * 4)) / 5;
                this.lastTime = frame.Timestamp;
                this.Text     = @"Frame #" + frame.FrameIndex + @" - Time: " + frame.Timestamp + @" - FPS: "
                                + this.fps;
                this.pb_preview.Image = this.image.Clone(
                    new Rectangle(new Point(0, 0), this.image.Size),
                    PixelFormat.Format24bppRgb);
                frame.Release();
            }));
        }