diff --git a/KinectConnection/BodyImageStream.cs b/KinectConnection/BodyImageStream.cs index 8ab7ad3..2decc32 100644 --- a/KinectConnection/BodyImageStream.cs +++ b/KinectConnection/BodyImageStream.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Kinect; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,15 +12,68 @@ namespace KinectConnection { public class BodyImageStream : KinectStream { + private BodyFrameReader reader; + private Body[] bodies = null; + + public Body[] Bodies + { + get { return bodies; } + set { SetProperty(ref bodies, value); } + } + + public BodyImageStream() : base() + { + // Initialize the bodies array + this.bodies = new Body[this.KinectSensor.BodyFrameSource.BodyCount]; + } public override void Start() { - throw new NotImplementedException(); + // Open the reader for the body frames + this.reader = this.KinectSensor.BodyFrameSource.OpenReader(); + + // Subscribe to the event + this.reader.FrameArrived += this.Reader_BodyFrameArrived; } public override void Stop() { - throw new NotImplementedException(); + if (this.reader != null) + { + // Unsubscribe from the event + this.reader.FrameArrived -= this.Reader_BodyFrameArrived; + + // Dispose the reader to free resources + this.reader.Dispose(); + this.reader = null; + } + } + + private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) + { + using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame()) + { + if (bodyFrame != null) + { + bodyFrame.GetAndRefreshBodyData(this.bodies); + } + } + + // Process the body data + foreach (Body body in this.bodies) + { + if (body.IsTracked) + { + // Get the right hand joint + Joint rightHand = body.Joints[JointType.HandRight]; + + // Check the tracking state of the joint + if (rightHand.TrackingState == TrackingState.Tracked) + { + CameraSpacePoint position = rightHand.Position; + } + } + } } } } diff --git a/KinectConnection/KinectStreamsFactory.cs b/KinectConnection/KinectStreamsFactory.cs index 3532887..63b45b0 100644 --- a/KinectConnection/KinectStreamsFactory.cs +++ b/KinectConnection/KinectStreamsFactory.cs @@ -20,6 +20,7 @@ namespace KinectConnection streamFactory = new Dictionary> { { KinectStreams.Color, () => new ColorImageStream() }, + { KinectStreams.Body, () => new BodyImageStream() }, // Other streams ... }; } diff --git a/KinectConnection/enums/KinectStreams.cs b/KinectConnection/enums/KinectStreams.cs index 4326fd5..e7eaf76 100644 --- a/KinectConnection/enums/KinectStreams.cs +++ b/KinectConnection/enums/KinectStreams.cs @@ -8,6 +8,7 @@ None, Color, Depth, - IR + IR, + Body } } diff --git a/KinectSensorStreams/View/MainWindow.xaml b/KinectSensorStreams/View/MainWindow.xaml index dff2997..8fca481 100644 --- a/KinectSensorStreams/View/MainWindow.xaml +++ b/KinectSensorStreams/View/MainWindow.xaml @@ -110,8 +110,30 @@ - - + + + + + + + + + + + + + + + + + + + + + diff --git a/KinectSensorStreams/ViewModel/MainWindowVM.cs b/KinectSensorStreams/ViewModel/MainWindowVM.cs index 26b4c60..f3daac6 100644 --- a/KinectSensorStreams/ViewModel/MainWindowVM.cs +++ b/KinectSensorStreams/ViewModel/MainWindowVM.cs @@ -1,12 +1,16 @@ -using CommunityToolkit.Mvvm.Input; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using KinectConnection; using KinectConnection.enums; +using System.Net.NetworkInformation; using System.Windows.Input; namespace KinectSensorStreams.ViewModel { - public class MainWindowVM + public class MainWindowVM : ObservableObject { + private KinectStream kinectStream; + #region Properties /// @@ -31,7 +35,11 @@ namespace KinectSensorStreams.ViewModel /// /// The Kinect stream property. /// - public KinectStream KinectStream { get; set; } + public KinectStream KinectStream + { + get { return kinectStream; } + set { SetProperty(ref kinectStream, value); } + } #endregion @@ -48,7 +56,7 @@ namespace KinectSensorStreams.ViewModel // factory KinectStreamsFactory = new KinectStreamsFactory(new KinectManager()); // kinect stream => color stream for now - KinectStream = KinectStreamsFactory[KinectStreams.Color]; + KinectStream = KinectStreamsFactory[KinectStreams.Body]; StartCommand = new RelayCommand(Start); @@ -75,7 +83,7 @@ namespace KinectSensorStreams.ViewModel private void Color() { - KinectStream.Start(); + //KinectStream.Start(); } #endregion