Esempio n. 1
0
        //Show frames diff image
        private async Task <BitmapSource> ShowFramesDiffImage()
        {
            float  dpi    = MyCanvas.getDpi();
            double Width  = skeletonImageViewPane.ActualWidth;
            double Height = skeletonImageViewPane.ActualHeight;
            Task <BitmapSource> DrawFramesDiffImageTask = Task.Run(() => {
                DrawingVisual visual = new DrawingVisual();

                int intWidth  = Convert.ToInt32(Width);
                int intHeight = Convert.ToInt32(Height);

                List <KeyValuePair <float, bool> > framesDiffList = this.postSkeletonData.getFrameDiffList();
                float MaxFrameDiff = this.postSkeletonData.MaxFrameDiff;
                //List<KeyValuePair<float, bool>> framesDiffList = utils.TestFramesDiffList;

                using (DrawingContext context = visual.RenderOpen())
                {
                    float LineWidth = (float)Width / this.sysParameter.frame_diff_queue_len;
                    Pen whitePen    = new Pen(Brushes.White, LineWidth);
                    Pen greenPen    = new Pen(Brushes.LightGreen, LineWidth);
                    for (int i = 0; i < framesDiffList.Count; i++)
                    {
                        var frameDiffPair = framesDiffList[i];
                        float lineHeight  = frameDiffPair.Key / MaxFrameDiff * intHeight;
                        var startPoint    = new Point(LineWidth * i, intHeight);
                        var endPoint      = new Point(LineWidth * i, intHeight - lineHeight);
                        if (frameDiffPair.Value)
                        {
                            context.DrawLine(greenPen, startPoint, endPoint);
                        }
                        else
                        {
                            context.DrawLine(whitePen, startPoint, endPoint);
                        }
                    }
                    FormattedText formattedText = new FormattedText(
                        $"Frames Diff Max: {MaxFrameDiff:F2}",
                        CultureInfo.CurrentCulture,
                        FlowDirection.LeftToRight,
                        new Typeface("Arial"),
                        12,
                        Brushes.White,
                        VisualTreeHelper.GetDpi(this).PixelsPerDip);
                    context.DrawText(formattedText, new Point(0, 0));
                }

                BitmapSource framesDiffBitmap = this.utils.ConvertVisual2BitmapSource(visual, Width, Height, dpi);
                framesDiffBitmap.Freeze();

                return(framesDiffBitmap);
            });

            return(await DrawFramesDiffImageTask.ConfigureAwait(false));
        }
Esempio n. 2
0
        //Show skeleton image
        private async Task <BitmapSource> ShowSkeletonImage(Capture capture, Tracker tracker)
        {
            tracker.EnqueueCapture(capture);
            // Try getting latest tracker frame.
            using (Microsoft.Azure.Kinect.BodyTracking.Frame frame = tracker.PopResult(TimeSpan.Zero, throwOnTimeout: false))
            {
                if (frame != null)
                {
                    // Save this frame for visualization in Renderer.

                    // One can access frame data here and extract e.g. tracked bodies from it for the needed purpose.
                    // Instead, for simplicity, we transfer the frame object to the rendering background thread.
                    // This example shows that frame popped from tracker should be disposed. Since here it is used
                    // in a different thread, we use Reference method to prolong the lifetime of the frame object.
                    // For reference on how to read frame data, please take a look at Renderer.NativeWindow_Render().
                    this.visualFrameData.Frame = frame.Reference();
                }
                else
                {
                    return(null);
                }
            }

            float  dpi    = MyCanvas.getDpi();
            double Width  = skeletonImageViewPane.ActualWidth;
            double Height = skeletonImageViewPane.ActualHeight;

            Task <BitmapSource> DrawSkeletonDataTask = Task.Run(() =>
            {
                var lastFrame = this.visualFrameData.TakeFrameWithOwnership();
                if (lastFrame == null || lastFrame.NumberOfBodies == 0)
                {
                    return(null);
                }

                //Get first skeleton
                var skeleton = lastFrame.GetBodySkeleton(0);

                //Update last post skeleton data
                UpdateLastPostSkeletonData(skeleton);

                //Draw Visual
                List <float> jointPosList = new List <float>();
                for (int jointId = 0; jointId < (int)JointId.Count; jointId++)
                {
                    var joint = skeleton.GetJoint(jointId);
                    jointPosList.Add(joint.Position.X);
                    jointPosList.Add(joint.Position.Y);
                }

                float MarginX = 200.0f; float MarginY = 100.0f;
                jointPosList  = this.utils.SkeletonDataAbs2Rel(jointPosList, (int)Width, (int)Height, MarginX, MarginY);

                DrawingVisual visual = new DrawingVisual();
                using (DrawingContext context = visual.RenderOpen())
                {
                    const float radius = 3.0f;

                    var boneColor = Color.FromRgb(255, 255, 255);
                    Brush bursh   = new SolidColorBrush(boneColor);
                    Pen whitePen  = new Pen(Brushes.White, radius);
                    Pen greenPen  = new Pen(Brushes.LightGreen, radius);

                    for (int jointId = 0; jointId < jointPosList.Count / 2; jointId++)
                    {
                        // Render the joint as a ellipse.
                        var jointPoint = new Point(jointPosList[2 * jointId], jointPosList[2 * jointId + 1]);
                        context.DrawEllipse(bursh, whitePen, jointPoint, radius, radius);

                        // Render the bone as a line.
                        if (JointConnections.JointParent.TryGetValue((JointId)jointId, out JointId parentId))
                        {
                            var startPos = new Point(jointPosList[2 * jointId], jointPosList[2 * jointId + 1]);
                            int parId    = Convert.ToInt32(parentId);
                            var endPos   = new Point(jointPosList[2 * parId], jointPosList[2 * parId + 1]);

                            if (SLRPostJoint.PostJointsList.Contains((JointId)jointId) && SLRPostJoint.PostJointsList.Contains(parentId))
                            {
                                context.DrawLine(greenPen, startPos, endPos);
                            }
                            else
                            {
                                context.DrawLine(whitePen, startPos, endPos);
                            }
                        }
                    }
                }

                //this.utils.UpdataMyCanvasView(skeletonImageViewPane, visual, dpi);
                BitmapSource skeletonBitmapSource = utils.ConvertVisual2BitmapSource(visual, Width, Height, dpi);

                skeletonBitmapSource.Freeze();

                return(skeletonBitmapSource);
            });

            return(await DrawSkeletonDataTask.ConfigureAwait(false));
        }