From 249f2edbfe7bd711fb154f989b6aefcf87ef8bfb Mon Sep 17 00:00:00 2001 From: Jonathan Trottier Date: Wed, 3 May 2023 10:08:00 -0400 Subject: [PATCH] squelette.Entrainement --- .../JeuHoy_WPF_Natif/JeuHoy_WPF_Natif.csproj | 4 +- .../View/wEntrainement.xaml.cs | 104 +++++++++++++++++- 2 files changed, 102 insertions(+), 6 deletions(-) diff --git a/JeuHoyEtudiants/JeuHoy_WPF_Natif/JeuHoy_WPF_Natif.csproj b/JeuHoyEtudiants/JeuHoy_WPF_Natif/JeuHoy_WPF_Natif.csproj index 400938d..1254054 100644 --- a/JeuHoyEtudiants/JeuHoy_WPF_Natif/JeuHoy_WPF_Natif.csproj +++ b/JeuHoyEtudiants/JeuHoy_WPF_Natif/JeuHoy_WPF_Natif.csproj @@ -282,8 +282,6 @@ Designer - - - + \ No newline at end of file diff --git a/JeuHoyEtudiants/JeuHoy_WPF_Natif/View/wEntrainement.xaml.cs b/JeuHoyEtudiants/JeuHoy_WPF_Natif/View/wEntrainement.xaml.cs index a147dcf..ab4ab88 100644 --- a/JeuHoyEtudiants/JeuHoy_WPF_Natif/View/wEntrainement.xaml.cs +++ b/JeuHoyEtudiants/JeuHoy_WPF_Natif/View/wEntrainement.xaml.cs @@ -19,11 +19,22 @@ namespace JeuHoy_WPF.View /// public partial class wEntrainement : Window { - + private Dictionary _dicImgFigure = new Dictionary(); private JouerSon _son = new JouerSon(); private int _positionEnCours = 1; - + + + private KinectSensor _sensor; + private MultiSourceFrameReader _multiSourceFrameReader; + private BodyFrameReader _bodyFrameReader; + private WriteableBitmap _bitmap; + //private DisplayFrameType _displayFrameType = DisplayFrameType.Color; + + private byte[] _picPixels = null; + + + /// /// Constructeur @@ -32,6 +43,27 @@ namespace JeuHoy_WPF.View { InitializeComponent(); + + _sensor = KinectSensor.GetDefault(); + if (_sensor != null) + { + _sensor.Open(); + + //Lecture des images + _multiSourceFrameReader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color); + //_multiSourceFrameReader.MultiSourceFrameArrived += MultiSourceFrameReader_MultiSourceFrameArrived; + + FrameDescription frameDescription = _sensor.ColorFrameSource.FrameDescription; + _picPixels = new byte[frameDescription.Width * frameDescription.Height * 4]; + + //Lecture des squelettes détectés + _bodyFrameReader = _sensor.BodyFrameSource.OpenReader(); + _bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived; + + + } + + for (int i = 1; i <= CstApplication.NBFIGURE; i++) { Uri uriSource = new Uri(AppDomain.CurrentDomain.BaseDirectory + @"./HoyContent/fig" + i + ".png", UriKind.Absolute); @@ -43,6 +75,71 @@ namespace JeuHoy_WPF.View _son.JouerSonAsync(@"./HoyContent/hoy.wav"); } + + private void MultiSourceFrameReader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e) + { + MultiSourceFrame multiSource = e.FrameReference.AcquireFrame(); + + if (multiSource == null) + return; + + + + using (ColorFrame colorFrame = multiSource.ColorFrameReference.AcquireFrame()) + { + if (colorFrame != null) + { + FrameDescription frameDescription = colorFrame.FrameDescription; + if (_bitmap == null) + _bitmap = new WriteableBitmap(frameDescription.Width, + frameDescription.Height, + 96.0, + 96.0, + PixelFormats.Bgra32, + null); + colorFrame.CopyConvertedFrameDataToArray(_picPixels, ColorImageFormat.Bgra); + _bitmap.Lock(); + Marshal.Copy(_picPixels, 0, _bitmap.BackBuffer, _picPixels.Length); + _bitmap.AddDirtyRect(new Int32Rect(0, 0, _bitmap.PixelWidth, _bitmap.PixelHeight)); + _bitmap.Unlock(); + picKinect.Source = _bitmap; + } + + } + + + + } + + + + private void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) + { + using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame()) + { + if (bodyFrame == null) + return; + Body[] bodies = new Body[bodyFrame.BodyCount]; + bodyFrame.GetAndRefreshBodyData(bodies); + Body body = bodies.FirstOrDefault(b => b.IsTracked); + + if (body != null) + { + DessinerSquelette(body, _sensor); + + + } + + + + + + + + } + } + + /// /// Dessine un ellipse pour chacune des jointure du squelette détecté. /// @@ -54,6 +151,7 @@ namespace JeuHoy_WPF.View { if (body != null) { + pDessinSquelette.Children.Clear(); Joint[] joints = body.Joints.Values.ToArray(); for (int i = 0; i < joints.Count(); i++) DrawJoint(sensor, joints[i], CstApplication.BODY_ELLIPSE_SIZE, pDessinSquelette); @@ -82,7 +180,7 @@ namespace JeuHoy_WPF.View // Créer un cercle à la position du joint Ellipse ellipse = new Ellipse(); - ellipse.Fill = new SolidColorBrush(Colors.Green); + ellipse.Fill = new SolidColorBrush(Colors.Yellow); ellipse.Width = size; ellipse.Height = size;