private async void Window_Loaded(object sender, RoutedEventArgs e) { while (true) { // Get Capture Frame using (K4A.Capture capture = await Task.Run(() => { return(this.device.GetCapture()); })) { // Enque Capture tracker.EnqueueCapture(capture); // Pop Result using (K4ABT.Frame frame = tracker.PopResult()) // Get Color Image using (K4A.Image color_image = frame.Capture.Color) { // Get Color Buffer and Write Bitmap byte[] color_buffer = color_image.Memory.ToArray(); color_bitmap.WritePixels(color_rect, color_buffer, color_stride, 0, 0); // Clear All Ellipse from Canvas Canvas_Body.Children.Clear(); // Draw Skeleton for (uint body_index = 0; body_index < frame.NumberOfBodies; body_index++) { // Get Skeleton and ID K4ABT.Skeleton skeleton = frame.GetBodySkeleton(body_index); uint id = frame.GetBodyId(body_index); // Draw Joints for (int joint_index = 0; joint_index < (int)K4ABT.JointId.Count; joint_index++) { // Get Joint and Position K4ABT.Joint joint = skeleton.GetJoint(joint_index); Vector2? position = calibration.TransformTo2D(joint.Position, K4A.CalibrationDeviceType.Depth, K4A.CalibrationDeviceType.Color); if (!position.HasValue) { continue; } // Create Ellipse const int radius = 10; SolidColorBrush stroke_color = new SolidColorBrush(colors[id % colors.Length]); SolidColorBrush fill_color = new SolidColorBrush((joint.ConfidenceLevel >= K4ABT.JointConfidenceLevel.Medium) ? colors[id % colors.Length] : Colors.Transparent); Ellipse ellipse = new Ellipse() { Width = radius, Height = radius, StrokeThickness = 1, Stroke = stroke_color, Fill = fill_color }; // Add Ellipse to Canvas Canvas.SetLeft(ellipse, position.Value.X - (radius / 2)); Canvas.SetTop(ellipse, position.Value.Y - (radius / 2)); Canvas_Body.Children.Add(ellipse); } } } } } }
private async void Window_Loaded(object sender, RoutedEventArgs e) { Tracker tracker = Tracker.Create(this.kinect.GetCalibration(), new TrackerConfiguration() { ProcessingMode = TrackerProcessingMode.Gpu, SensorOrientation = SensorOrientation.Default }); while (running) { using (Capture capture = await Task.Run(() => { return(this.kinect.GetCapture()); })) { this.StatusText = "Received Capture: " + capture.Depth.DeviceTimestamp; this.bitmap.Lock(); var color = capture.Color; var region = new Int32Rect(0, 0, color.WidthPixels, color.HeightPixels); unsafe { using (var pin = color.Memory.Pin()) { this.bitmap.WritePixels(region, (IntPtr)pin.Pointer, (int)color.Size, color.StrideBytes); var bmpSource = BitmapSource.Create(color.WidthPixels, color.HeightPixels, 96.0, 96.0, PixelFormats.Bgr32, null, color.Memory.ToArray(), color.StrideBytes); // JpegBitmapEncoder to save BitmapSource to file // imageSerial is the serial of the sequential image JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmpSource)); using (var fs = new FileStream("./img/" + (imageSerial++) + ".jpeg", FileMode.Create, FileAccess.Write)) { encoder.Save(fs); } } } this.bitmap.AddDirtyRect(region); this.bitmap.Unlock(); tracker.EnqueueCapture(capture); } // Try getting latest tracker frame. using (Frame lastFrame = tracker.PopResult(TimeSpan.Zero, throwOnTimeout: false)) { if (lastFrame == null) { continue; } lock (lockObject) { List <Vector3> joints = new List <Vector3>(); for (uint i = 0; i < lastFrame.NumberOfBodies; ++i) { Skeleton skeleton = lastFrame.GetBodySkeleton(i); var bodyId = lastFrame.GetBodyId(i); for (int jointId = 0; jointId < (int)JointId.Count; ++jointId) { var joint = skeleton.GetJoint(jointId); joints.Add(joint.Position / 1000); } } jointsList.Add(joints); } } } }