From 7a0e038241f45731bfe0859798991f68402acbb2 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 14 Feb 2024 11:57:50 +0100 Subject: [PATCH 1/2] Create(Gesture): there are problems with gestures --- Sources/KinectUtils/Gesture.cs | 4 ++-- Sources/LibMyGesturesBank/AllGesturesFactory.cs | 1 + Sources/LibMyGesturesBank/ClapHands.cs | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/KinectUtils/Gesture.cs b/Sources/KinectUtils/Gesture.cs index 9c850d8..34011c8 100644 --- a/Sources/KinectUtils/Gesture.cs +++ b/Sources/KinectUtils/Gesture.cs @@ -9,7 +9,7 @@ namespace KinectUtils { public abstract class Gesture : BaseGesture { - public bool IsRecognitionRunning { get; set; } + public bool IsRecognitionRunning { get; set; } = false; protected int MinNbOfFrames = 10; // Exemple de valeur, ajustez selon le geste protected int MaxNbOfFrames = 50; // Exemple de valeur, ajustez selon le geste @@ -22,7 +22,6 @@ namespace KinectUtils if (TestInitialConditions(body)) { IsRecognitionRunning = true; - Console.WriteLine(GestureName); currentFrameCount = 0; } } @@ -37,6 +36,7 @@ namespace KinectUtils else if (TestEndConditions(body) && currentFrameCount >= MinNbOfFrames) { OnGestureRecognized(body); + Console.WriteLine(GestureName); IsRecognitionRunning = false; } } diff --git a/Sources/LibMyGesturesBank/AllGesturesFactory.cs b/Sources/LibMyGesturesBank/AllGesturesFactory.cs index 5802cfe..dee370c 100644 --- a/Sources/LibMyGesturesBank/AllGesturesFactory.cs +++ b/Sources/LibMyGesturesBank/AllGesturesFactory.cs @@ -18,6 +18,7 @@ namespace MyGesturesBank new PostureHandUp(), new RightHandUp(), new TwoHandsDragon(), + new ClapHands(), // Ajoutez d'autres gestes ici }; } diff --git a/Sources/LibMyGesturesBank/ClapHands.cs b/Sources/LibMyGesturesBank/ClapHands.cs index dc01856..b56ad47 100644 --- a/Sources/LibMyGesturesBank/ClapHands.cs +++ b/Sources/LibMyGesturesBank/ClapHands.cs @@ -3,10 +3,11 @@ using Microsoft.Kinect; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; -namespace LibMyGesturesBank +namespace MyGesturesBank { public class ClapHands : Gesture { @@ -22,7 +23,7 @@ namespace LibMyGesturesBank var handLeft = body.Joints[JointType.HandLeft].Position; // Vérifie si les mains sont à une hauteur similaire et devant le corps - return Math.Abs(handRight.Y - handLeft.Y) < 0.1f && handRight.Z < 0.5f && handLeft.Z < 0.5f; + return Math.Abs(handRight.Y - handLeft.Y) < 0.5f && handRight.Z < 0.5f && handLeft.Z < 0.5f; } protected override bool TestRunningGesture(Body body) -- 2.36.3 From 94405148f6958f71a3b045fd7794626d3d18d054 Mon Sep 17 00:00:00 2001 From: Louis DUFOUR Date: Sat, 17 Feb 2024 18:39:26 +0100 Subject: [PATCH 2/2] Format(Gesture): en rend conforme le code au convention de CSharpier --- Sources/KinectUtils/BaseGesture.cs | 1 - Sources/KinectUtils/BaseMapping.cs | 10 +- Sources/KinectUtils/Gesture.cs | 17 +- Sources/KinectUtils/GestureManager.cs | 27 +-- Sources/KinectUtils/IGestureFactory.cs | 2 +- Sources/Lib/BodyImageStream.cs | 164 ++++++++++-------- Sources/Lib/ColorAndBodyImageStream.cs | 53 ++++-- Sources/Lib/ColorImageStream.cs | 45 +++-- Sources/Lib/DepthImageStream.cs | 61 ++++--- Sources/Lib/InfraredImageStream.cs | 44 ++--- Sources/Lib/KinectManager.cs | 24 ++- Sources/Lib/KinectStream.cs | 2 +- Sources/Lib/KinectStreamsFactory.cs | 24 +-- Sources/LibMyGesturesBank/ClapHands.cs | 6 +- Sources/LibMyGesturesBank/PostureHandUp.cs | 3 +- .../LibMyGesturesBank/PostureHandsOnHead .cs | 6 +- Sources/LibMyGesturesBank/RightHandUp.cs | 2 +- Sources/LibMyGesturesBank/SwipeRightHand.cs | 3 +- Sources/LibMyGesturesBank/TwoHandsDragon.cs | 2 +- Sources/WpfApp/App.xaml.cs | 4 +- Sources/WpfApp/Bone.cs | 22 +-- Sources/WpfApp/Bones.cs | 4 +- Sources/WpfApp/MainWindow.xaml.cs | 10 +- Sources/WpfApp/Properties/AssemblyInfo.cs | 9 +- Sources/WpfApp/StreamTemplateSelector.cs | 6 +- 25 files changed, 318 insertions(+), 233 deletions(-) diff --git a/Sources/KinectUtils/BaseGesture.cs b/Sources/KinectUtils/BaseGesture.cs index 5b69899..1c6fbc0 100644 --- a/Sources/KinectUtils/BaseGesture.cs +++ b/Sources/KinectUtils/BaseGesture.cs @@ -32,5 +32,4 @@ namespace KinectUtils GestureName = gestureName; } } - } diff --git a/Sources/KinectUtils/BaseMapping.cs b/Sources/KinectUtils/BaseMapping.cs index 8d16076..a37b1a4 100644 --- a/Sources/KinectUtils/BaseMapping.cs +++ b/Sources/KinectUtils/BaseMapping.cs @@ -13,21 +13,25 @@ namespace KinectUtils { throw new NotImplementedException(); } + public void SubscribeToEndGesture(BaseGesture gesture) { throw new NotImplementedException(); } + public void SubscribeToToggleGesture(BaseGesture gesture) { throw new NotImplementedException(); } + protected T Mapping(Body body) { throw new NotImplementedException(); } - public bool TestMapping(Body body, out T output) - { - throw new NotImplementedException(); + + public bool TestMapping(Body body, out T output) + { + throw new NotImplementedException(); } } } diff --git a/Sources/KinectUtils/Gesture.cs b/Sources/KinectUtils/Gesture.cs index 34011c8..1e0b757 100644 --- a/Sources/KinectUtils/Gesture.cs +++ b/Sources/KinectUtils/Gesture.cs @@ -10,7 +10,7 @@ namespace KinectUtils public abstract class Gesture : BaseGesture { public bool IsRecognitionRunning { get; set; } = false; - + protected int MinNbOfFrames = 10; // Exemple de valeur, ajustez selon le geste protected int MaxNbOfFrames = 50; // Exemple de valeur, ajustez selon le geste private int currentFrameCount = 0; @@ -29,7 +29,11 @@ namespace KinectUtils { currentFrameCount++; - if (!TestPosture(body) || !TestRunningGesture(body) || currentFrameCount > MaxNbOfFrames) + if ( + !TestPosture(body) + || !TestRunningGesture(body) + || currentFrameCount > MaxNbOfFrames + ) { IsRecognitionRunning = false; } @@ -42,10 +46,9 @@ namespace KinectUtils } } - abstract protected bool TestInitialConditions(Body body); - abstract protected bool TestPosture(Body body); - abstract protected bool TestRunningGesture(Body body); - abstract protected bool TestEndConditions(Body body); + protected abstract bool TestInitialConditions(Body body); + protected abstract bool TestPosture(Body body); + protected abstract bool TestRunningGesture(Body body); + protected abstract bool TestEndConditions(Body body); } } - diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index ddce0a3..d4761cf 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -12,8 +12,8 @@ namespace KinectUtils { public static class GestureManager { - static private BodyFrameReader bodyFrameReader; - static private Body[] bodies; + private static BodyFrameReader bodyFrameReader; + private static Body[] bodies; static event EventHandler GestureRecognized; static KinectManager KinectManager { get; set; } static List KnownGestures { get; set; } = new List(); @@ -26,6 +26,7 @@ namespace KinectUtils AddGesture(gesture); } } + public static void AddGesture(BaseGesture gesture) { if (!KnownGestures.Contains(gesture)) @@ -34,6 +35,7 @@ namespace KinectUtils gesture.GestureRecognized += (sender, e) => GestureRecognized?.Invoke(sender, e); } } + public static void RemoveGesture(BaseGesture gesture) { if (KnownGestures.Contains(gesture)) @@ -43,34 +45,32 @@ namespace KinectUtils } } - static public void StartAcquiringFrames(KinectManager kinectManager) + public static void StartAcquiringFrames(KinectManager kinectManager) { bodyFrameReader = kinectManager.Sensor.BodyFrameSource.OpenReader(); bodyFrameReader.FrameArrived += Reader_BodyFrameArrived; } - static private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) + private static void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) { using (var bodyframe = e.FrameReference.AcquireFrame()) { if (bodyframe != null) { - - if(bodies == null) + if (bodies == null) { bodies = new Body[bodyframe.BodyCount]; } bodyframe.GetAndRefreshBodyData(bodies); - if (bodies != null) + if (bodies != null) + { + foreach (var body in bodies) { - foreach (var body in bodies) + if (body.IsTracked) { - if (body.IsTracked) + foreach (var gesture in KnownGestures) { - foreach (var gesture in KnownGestures) - { - gesture.TestGesture(body); - } + gesture.TestGesture(body); } } } @@ -79,3 +79,4 @@ namespace KinectUtils } } } +} diff --git a/Sources/KinectUtils/IGestureFactory.cs b/Sources/KinectUtils/IGestureFactory.cs index b01155f..7599387 100644 --- a/Sources/KinectUtils/IGestureFactory.cs +++ b/Sources/KinectUtils/IGestureFactory.cs @@ -8,6 +8,6 @@ namespace KinectUtils { public interface IGestureFactory { - IEnumerable CreateGestures(); + IEnumerable CreateGestures(); } } diff --git a/Sources/Lib/BodyImageStream.cs b/Sources/Lib/BodyImageStream.cs index eea0d35..c601f04 100644 --- a/Sources/Lib/BodyImageStream.cs +++ b/Sources/Lib/BodyImageStream.cs @@ -28,6 +28,7 @@ namespace Lib get { return bodies; } private set { bodies = value; } } + public override void Stop() { if (Reader != null) @@ -35,13 +36,22 @@ namespace Lib Reader.Dispose(); Reader = null; } - } - public BodyImageStream(KinectManager kinectmanager, Canvas skeletonCanvas) : base(kinectmanager) + public BodyImageStream(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); + 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; @@ -56,7 +66,10 @@ namespace Lib Joint joint1 = body.Joints[JointType1]; // ne dessinez que si les deux joints sont suivis - if (joint0.TrackingState == TrackingState.Tracked && joint1.TrackingState == TrackingState.Tracked) + if ( + joint0.TrackingState == TrackingState.Tracked + && joint1.TrackingState == TrackingState.Tracked + ) { Line bone = new Line { @@ -74,7 +87,10 @@ namespace Lib private Point MapJointToScreen(Joint joint) { - ColorSpacePoint colorPoint = this.KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position); + ColorSpacePoint colorPoint = + this.KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace( + joint.Position + ); // Gestion des coordonnées infinies float x = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X; @@ -84,86 +100,86 @@ namespace Lib } 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) { - // 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) { - Joint joint = body.Joints[JointType]; - if (joint.TrackingState == TrackingState.Tracked) - { - DrawJoint(MapJointToScreen(joint)); - } + DrawJoint(MapJointToScreen(joint)); } } + } - private void DrawJoint(Point point) + private void DrawJoint(Point point) + { + Ellipse ellipse = new Ellipse { - Ellipse ellipse = new Ellipse - { - Width = 10, - Height = 10, - Fill = new SolidColorBrush(Colors.Red) - }; + 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); + Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2); + Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2); - Canvas.Children.Add(ellipse); - } - private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) + Canvas.Children.Add(ellipse); + } + + private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) + { + using (var bodyframe = e.FrameReference.AcquireFrame()) { - using (var bodyframe = e.FrameReference.AcquireFrame()) + if (bodyframe != null) { - if (bodyframe != null) - { - bodyframe.GetAndRefreshBodyData(this.bodies); + bodyframe.GetAndRefreshBodyData(this.bodies); // nettoyer le Canvas avant de dessiner - if (Canvas != null) + if (Canvas != null) + { + Canvas.Children.Clear(); + foreach (var body in this.bodies) { - Canvas.Children.Clear(); - foreach (var body in this.bodies) + if (body.IsTracked) { - if (body.IsTracked) - { - // dessiner le squelette - drawskeleton(body); - } + // dessiner le squelette + drawskeleton(body); } } } @@ -171,4 +187,4 @@ namespace Lib } } } - +} diff --git a/Sources/Lib/ColorAndBodyImageStream.cs b/Sources/Lib/ColorAndBodyImageStream.cs index f410334..a70e62f 100644 --- a/Sources/Lib/ColorAndBodyImageStream.cs +++ b/Sources/Lib/ColorAndBodyImageStream.cs @@ -22,7 +22,8 @@ namespace Lib } private ColorFrameReader _colorReader; - public ColorFrameReader ColorReader { + public ColorFrameReader ColorReader + { get { return _colorReader; } private set { _colorReader = value; } } @@ -33,6 +34,7 @@ namespace Lib get { return bodies; } private set { bodies = value; } } + public override void Stop() { if (Reader != null) @@ -40,18 +42,27 @@ namespace Lib Reader.Dispose(); Reader = null; } - if(ColorReader != null) + if (ColorReader != null) { ColorReader.Dispose(); ColorReader = null; } - } - public ColorAndBodyImageStream(KinectManager kinectmanager, Canvas skeletonCanvas) : base(kinectmanager) + 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); + 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; ColorReader = KinectManager.Sensor.ColorFrameSource.OpenReader(); @@ -67,7 +78,10 @@ namespace Lib Joint joint1 = body.Joints[JointType1]; // ne dessinez que si les deux joints sont suivis - if (joint0.TrackingState == TrackingState.Tracked && joint1.TrackingState == TrackingState.Tracked) + if ( + joint0.TrackingState == TrackingState.Tracked + && joint1.TrackingState == TrackingState.Tracked + ) { Line bone = new Line { @@ -85,7 +99,10 @@ namespace Lib private Point MapJointToScreen(Joint joint) { - ColorSpacePoint colorPoint = this.KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position); + ColorSpacePoint colorPoint = + this.KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace( + joint.Position + ); // Gestion des coordonnées infinies float x = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X; @@ -157,6 +174,7 @@ namespace Lib Canvas.Children.Add(ellipse); } + private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) { using (var bodyframe = e.FrameReference.AcquireFrame()) @@ -178,6 +196,7 @@ namespace Lib } } } + private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) @@ -193,14 +212,22 @@ namespace Lib this.Bitmap.Lock(); // Vérifier si la taille de l'image a changé - if ((colorFrameDescription.Width == this.Bitmap.PixelWidth) && (colorFrameDescription.Height == this.Bitmap.PixelHeight)) + if ( + (colorFrameDescription.Width == this.Bitmap.PixelWidth) + && (colorFrameDescription.Height == this.Bitmap.PixelHeight) + ) { colorFrame.CopyConvertedFrameDataToIntPtr( this.Bitmap.BackBuffer, - (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), - ColorImageFormat.Bgra); - - this.Bitmap.AddDirtyRect(new Int32Rect(0, 0, this.Bitmap.PixelWidth, this.Bitmap.PixelHeight)); + (uint)( + colorFrameDescription.Width * colorFrameDescription.Height * 4 + ), + ColorImageFormat.Bgra + ); + + this.Bitmap.AddDirtyRect( + new Int32Rect(0, 0, this.Bitmap.PixelWidth, this.Bitmap.PixelHeight) + ); } this.Bitmap.Unlock(); diff --git a/Sources/Lib/ColorImageStream.cs b/Sources/Lib/ColorImageStream.cs index 2580220..d28a374 100644 --- a/Sources/Lib/ColorImageStream.cs +++ b/Sources/Lib/ColorImageStream.cs @@ -9,19 +9,26 @@ namespace Lib public class ColorImageStream : KinectStream { private ColorFrameReader reader; - public ColorFrameReader Reader { - get { - return reader; - } - set - { - reader = value; - } + public ColorFrameReader Reader + { + get { return reader; } + set { reader = value; } } - public ColorImageStream(KinectManager kinectManager) : base(kinectManager) + + public ColorImageStream(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); + 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.ColorFrameSource.OpenReader(); reader.FrameArrived += Reader_ColorFrameArrived; } @@ -50,14 +57,22 @@ namespace Lib this.Bitmap.Lock(); // Vérifier si la taille de l'image a changé - if ((colorFrameDescription.Width == this.Bitmap.PixelWidth) && (colorFrameDescription.Height == this.Bitmap.PixelHeight)) + if ( + (colorFrameDescription.Width == this.Bitmap.PixelWidth) + && (colorFrameDescription.Height == this.Bitmap.PixelHeight) + ) { colorFrame.CopyConvertedFrameDataToIntPtr( this.Bitmap.BackBuffer, - (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4), - ColorImageFormat.Bgra); + (uint)( + colorFrameDescription.Width * colorFrameDescription.Height * 4 + ), + ColorImageFormat.Bgra + ); - this.Bitmap.AddDirtyRect(new Int32Rect(0, 0, this.Bitmap.PixelWidth, this.Bitmap.PixelHeight)); + this.Bitmap.AddDirtyRect( + new Int32Rect(0, 0, this.Bitmap.PixelWidth, this.Bitmap.PixelHeight) + ); } this.Bitmap.Unlock(); diff --git a/Sources/Lib/DepthImageStream.cs b/Sources/Lib/DepthImageStream.cs index 1cdafd9..649f7c4 100644 --- a/Sources/Lib/DepthImageStream.cs +++ b/Sources/Lib/DepthImageStream.cs @@ -15,33 +15,33 @@ namespace Lib private DepthFrameReader reader; public DepthFrameReader Reader { - get - { - return reader; - } - set - { - reader = value; - } + get { return reader; } + set { reader = value; } } private byte[] depthPixels; - public byte[] DepthPixels { - get { - return depthPixels; - } - set - { - depthPixels = value; - } + public byte[] DepthPixels + { + get { return depthPixels; } + set { depthPixels = value; } } - public DepthImageStream(KinectManager kinectManager) : base(kinectManager) + + public DepthImageStream(KinectManager kinectManager) + : base(kinectManager) { var frameDescription = KinectManager.Sensor.DepthFrameSource.FrameDescription; - this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null); + this.Bitmap = new WriteableBitmap( + frameDescription.Width, + frameDescription.Height, + 96.0, + 96.0, + PixelFormats.Gray8, + null + ); reader = KinectManager.Sensor.DepthFrameSource.OpenReader(); reader.FrameArrived += Reader_DepthFrameArrived; DepthPixels = new byte[frameDescription.Width * frameDescription.Height]; } + private void Reader_DepthFrameArrived(object sender, DepthFrameArrivedEventArgs e) { using (DepthFrame depthFrame = e.FrameReference.AcquireFrame()) @@ -55,14 +55,25 @@ namespace Lib depthFrame.CopyFrameDataToArray(depthData); // Traitez les données de profondeur - ProcessDepthFrameData(depthData, depthFrameDescription.LengthInPixels, depthFrame.DepthMinReliableDistance, depthFrame.DepthMaxReliableDistance); + ProcessDepthFrameData( + depthData, + depthFrameDescription.LengthInPixels, + depthFrame.DepthMinReliableDistance, + depthFrame.DepthMaxReliableDistance + ); // Mettez à jour le bitmap de profondeur this.Bitmap.WritePixels( - new Int32Rect(0, 0, depthFrameDescription.Width, depthFrameDescription.Height), + new Int32Rect( + 0, + 0, + depthFrameDescription.Width, + depthFrameDescription.Height + ), this.depthPixels, depthFrameDescription.Width, - 0); + 0 + ); } } } @@ -76,7 +87,12 @@ namespace Lib } } - private void ProcessDepthFrameData(ushort[] depthData, uint depthFrameDataSize, ushort minDepth, ushort maxDepth) + private void ProcessDepthFrameData( + ushort[] depthData, + uint depthFrameDataSize, + ushort minDepth, + ushort maxDepth + ) { // Convertir les données de profondeur en niveaux de gris for (int i = 0; i < depthFrameDataSize; ++i) @@ -85,6 +101,5 @@ namespace Lib DepthPixels[i] = (byte)(depth >= minDepth && depth <= maxDepth ? (depth % 256) : 0); } } - } } diff --git a/Sources/Lib/InfraredImageStream.cs b/Sources/Lib/InfraredImageStream.cs index fd0fef8..c566c89 100644 --- a/Sources/Lib/InfraredImageStream.cs +++ b/Sources/Lib/InfraredImageStream.cs @@ -16,30 +16,27 @@ namespace Lib private byte[] infraredPixels; public byte[] InfraredPixels { - get - { - return infraredPixels; - } - private set - { - infraredPixels = value; - } + get { return infraredPixels; } + private set { infraredPixels = value; } } public InfraredFrameReader Reader { - get - { - return reader; - } - private set - { - reader = value; - } + get { return reader; } + private set { reader = value; } } - public InfraredImageStream(KinectManager kinectManager) : base(kinectManager) + + public InfraredImageStream(KinectManager kinectManager) + : base(kinectManager) { var frameDescription = KinectManager.Sensor.InfraredFrameSource.FrameDescription; - this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null); + this.Bitmap = new WriteableBitmap( + frameDescription.Width, + frameDescription.Height, + 96.0, + 96.0, + PixelFormats.Gray8, + null + ); reader = KinectManager.Sensor.InfraredFrameSource.OpenReader(); reader.FrameArrived += Reader_InfraredFrameArrived; infraredPixels = new byte[frameDescription.Width * frameDescription.Height]; @@ -53,6 +50,7 @@ namespace Lib reader = null; } } + private void ProcessInfraredFrameData(ushort[] frameData, uint frameDataSize) { // Convertir les données infrarouges en niveaux de gris @@ -81,10 +79,16 @@ namespace Lib // Mettez à jour le bitmap infrarouge this.Bitmap.WritePixels( - new Int32Rect(0, 0, infraredFrameDescription.Width, infraredFrameDescription.Height), + new Int32Rect( + 0, + 0, + infraredFrameDescription.Width, + infraredFrameDescription.Height + ), this.infraredPixels, infraredFrameDescription.Width, - 0); + 0 + ); } } } diff --git a/Sources/Lib/KinectManager.cs b/Sources/Lib/KinectManager.cs index dbe75da..9e06c85 100644 --- a/Sources/Lib/KinectManager.cs +++ b/Sources/Lib/KinectManager.cs @@ -8,10 +8,9 @@ namespace Lib public class KinectManager : INotifyPropertyChanged { private KinectSensor sensor; - public KinectSensor Sensor { - get { - return sensor; - } + public KinectSensor Sensor + { + get { return sensor; } set { sensor = value; @@ -31,6 +30,7 @@ namespace Lib } public event PropertyChangedEventHandler PropertyChanged; + protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); @@ -46,16 +46,20 @@ namespace Lib sensor.IsAvailableChanged += KinectSensor_IsAvailableChanged; } } + public void StartSensor() { - if (sensor != null && !sensor.IsOpen) { + if (sensor != null && !sensor.IsOpen) + { sensor.Open(); } - else { + else + { sensor = KinectSensor.GetDefault(); sensor.Open(); } } + public void StopSensor() { if (sensor != null) @@ -64,6 +68,7 @@ namespace Lib sensor = null; } } + public bool Status { get { return Sensor != null && Sensor.IsAvailable; } @@ -72,9 +77,10 @@ namespace Lib public string StatusText { get { return _statusText; } - set { + set + { _statusText = value; - if(this.PropertyChanged != null) + if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText")); } @@ -110,4 +116,4 @@ namespace Lib } } } -} \ No newline at end of file +} diff --git a/Sources/Lib/KinectStream.cs b/Sources/Lib/KinectStream.cs index 0be4e5a..dea4da0 100644 --- a/Sources/Lib/KinectStream.cs +++ b/Sources/Lib/KinectStream.cs @@ -39,6 +39,7 @@ namespace Lib } public event PropertyChangedEventHandler PropertyChanged; + public void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); @@ -60,6 +61,5 @@ namespace Lib } public abstract void Stop(); - } } diff --git a/Sources/Lib/KinectStreamsFactory.cs b/Sources/Lib/KinectStreamsFactory.cs index 1571530..ce7c603 100644 --- a/Sources/Lib/KinectStreamsFactory.cs +++ b/Sources/Lib/KinectStreamsFactory.cs @@ -10,7 +10,8 @@ namespace Lib public class KinectStreamsFactory { private KinectManager _kinectManager; - public KinectManager KinectManager { + public KinectManager KinectManager + { get { return _kinectManager; } private set { _kinectManager = value; } } @@ -22,18 +23,21 @@ namespace Lib private set { _streamFactory = value; } } - public KinectStreamsFactory(KinectManager kinect,Canvas SkeletonCanvas) + public KinectStreamsFactory(KinectManager kinect, Canvas SkeletonCanvas) { _kinectManager = kinect; // Initialisation de la fabrique avec les fonctions de création pour chaque type de flux. StreamFactory = new Dictionary> - { - { KinectStreams.Color, () => new ColorImageStream(KinectManager) }, - { KinectStreams.Depth, () => new DepthImageStream(KinectManager) }, - { KinectStreams.IR, () => new InfraredImageStream(KinectManager) }, - { KinectStreams.Body, () => new BodyImageStream(KinectManager,SkeletonCanvas) }, - { KinectStreams.ColorAndBody, () => new ColorAndBodyImageStream(KinectManager,SkeletonCanvas) } - }; + { + { KinectStreams.Color, () => new ColorImageStream(KinectManager) }, + { KinectStreams.Depth, () => new DepthImageStream(KinectManager) }, + { KinectStreams.IR, () => new InfraredImageStream(KinectManager) }, + { KinectStreams.Body, () => new BodyImageStream(KinectManager, SkeletonCanvas) }, + { + KinectStreams.ColorAndBody, + () => new ColorAndBodyImageStream(KinectManager, SkeletonCanvas) + } + }; } public KinectStream this[KinectStreams stream] @@ -51,4 +55,4 @@ namespace Lib } } } -} \ No newline at end of file +} diff --git a/Sources/LibMyGesturesBank/ClapHands.cs b/Sources/LibMyGesturesBank/ClapHands.cs index b56ad47..5df0fda 100644 --- a/Sources/LibMyGesturesBank/ClapHands.cs +++ b/Sources/LibMyGesturesBank/ClapHands.cs @@ -23,7 +23,9 @@ namespace MyGesturesBank var handLeft = body.Joints[JointType.HandLeft].Position; // Vérifie si les mains sont à une hauteur similaire et devant le corps - return Math.Abs(handRight.Y - handLeft.Y) < 0.5f && handRight.Z < 0.5f && handLeft.Z < 0.5f; + return Math.Abs(handRight.Y - handLeft.Y) < 0.5f + && handRight.Z < 0.5f + && handLeft.Z < 0.5f; } protected override bool TestRunningGesture(Body body) @@ -40,7 +42,7 @@ namespace MyGesturesBank // Le clap est un geste instantané, donc si les conditions sont remplies une fois, le geste est considéré comme terminé return true; } + public override string GestureName => "Clap Hands"; } - } diff --git a/Sources/LibMyGesturesBank/PostureHandUp.cs b/Sources/LibMyGesturesBank/PostureHandUp.cs index 661249b..46e14e8 100644 --- a/Sources/LibMyGesturesBank/PostureHandUp.cs +++ b/Sources/LibMyGesturesBank/PostureHandUp.cs @@ -13,8 +13,7 @@ namespace MyGesturesBank return handRight.Y > head.Y || handLeft.Y > head.Y; } + public override string GestureName => "Posture Hand Up"; } - - } diff --git a/Sources/LibMyGesturesBank/PostureHandsOnHead .cs b/Sources/LibMyGesturesBank/PostureHandsOnHead .cs index a1835f2..5cf603d 100644 --- a/Sources/LibMyGesturesBank/PostureHandsOnHead .cs +++ b/Sources/LibMyGesturesBank/PostureHandsOnHead .cs @@ -15,10 +15,10 @@ namespace MyGesturesBank // Ajustez les seuils selon la précision souhaitée var threshold = 0.1f; // Seuil pour ajuster la précision de la détection - return Math.Abs(handRight.Y - head.Y) < threshold && Math.Abs(handLeft.Y - head.Y) < threshold; + return Math.Abs(handRight.Y - head.Y) < threshold + && Math.Abs(handLeft.Y - head.Y) < threshold; } + public override string GestureName => "Posture Hands On Head"; } - - } diff --git a/Sources/LibMyGesturesBank/RightHandUp.cs b/Sources/LibMyGesturesBank/RightHandUp.cs index 3d8c90b..986f533 100644 --- a/Sources/LibMyGesturesBank/RightHandUp.cs +++ b/Sources/LibMyGesturesBank/RightHandUp.cs @@ -17,7 +17,7 @@ namespace MyGesturesBank return handRight.Y > head.Y; } + public override string GestureName => "Right Hand Up"; } - } diff --git a/Sources/LibMyGesturesBank/SwipeRightHand.cs b/Sources/LibMyGesturesBank/SwipeRightHand.cs index 44291b0..0b80634 100644 --- a/Sources/LibMyGesturesBank/SwipeRightHand.cs +++ b/Sources/LibMyGesturesBank/SwipeRightHand.cs @@ -10,7 +10,6 @@ namespace MyGesturesBank { public class SwipeRightHand : Gesture { - private float previousX = float.NaN; protected override bool TestInitialConditions(Body body) @@ -55,7 +54,7 @@ namespace MyGesturesBank // Condition de fin : la main droite est bien à droite de la base de la colonne vertébrale return handRight.X > spineBase.X + 0.5f; // Ajustez cette valeur selon le besoin } + public override string GestureName => "Swipe Right Hand"; } - } diff --git a/Sources/LibMyGesturesBank/TwoHandsDragon.cs b/Sources/LibMyGesturesBank/TwoHandsDragon.cs index 6b5400a..0f30adc 100644 --- a/Sources/LibMyGesturesBank/TwoHandsDragon.cs +++ b/Sources/LibMyGesturesBank/TwoHandsDragon.cs @@ -18,7 +18,7 @@ namespace MyGesturesBank return handRight.Y > head.Y && handLeft.Y > head.Y; } + public override string GestureName => "Two Hands Dragon"; } - } diff --git a/Sources/WpfApp/App.xaml.cs b/Sources/WpfApp/App.xaml.cs index ebc8edd..be40af0 100644 --- a/Sources/WpfApp/App.xaml.cs +++ b/Sources/WpfApp/App.xaml.cs @@ -11,7 +11,5 @@ namespace WpfApp /// /// Logique d'interaction pour App.xaml /// - public partial class App : Application - { - } + public partial class App : Application { } } diff --git a/Sources/WpfApp/Bone.cs b/Sources/WpfApp/Bone.cs index 4b9bf97..de5c550 100644 --- a/Sources/WpfApp/Bone.cs +++ b/Sources/WpfApp/Bone.cs @@ -11,9 +11,10 @@ namespace WpfApp public class Bone : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; + private void OnPropertyChanged(string propName) { - if(PropertyChanged != null) + if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } @@ -23,23 +24,17 @@ namespace WpfApp public double Top { get { return _top; } - set { + set + { _top = value; OnPropertyChanged(nameof(Top)); } } - public double Left - { - get; - set; - } + public double Left { get; set; } public double Diameter { - get - { - return _diameter; - } + get { return _diameter; } set { _diameter = value; @@ -47,9 +42,6 @@ namespace WpfApp } } private double _diameter; - public SolidColorBrush ColorBrush { - get; - set; - } + public SolidColorBrush ColorBrush { get; set; } } } diff --git a/Sources/WpfApp/Bones.cs b/Sources/WpfApp/Bones.cs index e56fc98..f1b6706 100644 --- a/Sources/WpfApp/Bones.cs +++ b/Sources/WpfApp/Bones.cs @@ -9,9 +9,9 @@ namespace WpfApp { class Bones : Collection { - public Bones() { + public Bones() + { Add(new Bone { }); } - } } diff --git a/Sources/WpfApp/MainWindow.xaml.cs b/Sources/WpfApp/MainWindow.xaml.cs index 823fe81..6aa8bc6 100644 --- a/Sources/WpfApp/MainWindow.xaml.cs +++ b/Sources/WpfApp/MainWindow.xaml.cs @@ -22,7 +22,7 @@ namespace WpfApp /// /// Logique d'interaction pour MainWindow.xaml /// - public partial class MainWindow : Window,INotifyPropertyChanged + public partial class MainWindow : Window, INotifyPropertyChanged { private KinectManager kinectManager; public KinectManager KinectManager @@ -34,7 +34,8 @@ namespace WpfApp public KinectStream CurrentKinectStream { get { return _currentKinectStream; } - set { + set + { _currentKinectStream = value; OnPropertyChanged(nameof(CurrentKinectStream)); } @@ -63,10 +64,8 @@ namespace WpfApp Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText); CurrentKinectStream.Start(); Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText); - } - private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText); @@ -104,6 +103,7 @@ namespace WpfApp Debug.WriteLine(CurrentKinectStream.GetType().Name); CurrentKinectStream.Start(); } + private void ToColorAndBodyImageStream(object sender, RoutedEventArgs e) { CurrentKinectStream.Stop(); @@ -112,4 +112,4 @@ namespace WpfApp CurrentKinectStream.Start(); } } -} \ No newline at end of file +} diff --git a/Sources/WpfApp/Properties/AssemblyInfo.cs b/Sources/WpfApp/Properties/AssemblyInfo.cs index a44044c..bf0b006 100644 --- a/Sources/WpfApp/Properties/AssemblyInfo.cs +++ b/Sources/WpfApp/Properties/AssemblyInfo.cs @@ -33,14 +33,13 @@ using System.Windows; [assembly: ThemeInfo( ResourceDictionaryLocation.None, //où se trouvent les dictionnaires de ressources spécifiques à un thème - //(utilisé si une ressource est introuvable dans la page, - // ou dictionnaires de ressources de l'application) + //(utilisé si une ressource est introuvable dans la page, + // ou dictionnaires de ressources de l'application) ResourceDictionaryLocation.SourceAssembly //où se trouve le dictionnaire de ressources générique - //(utilisé si une ressource est introuvable dans la page, - // dans l'application ou dans l'un des dictionnaires de ressources spécifiques à un thème) +//(utilisé si une ressource est introuvable dans la page, +// dans l'application ou dans l'un des dictionnaires de ressources spécifiques à un thème) )] - // Les informations de version pour un assembly se composent des quatre valeurs suivantes : // // Version principale diff --git a/Sources/WpfApp/StreamTemplateSelector.cs b/Sources/WpfApp/StreamTemplateSelector.cs index c22b909..ed7df77 100644 --- a/Sources/WpfApp/StreamTemplateSelector.cs +++ b/Sources/WpfApp/StreamTemplateSelector.cs @@ -16,8 +16,10 @@ namespace WpfApp public override DataTemplate SelectTemplate(object item, DependencyObject container) { - if (item is BodyImageStream) return ColorAndBodyImageStreamTemplate; - else return OtherStreamTemplate; + if (item is BodyImageStream) + return ColorAndBodyImageStreamTemplate; + else + return OtherStreamTemplate; } } } -- 2.36.3