示例#1
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();
            }));
        }
示例#2
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
                    {
                    }
                }));
            }
        }
        private void NiTeOnNewData(HandTracker handTracker)
        {
            try
            {
                if (Settings.Default.SmartCam &&
                    _userTracker != null &&
                    _userTracker.IsValid &&
                    _handTracker != null &&
                    _handTracker.IsValid)
                {
                    using (var userFrame = _userTracker.ReadFrame())
                    {
                        using (var handFrame = _handTracker.ReadFrame())
                        {
                            foreach (var gesture in handFrame.Gestures)
                            {
                                if (!gesture.IsComplete)
                                {
                                    continue;
                                }

                                var   handPos = _handTracker.ConvertHandCoordinatesToDepth(gesture.CurrentPosition);
                                short userId  =
                                    Marshal.ReadByte(
                                        userFrame.UserMap.Pixels +
                                        (int)(handPos.Y * userFrame.UserMap.DataStrideBytes) +
                                        (int)(handPos.X * 2));

                                if (userId > 0)
                                {
                                    _activeUserId = userId;
                                }
                            }
                        }

                        if (_activeUserId > 0)
                        {
                            var user = userFrame.GetUserById(_activeUserId);

                            if (user.IsValid && user.IsVisible && user.CenterOfMass.Z > 0)
                            {
                                var position     = new RectangleF(0, 0, 0, 0);
                                var userLocation = _userTracker.ConvertJointCoordinatesToDepth(user.CenterOfMass);
                                var pSize        =
                                    (int)
                                    (Math.Max((int)((4700 - user.CenterOfMass.Z) * 0.08), 50) *
                                     ((float)userFrame.UserMap.FrameSize.Height / 480));
                                position.Y             = (int)userLocation.Y - pSize;
                                position.Height        = pSize;
                                position.X             = (int)userLocation.X;
                                _activePosition.X      = position.X / userFrame.UserMap.FrameSize.Width;
                                _activePosition.Width  = position.Width / userFrame.UserMap.FrameSize.Width;
                                _activePosition.Y      = position.Y / userFrame.UserMap.FrameSize.Height;
                                _activePosition.Height = position.Height / userFrame.UserMap.FrameSize.Height;

                                return;
                            }
                        }
                    }
                }
            }
            catch
            {
                // ignored
            }

            _activeUserId = 0;
        }