From c76b1a3f7c6fa681aed6231f445a3afa5d983b3c Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Fri, 2 Feb 2024 09:47:48 +0100 Subject: [PATCH] WantToFix(dev): push some crap --- Sources/ConsoleApp/Program.cs | 58 ++--- Sources/Lib/ColorAndBodyImageStream.cs | 270 +++++++++++++++-------- Sources/Lib/KinectStreams.cs | 3 +- Sources/Lib/KinectStreamsFactory.cs | 22 +- Sources/Lib/Lib.csproj | 1 + Sources/LibMyGesturesBank/BaseGesture.cs | 1 - Sources/WpfApp/MainWindow.xaml | 10 +- Sources/WpfApp/MainWindow.xaml.cs | 8 + Sources/WpfApp/StreamTemplateSelector.cs | 23 ++ Sources/WpfApp/WpfApp.csproj | 1 + 10 files changed, 253 insertions(+), 144 deletions(-) create mode 100644 Sources/WpfApp/StreamTemplateSelector.cs diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index dc436f5..b02defb 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -14,40 +14,40 @@ namespace ConsoleApp { static void Main(string[] args) { - KinectManager kinectManager = new KinectManager(); - if (!kinectManager.StartSensor()) - { - Console.WriteLine("Kinect n'est pas connecté ou non reconnu."); - return; - } + // KinectManager kinectManager = new KinectManager(); + // if (kinectManager.StartSensor()) + // { + // Console.WriteLine("Kinect n'est pas connecté ou non reconnu."); + // return; + // } - // Créez les instances de vos postures - PostureHandUp handUpPosture = new PostureHandUp(); - PostureHandsOnHead handsOnHeadPosture = new PostureHandsOnHead(); + // // Créez les instances de vos postures + // PostureHandUp handUpPosture = new PostureHandUp(); + // PostureHandsOnHead handsOnHeadPosture = new PostureHandsOnHead(); - // Abonnez-vous aux événements de reconnaissance de posture - handUpPosture.GestureRecognized += (sender, e) => - { - Console.WriteLine("Posture Hand Up reconnue !"); - }; + // // Abonnez-vous aux événements de reconnaissance de posture + // handUpPosture.GestureRecognized += (sender, e) => + // { + // Console.WriteLine("Posture Hand Up reconnue !"); + // }; - handsOnHeadPosture.GestureRecognized += (sender, e) => - { - Console.WriteLine("Posture Hands On Head reconnue !"); - }; + // handsOnHeadPosture.GestureRecognized += (sender, e) => + // { + // Console.WriteLine("Posture Hands On Head reconnue !"); + // }; - // Boucle pour tester les postures - while (true) - { - Body body = kinectManager.GetNextBody(); // Méthode fictive pour obtenir les données du corps - if (body != null) - { - handUpPosture.TestGesture(body); - handsOnHeadPosture.TestGesture(body); - } + // // Boucle pour tester les postures + // while (true) + // { + // Body body = kinectManager.GetNextBody(); // Méthode fictive pour obtenir les données du corps + // if (body != null) + // { + // handUpPosture.TestGesture(body); + // handsOnHeadPosture.TestGesture(body); + // } - Thread.Sleep(50); // Une petite pause pour ne pas surcharger le CPU - } + // Thread.Sleep(50); // Une petite pause pour ne pas surcharger le CPU + // } } } diff --git a/Sources/Lib/ColorAndBodyImageStream.cs b/Sources/Lib/ColorAndBodyImageStream.cs index 5b753e4..f52390b 100644 --- a/Sources/Lib/ColorAndBodyImageStream.cs +++ b/Sources/Lib/ColorAndBodyImageStream.cs @@ -1,97 +1,175 @@ -//using Microsoft.Kinect; -//using System; -//using System.Collections.Generic; -//using System.Linq; -//using System.Text; -//using System.Threading.Tasks; -//using System.Windows.Media.Imaging; -//using System.Windows.Media; -//using System.Windows; -//using System.Windows.Shapes; - -//namespace Lib -//{ -// class ColorAndBodyImageStream : KinectStream -// { -// private BodyFrameReader reader; -// public BodyFrameReader Reader -// { -// get { return reader; } -// set { reader = value; } -// } - -// private Body[] bodies; -// public Body[] Bodies -// { -// get { return bodies; } -// private set { bodies = value; } -// } -// public ColorAndBodyImageStream(KinectManager kinectManager) : base(kinectManager) -// { -// var frameDescription = KinectManager.Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); -// this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); -// reader = KinectManager.Sensor.BodyFrameSource.OpenReader(); -// reader.FrameArrived += Reader_BodyFrameArrived; -// // Initialiser le tableau des corps -// this.bodies = new Body[KinectManager.Sensor.BodyFrameSource.BodyCount]; -// } - -// private void DrawSkeleton(Body body) -// { -// foreach (JointType jointType in body.Joints.Keys) -// { -// Joint joint = body.Joints[jointType]; -// if (joint.TrackingState == TrackingState.Tracked) -// { -// // Convertir les coordonnées du joint en coordonnées de l'écran -// Point point = new Point(); -// ColorSpacePoint colorPoint = KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position); -// point.X = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X; -// point.Y = float.IsInfinity(colorPoint.Y) ? 0 : colorPoint.Y; - -// // Dessiner le joint -// DrawJoint(point); -// } -// } - -// // Dessinez les os ici si nécessaire -// } - -// private void DrawJoint(Point point) -// { -// Ellipse ellipse = new Ellipse -// { -// Width = 10, -// Height = 10, -// Fill = new SolidColorBrush(Colors.Red) -// }; - -// Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2); -// Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2); - -// skeletonCanvas.Children.Add(ellipse); -// } -// private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) -// { -// using (var bodyFrame = e.FrameReference.AcquireFrame()) -// { -// if (bodyFrame != null) -// { -// bodyFrame.GetAndRefreshBodyData(this.bodies); - -// skeletonCanvas.Children.Clear(); // Nettoyer le canvas avant de dessiner - -// foreach (var body in this.bodies) -// { -// if (body.IsTracked) -// { -// // Dessiner le squelette -// DrawSkeleton(body); -// } -// } -// } -// } -// } -// } -//} +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media.Imaging; +using System.Windows.Media; +using System.Windows; +using System.Windows.Shapes; +using Lib; +using System.Windows.Controls; + +namespace Lib +{ + public class ColorAndBodyImageStream : KinectStream + { + private BodyFrameReader reader; + public BodyFrameReader Reader + { + get { return reader; } + set { reader = value; } + } + + private Body[] bodies; + public Body[] Bodies + { + get { return bodies; } + private set { bodies = value; } + } + public override void Stop() + { + if (reader != null) + { + reader.Dispose(); + reader = null; + } + } + private Canvas skeletonCanvas; + public Canvas SkeletonCanvas { + get { return skeletonCanvas; } + private set { skeletonCanvas = value; } + } + + public ColorAndBodyImageStream(KinectManager kinectmanager, Canvas skeletonCanvas) : base(kinectmanager) + { + var framedescription = kinectmanager.Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); + Bitmap = new WriteableBitmap(framedescription.Width, framedescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null); + reader = kinectmanager.Sensor.BodyFrameSource.OpenReader(); + reader.FrameArrived += Reader_BodyFrameArrived; + // initialiser le tableau des corps + this.bodies = new Body[kinectmanager.Sensor.BodyFrameSource.BodyCount]; + SkeletonCanvas = skeletonCanvas; + } + + private void drawbone(Body body, JointType JointType0, JointType JointType1) + { + Joint joint0 = body.Joints[JointType0]; + Joint joint1 = body.Joints[JointType1]; + + // ne dessinez que si les deux joints sont suivis + if (joint0.TrackingState == TrackingState.Tracked && joint1.TrackingState == TrackingState.Tracked) + { + Line bone = new Line + { + Stroke = new SolidColorBrush(Colors.LightBlue), + StrokeThickness = 4, + X1 = MapJointToScreen(joint0).X, + Y1 = MapJointToScreen(joint0).Y, + X2 = MapJointToScreen(joint1).X, + Y2 = MapJointToScreen(joint1).Y + }; + + skeletonCanvas.Children.Add(bone); + } + } + + private Point MapJointToScreen(Joint joint) + { + ColorSpacePoint colorPoint = this.KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position); + + // Gestion des coordonnées infinies + float x = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X; + float y = float.IsInfinity(colorPoint.Y) ? 0 : colorPoint.Y; + + return new Point(x, y); + } + + private void drawskeleton(Body body) + { + // tête et cou + drawbone(body, JointType.Head, JointType.Neck); + drawbone(body, JointType.Neck, JointType.SpineShoulder); + + // torse + drawbone(body, JointType.SpineShoulder, JointType.SpineMid); + drawbone(body, JointType.SpineMid, JointType.SpineBase); + drawbone(body, JointType.SpineShoulder, JointType.ShoulderRight); + drawbone(body, JointType.SpineShoulder, JointType.ShoulderLeft); + drawbone(body, JointType.SpineBase, JointType.HipRight); + drawbone(body, JointType.SpineBase, JointType.HipLeft); + + // bras droit + drawbone(body, JointType.ShoulderRight, JointType.ElbowRight); + drawbone(body, JointType.ElbowRight, JointType.WristRight); + drawbone(body, JointType.WristRight, JointType.HandRight); + drawbone(body, JointType.HandRight, JointType.HandTipRight); + drawbone(body, JointType.WristRight, JointType.ThumbRight); + + // bras gauche + drawbone(body, JointType.ShoulderLeft, JointType.ElbowLeft); + drawbone(body, JointType.ElbowLeft, JointType.WristLeft); + drawbone(body, JointType.WristLeft, JointType.HandLeft); + drawbone(body, JointType.HandLeft, JointType.HandTipLeft); + drawbone(body, JointType.WristLeft, JointType.ThumbLeft); + + // jambe droite + drawbone(body, JointType.HipRight, JointType.KneeRight); + drawbone(body, JointType.KneeRight, JointType.AnkleRight); + drawbone(body, JointType.AnkleRight, JointType.FootRight); + + // jambe gauche + drawbone(body, JointType.HipLeft, JointType.KneeLeft); + drawbone(body, JointType.KneeLeft, JointType.AnkleLeft); + drawbone(body, JointType.AnkleLeft, JointType.FootLeft); + + // dessinez les joints + foreach (JointType JointType in body.Joints.Keys) + { + Joint joint = body.Joints[JointType]; + if (joint.TrackingState == TrackingState.Tracked) + { + DrawJoint(MapJointToScreen(joint)); + } + } + } + + private void DrawJoint(Point point) + { + Ellipse ellipse = new Ellipse + { + Width = 10, + Height = 10, + Fill = new SolidColorBrush(Colors.Red) + }; + + Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2); + Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2); + + skeletonCanvas.Children.Add(ellipse); + } + private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) + { + using (var bodyframe = e.FrameReference.AcquireFrame()) + { + if (bodyframe != null) + { + bodyframe.GetAndRefreshBodyData(this.bodies); + + SkeletonCanvas.Children.Clear(); // nettoyer le Canvas avant de dessiner + + foreach (var body in this.bodies) + { + if (body.IsTracked) + { + // dessiner le squelette + drawskeleton(body); + } + } + } + } + } + } +} diff --git a/Sources/Lib/KinectStreams.cs b/Sources/Lib/KinectStreams.cs index 43c3b97..5744db3 100644 --- a/Sources/Lib/KinectStreams.cs +++ b/Sources/Lib/KinectStreams.cs @@ -11,6 +11,7 @@ namespace Lib None, Color, Depth, - IR + IR, + ColorAndBody } } diff --git a/Sources/Lib/KinectStreamsFactory.cs b/Sources/Lib/KinectStreamsFactory.cs index 7ac4345..f119338 100644 --- a/Sources/Lib/KinectStreamsFactory.cs +++ b/Sources/Lib/KinectStreamsFactory.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Controls; namespace Lib { @@ -14,32 +15,33 @@ namespace Lib private set { _kinectManager = value; } } - private Dictionary> _streamFactory; - public Dictionary> StreamFactory + private Dictionary> _streamFactory; + public Dictionary> StreamFactory { get { return _streamFactory; } private set { _streamFactory = value; } } - public KinectStreamsFactory(KinectManager kinect) + public KinectStreamsFactory(KinectManager kinect, Canvas skeletonCanvas = null) { _kinectManager = kinect; // Initialisation de la fabrique avec les fonctions de création pour chaque type de flux. - StreamFactory = new Dictionary> + StreamFactory = new Dictionary> { - { KinectStreams.Color, () => new ColorImageStream(KinectManager) }, - { KinectStreams.Depth, () => new DepthImageStream(KinectManager) }, - { KinectStreams.IR, () => new InfraredImageStream(KinectManager) } + { KinectStreams.Color, (canvas) => new ColorImageStream(KinectManager) }, + { KinectStreams.Depth, (canvas) => new DepthImageStream(KinectManager) }, + { KinectStreams.IR, (canvas) => new InfraredImageStream(KinectManager) }, + { KinectStreams.ColorAndBody, (canvas) => new ColorAndBodyImageStream(KinectManager,skeletonCanvas) } }; } - public KinectStream this[KinectStreams stream] + public KinectStream this[KinectStreams stream, Canvas skeletonCanvas = null] { get { if (StreamFactory.ContainsKey(stream)) { - return StreamFactory[stream](); + return StreamFactory[stream](skeletonCanvas); } else { @@ -48,4 +50,4 @@ namespace Lib } } } -} +} \ No newline at end of file diff --git a/Sources/Lib/Lib.csproj b/Sources/Lib/Lib.csproj index 5be8933..e7870f9 100644 --- a/Sources/Lib/Lib.csproj +++ b/Sources/Lib/Lib.csproj @@ -35,6 +35,7 @@ ..\packages\Microsoft.Kinect.2.0.1410.19000\lib\net45\Microsoft.Kinect.dll + diff --git a/Sources/LibMyGesturesBank/BaseGesture.cs b/Sources/LibMyGesturesBank/BaseGesture.cs index 7ba9866..0750ecd 100644 --- a/Sources/LibMyGesturesBank/BaseGesture.cs +++ b/Sources/LibMyGesturesBank/BaseGesture.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using System; using Microsoft.Kinect; namespace LibMyGesturesBank diff --git a/Sources/WpfApp/MainWindow.xaml b/Sources/WpfApp/MainWindow.xaml index 88530fb..32cac25 100644 --- a/Sources/WpfApp/MainWindow.xaml +++ b/Sources/WpfApp/MainWindow.xaml @@ -22,16 +22,12 @@