From beff9b59540b437262a6f109496772aeafe75929 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Sat, 3 Feb 2024 12:26:21 +0100 Subject: [PATCH 01/17] Add gestures to the test methods --- Sources/ConsoleApp/ConsoleApp.csproj | 9 +++++ Sources/ConsoleApp/Program.cs | 34 +++++++++++++++++-- Sources/KinectUtils/GestureManager.cs | 4 +++ Sources/Lib/BodyImageStream.cs | 18 +++++----- .../LibMyGesturesBank/MyGesturesBank.csproj | 2 -- .../LibMyGesturesBank/PostureHandsOnHead .cs | 5 +-- Sources/LibMyGesturesBank/RightHandUp.cs | 12 ------- Sources/LibMyGesturesBank/TwoHandsDragon.cs | 12 ------- 8 files changed, 58 insertions(+), 38 deletions(-) delete mode 100644 Sources/LibMyGesturesBank/RightHandUp.cs delete mode 100644 Sources/LibMyGesturesBank/TwoHandsDragon.cs diff --git a/Sources/ConsoleApp/ConsoleApp.csproj b/Sources/ConsoleApp/ConsoleApp.csproj index 11b775b..4d45df5 100644 --- a/Sources/ConsoleApp/ConsoleApp.csproj +++ b/Sources/ConsoleApp/ConsoleApp.csproj @@ -36,6 +36,7 @@ ..\packages\Microsoft.Kinect.2.0.1410.19000\lib\net45\Microsoft.Kinect.dll + @@ -54,6 +55,14 @@ + + {2d44487e-f514-4063-9494-2af1e8c9e9c8} + KinectUtils + + + {2496DFB1-EB55-47A1-A780-211E079B289D} + MyGesturesBank + {0751c83e-7845-4e5f-a5d3-e11aba393aca} Lib diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index fb7ff8d..b8f5ea7 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -1,11 +1,14 @@ using Lib; using Microsoft.Kinect; +using MyGesturesBank; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Windows.Controls; namespace ConsoleApp { @@ -13,6 +16,32 @@ namespace ConsoleApp { static void Main(string[] args) { + KinectManager kinectManager = new KinectManager(); + Canvas skeletonCanvas = null; + KinectStreamsFactory Factory = new KinectStreamsFactory(kinectManager, skeletonCanvas); + BodyImageStream CurrentKinectStream = (BodyImageStream)Factory[KinectStreams.Body]; + CurrentKinectStream.Start(); + PostureHandUp handUpPosture = new PostureHandUp(); + PostureHandsOnHead handsOnHeadPosture = new PostureHandsOnHead(); + handUpPosture.GestureRecognized += (sender, e) => + { + Console.WriteLine("Posture Hand Up reconnue !"); + }; + handsOnHeadPosture.GestureRecognized += (sender, e) => + { + Console.WriteLine("Posture Hands On Head reconnue !"); + }; + Body body = null; + while (true) + { + body = CurrentKinectStream.Bodies.FirstOrDefault(); + if (body != null) + { + handUpPosture.TestGesture(body); + handsOnHeadPosture.TestGesture(body); + } + Thread.Sleep(50); + } // KinectManager kinectManager = new KinectManager(); // if (kinectManager.StartSensor()) // { @@ -38,7 +67,7 @@ namespace ConsoleApp // // Boucle pour tester les postures // while (true) // { - // Body body = kinectManager.GetNextBody(); // Méthode fictive pour obtenir les données du corps + // Body body = kinecManager.GetNextBody(); // Méthode fictive pour obtenir les données du corps // if (body != null) // { // handUpPosture.TestGesture(body); @@ -47,7 +76,8 @@ namespace ConsoleApp // Thread.Sleep(50); // Une petite pause pour ne pas surcharger le CPU // } - } + //} + } } } diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index d9e08a5..339375f 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -32,5 +32,9 @@ namespace KinectUtils { throw new NotImplementedException(); } + static public void StopAcquiringFrames() + { + throw new NotImplementedException(); + } } } diff --git a/Sources/Lib/BodyImageStream.cs b/Sources/Lib/BodyImageStream.cs index 8b7b9bc..eea0d35 100644 --- a/Sources/Lib/BodyImageStream.cs +++ b/Sources/Lib/BodyImageStream.cs @@ -153,20 +153,22 @@ namespace Lib if (bodyframe != null) { bodyframe.GetAndRefreshBodyData(this.bodies); - - Canvas.Children.Clear(); // nettoyer le Canvas avant de dessiner - - foreach (var body in this.bodies) + // nettoyer le Canvas avant de dessiner + if (Canvas != null) { - if (body.IsTracked) + Canvas.Children.Clear(); + foreach (var body in this.bodies) { - // dessiner le squelette - drawskeleton(body); + if (body.IsTracked) + { + // dessiner le squelette + drawskeleton(body); + } } } } } } } -} + } diff --git a/Sources/LibMyGesturesBank/MyGesturesBank.csproj b/Sources/LibMyGesturesBank/MyGesturesBank.csproj index 7a43ab1..aedd2ed 100644 --- a/Sources/LibMyGesturesBank/MyGesturesBank.csproj +++ b/Sources/LibMyGesturesBank/MyGesturesBank.csproj @@ -47,8 +47,6 @@ - - diff --git a/Sources/LibMyGesturesBank/PostureHandsOnHead .cs b/Sources/LibMyGesturesBank/PostureHandsOnHead .cs index 7ad19b9..4b8df45 100644 --- a/Sources/LibMyGesturesBank/PostureHandsOnHead .cs +++ b/Sources/LibMyGesturesBank/PostureHandsOnHead .cs @@ -20,10 +20,11 @@ namespace MyGesturesBank private bool IsHandNearHead(Joint hand, Joint head) { // Exemple de condition : la main est à moins de 30 cm de la tête - return Math.Sqrt( + double distance = Math.Sqrt( Math.Pow(hand.Position.X - head.Position.X, 2) + Math.Pow(hand.Position.Y - head.Position.Y, 2) + - Math.Pow(hand.Position.Z - head.Position.Z, 2)) < 0.3; + Math.Pow(hand.Position.Z - head.Position.Z, 2)); + return distance < 0.3 && distance > 0; } public override string GestureName => "Hands On Head"; diff --git a/Sources/LibMyGesturesBank/RightHandUp.cs b/Sources/LibMyGesturesBank/RightHandUp.cs deleted file mode 100644 index 259d3a5..0000000 --- a/Sources/LibMyGesturesBank/RightHandUp.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace LibMyGesturesBank -{ - internal class RightHandUp - { - } -} diff --git a/Sources/LibMyGesturesBank/TwoHandsDragon.cs b/Sources/LibMyGesturesBank/TwoHandsDragon.cs deleted file mode 100644 index a5e9d5f..0000000 --- a/Sources/LibMyGesturesBank/TwoHandsDragon.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace LibMyGesturesBank -{ - internal class TwoHandsDragon - { - } -} -- 2.36.3 From c54a3efd48eadd659f4ece987c57116189ce8dd1 Mon Sep 17 00:00:00 2001 From: "louis.dufour" Date: Wed, 7 Feb 2024 08:15:19 +0100 Subject: [PATCH 02/17] =?UTF-8?q?ToFix(dev):=20d=C3=A9but=20archi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/KinectUtils/AllGesturesFactory.cs | 8 +++- Sources/KinectUtils/GestureManager.cs | 29 +++++++++----- Sources/KinectUtils/KinectUtils.csproj | 4 ++ .../LibMyGesturesBank/MyGesturesBank.csproj | 5 +-- Sources/LibMyGesturesBank/RightHandUp.cs | 2 +- Sources/LibMyGesturesBank/SwipeRightHand.cs | 38 +++++++++++++++++++ Sources/LibMyGesturesBank/TwoHandsDragon.cs | 2 +- 7 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 Sources/LibMyGesturesBank/SwipeRightHand.cs diff --git a/Sources/KinectUtils/AllGesturesFactory.cs b/Sources/KinectUtils/AllGesturesFactory.cs index 970478a..719a15e 100644 --- a/Sources/KinectUtils/AllGesturesFactory.cs +++ b/Sources/KinectUtils/AllGesturesFactory.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using MyGesturesBank; namespace KinectUtils { @@ -10,7 +11,12 @@ namespace KinectUtils { public IEnumerable CreateGestures() { - throw new NotImplementedException(); + return new List + { + new SwipeRightHand(), + new ClapHands() + // Ajoutez d'autres gestes ici + }; } } } diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index d9e08a5..8599a34 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -7,27 +7,36 @@ using Lib; namespace KinectUtils { - static class GestureManager + public static class GestureManager { - static event EventHandler GestureRecognized; - static KinectManager KinectManager { get; set; } - static List KnownGestures { get; set; } + public static event EventHandler GestureRecognized; + public static List KnownGestures = new List(); - static public void AddGestures(IGestureFactory factory) + public static void AddGestures(IGestureFactory factory) { - KnownGestures = (List)factory.CreateGestures(); + var gestures = factory.CreateGestures(); + foreach (var gesture in gestures) + { + AddGesture(gesture); + } } - static public void AddGestures(params BaseGesture[] gestures) + public static void AddGesture(BaseGesture gesture) { - foreach (var gesture in gestures) + if (!KnownGestures.Contains(gesture)) { KnownGestures.Add(gesture); + gesture.GestureRecognized += (sender, e) => GestureRecognized?.Invoke(sender, e); } } - static public void RemoveGesture(BaseGesture gesture) + public static void RemoveGesture(BaseGesture gesture) { - KnownGestures.Remove(gesture); + if (KnownGestures.Contains(gesture)) + { + KnownGestures.Remove(gesture); + gesture.GestureRecognized -= (sender, e) => GestureRecognized?.Invoke(sender, e); + } } + static public void StartAcquiringFrames(KinectManager kinectManager) { throw new NotImplementedException(); diff --git a/Sources/KinectUtils/KinectUtils.csproj b/Sources/KinectUtils/KinectUtils.csproj index c64f282..8343b15 100644 --- a/Sources/KinectUtils/KinectUtils.csproj +++ b/Sources/KinectUtils/KinectUtils.csproj @@ -52,6 +52,10 @@ + + {2496dfb1-eb55-47a1-a780-211e079b289d} + MyGesturesBank + {0751c83e-7845-4e5f-a5d3-e11aba393aca} Lib diff --git a/Sources/LibMyGesturesBank/MyGesturesBank.csproj b/Sources/LibMyGesturesBank/MyGesturesBank.csproj index 7a43ab1..0601293 100644 --- a/Sources/LibMyGesturesBank/MyGesturesBank.csproj +++ b/Sources/LibMyGesturesBank/MyGesturesBank.csproj @@ -48,13 +48,10 @@ + - - {2d44487e-f514-4063-9494-2af1e8c9e9c8} - KinectUtils - {0751c83e-7845-4e5f-a5d3-e11aba393aca} Lib diff --git a/Sources/LibMyGesturesBank/RightHandUp.cs b/Sources/LibMyGesturesBank/RightHandUp.cs index 259d3a5..1c6734a 100644 --- a/Sources/LibMyGesturesBank/RightHandUp.cs +++ b/Sources/LibMyGesturesBank/RightHandUp.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace LibMyGesturesBank +namespace MyGesturesBank { internal class RightHandUp { diff --git a/Sources/LibMyGesturesBank/SwipeRightHand.cs b/Sources/LibMyGesturesBank/SwipeRightHand.cs new file mode 100644 index 0000000..d58ddd0 --- /dev/null +++ b/Sources/LibMyGesturesBank/SwipeRightHand.cs @@ -0,0 +1,38 @@ +using KinectUtils; +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyGesturesBank +{ + public class SwipeRightHand : Gesture + { + protected override bool TestInitialConditions(Body body) + { + // Implémentez la logique pour vérifier les conditions initiales du balayage à droite + return false; + } + + protected override bool TestPosture(Body body) + { + // Implémentez la vérification de la posture pour le balayage à droite + return false; + } + + protected override bool TestRunningGesture(Body body) + { + // Implémentez la dynamique du geste de balayage à droite + return false; + } + + protected override bool TestEndConditions(Body body) + { + // Vérifiez si les conditions de fin du geste de balayage à droite sont remplies + return false; + } + } + +} diff --git a/Sources/LibMyGesturesBank/TwoHandsDragon.cs b/Sources/LibMyGesturesBank/TwoHandsDragon.cs index a5e9d5f..6cafd63 100644 --- a/Sources/LibMyGesturesBank/TwoHandsDragon.cs +++ b/Sources/LibMyGesturesBank/TwoHandsDragon.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace LibMyGesturesBank +namespace MyGesturesBank { internal class TwoHandsDragon { -- 2.36.3 From 0857ab536a5bb68317b315c6b3be232c4a0b8d36 Mon Sep 17 00:00:00 2001 From: "louis.dufour" Date: Wed, 7 Feb 2024 08:37:38 +0100 Subject: [PATCH 03/17] Add(CreateGestures): gesture and posture --- Sources/LibMyGesturesBank/ClapHands.cs | 44 +++++++++++++++++++ .../LibMyGesturesBank/MyGesturesBank.csproj | 5 +++ Sources/LibMyGesturesBank/PostureHandUp.cs | 10 +++-- .../LibMyGesturesBank/PostureHandsOnHead .cs | 24 +++------- Sources/LibMyGesturesBank/RightHandUp.cs | 14 +++++- Sources/LibMyGesturesBank/SwipeRightHand.cs | 36 ++++++++++++--- Sources/LibMyGesturesBank/TwoHandsDragon.cs | 15 ++++++- 7 files changed, 115 insertions(+), 33 deletions(-) create mode 100644 Sources/LibMyGesturesBank/ClapHands.cs diff --git a/Sources/LibMyGesturesBank/ClapHands.cs b/Sources/LibMyGesturesBank/ClapHands.cs new file mode 100644 index 0000000..c1b6e81 --- /dev/null +++ b/Sources/LibMyGesturesBank/ClapHands.cs @@ -0,0 +1,44 @@ +using KinectUtils; +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LibMyGesturesBank +{ + public class ClapHands : Gesture + { + protected override bool TestInitialConditions(Body body) + { + // Les conditions initiales ne sont pas strictes pour un clap, on commence donc directement avec TestPosture et TestRunningGesture + return true; + } + + protected override bool TestPosture(Body body) + { + var handRight = body.Joints[JointType.HandRight].Position; + 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; + } + + protected override bool TestRunningGesture(Body body) + { + var handRight = body.Joints[JointType.HandRight].Position; + var handLeft = body.Joints[JointType.HandLeft].Position; + + // Vérifie si les mains se sont rencontrées au centre + return Math.Abs(handRight.X - handLeft.X) < 0.1f; + } + + protected override bool TestEndConditions(Body body) + { + // Le clap est un geste instantané, donc si les conditions sont remplies une fois, le geste est considéré comme terminé + return true; + } + } + +} diff --git a/Sources/LibMyGesturesBank/MyGesturesBank.csproj b/Sources/LibMyGesturesBank/MyGesturesBank.csproj index 0601293..562b0da 100644 --- a/Sources/LibMyGesturesBank/MyGesturesBank.csproj +++ b/Sources/LibMyGesturesBank/MyGesturesBank.csproj @@ -44,6 +44,7 @@ + @@ -52,6 +53,10 @@ + + {2d44487e-f514-4063-9494-2af1e8c9e9c8} + KinectUtils + {0751c83e-7845-4e5f-a5d3-e11aba393aca} Lib diff --git a/Sources/LibMyGesturesBank/PostureHandUp.cs b/Sources/LibMyGesturesBank/PostureHandUp.cs index 54b1a6e..38f5861 100644 --- a/Sources/LibMyGesturesBank/PostureHandUp.cs +++ b/Sources/LibMyGesturesBank/PostureHandUp.cs @@ -7,11 +7,13 @@ namespace MyGesturesBank { protected override bool TestPosture(Body body) { - // Exemple de condition : la main droite est plus haute que l'épaule droite - return body.Joints[JointType.HandRight].Position.Y > body.Joints[JointType.ShoulderRight].Position.Y; - } + var handRight = body.Joints[JointType.HandRight].Position; + var handLeft = body.Joints[JointType.HandLeft].Position; + var head = body.Joints[JointType.Head].Position; - public override string GestureName => "Hand Up"; + return handRight.Y > head.Y || handLeft.Y > head.Y; + } } + } diff --git a/Sources/LibMyGesturesBank/PostureHandsOnHead .cs b/Sources/LibMyGesturesBank/PostureHandsOnHead .cs index 4b8df45..239eee1 100644 --- a/Sources/LibMyGesturesBank/PostureHandsOnHead .cs +++ b/Sources/LibMyGesturesBank/PostureHandsOnHead .cs @@ -8,27 +8,15 @@ namespace MyGesturesBank { protected override bool TestPosture(Body body) { - // Condition pour la main droite proche de la tête - bool rightHandNearHead = IsHandNearHead(body.Joints[JointType.HandRight], body.Joints[JointType.Head]); + var handRight = body.Joints[JointType.HandRight].Position; + var handLeft = body.Joints[JointType.HandLeft].Position; + var head = body.Joints[JointType.Head].Position; - // Condition pour la main gauche proche de la tête - bool leftHandNearHead = IsHandNearHead(body.Joints[JointType.HandLeft], body.Joints[JointType.Head]); + // Ajustez les seuils selon la précision souhaitée + var threshold = 0.1f; // Seuil pour ajuster la précision de la détection - return rightHandNearHead && leftHandNearHead; + return Math.Abs(handRight.Y - head.Y) < threshold && Math.Abs(handLeft.Y - head.Y) < threshold; } - - private bool IsHandNearHead(Joint hand, Joint head) - { - // Exemple de condition : la main est à moins de 30 cm de la tête - double distance = Math.Sqrt( - Math.Pow(hand.Position.X - head.Position.X, 2) + - Math.Pow(hand.Position.Y - head.Position.Y, 2) + - Math.Pow(hand.Position.Z - head.Position.Z, 2)); - return distance < 0.3 && distance > 0; - } - - public override string GestureName => "Hands On Head"; - } diff --git a/Sources/LibMyGesturesBank/RightHandUp.cs b/Sources/LibMyGesturesBank/RightHandUp.cs index 1c6734a..8de2ef5 100644 --- a/Sources/LibMyGesturesBank/RightHandUp.cs +++ b/Sources/LibMyGesturesBank/RightHandUp.cs @@ -1,4 +1,6 @@ -using System; +using KinectUtils; +using Microsoft.Kinect; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,15 @@ using System.Threading.Tasks; namespace MyGesturesBank { - internal class RightHandUp + public class RightHandUp : Posture { + protected override bool TestPosture(Body body) + { + var handRight = body.Joints[JointType.HandRight].Position; + var head = body.Joints[JointType.Head].Position; + + return handRight.Y > head.Y; + } } + } diff --git a/Sources/LibMyGesturesBank/SwipeRightHand.cs b/Sources/LibMyGesturesBank/SwipeRightHand.cs index d58ddd0..c4c8eeb 100644 --- a/Sources/LibMyGesturesBank/SwipeRightHand.cs +++ b/Sources/LibMyGesturesBank/SwipeRightHand.cs @@ -10,28 +10,50 @@ namespace MyGesturesBank { public class SwipeRightHand : Gesture { + + private float previousX = float.NaN; + protected override bool TestInitialConditions(Body body) { - // Implémentez la logique pour vérifier les conditions initiales du balayage à droite - return false; + var handRight = body.Joints[JointType.HandRight].Position; + var shoulderRight = body.Joints[JointType.ShoulderRight].Position; + + // Conditions initiales : main droite au niveau ou à droite de l'épaule droite, mais pas trop éloignée + return handRight.X >= shoulderRight.X && handRight.X - shoulderRight.X < 0.5f; } protected override bool TestPosture(Body body) { - // Implémentez la vérification de la posture pour le balayage à droite - return false; + var handRight = body.Joints[JointType.HandRight].Position; + var head = body.Joints[JointType.Head].Position; + + // La main droite ne doit pas être plus haute que la tête + return handRight.Y <= head.Y; } protected override bool TestRunningGesture(Body body) { - // Implémentez la dynamique du geste de balayage à droite + var handRight = body.Joints[JointType.HandRight].Position.X; + + if (!float.IsNaN(previousX)) + { + // Vérifie si la main droite se déplace vers la droite + bool isMovingRight = handRight > previousX; + previousX = handRight; + return isMovingRight; + } + + previousX = handRight; return false; } protected override bool TestEndConditions(Body body) { - // Vérifiez si les conditions de fin du geste de balayage à droite sont remplies - return false; + var handRight = body.Joints[JointType.HandRight].Position; + var spineBase = body.Joints[JointType.SpineBase].Position; + + // 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 } } diff --git a/Sources/LibMyGesturesBank/TwoHandsDragon.cs b/Sources/LibMyGesturesBank/TwoHandsDragon.cs index 6cafd63..db3a161 100644 --- a/Sources/LibMyGesturesBank/TwoHandsDragon.cs +++ b/Sources/LibMyGesturesBank/TwoHandsDragon.cs @@ -1,4 +1,6 @@ -using System; +using KinectUtils; +using Microsoft.Kinect; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,16 @@ using System.Threading.Tasks; namespace MyGesturesBank { - internal class TwoHandsDragon + public class TwoHandsDragon : Posture { + protected override bool TestPosture(Body body) + { + var handRight = body.Joints[JointType.HandRight].Position; + var handLeft = body.Joints[JointType.HandLeft].Position; + var head = body.Joints[JointType.Head].Position; + + return handRight.Y > head.Y && handLeft.Y > head.Y; + } } + } -- 2.36.3 From 77f04c9162e3475777fbb6ce191814781b12e74f Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 7 Feb 2024 09:01:35 +0100 Subject: [PATCH 04/17] UPDATE(KinectUtils) --- Sources/KinectUtils/AllGesturesFactory.cs | 5 +++- Sources/KinectUtils/Gesture.cs | 7 +++-- Sources/KinectUtils/GestureManager.cs | 31 ++++++++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Sources/KinectUtils/AllGesturesFactory.cs b/Sources/KinectUtils/AllGesturesFactory.cs index 970478a..7ef77ae 100644 --- a/Sources/KinectUtils/AllGesturesFactory.cs +++ b/Sources/KinectUtils/AllGesturesFactory.cs @@ -10,7 +10,10 @@ namespace KinectUtils { public IEnumerable CreateGestures() { - throw new NotImplementedException(); + return new List + { + new PostureHandsOnHead() + }; } } } diff --git a/Sources/KinectUtils/Gesture.cs b/Sources/KinectUtils/Gesture.cs index f1140b3..42dd8ad 100644 --- a/Sources/KinectUtils/Gesture.cs +++ b/Sources/KinectUtils/Gesture.cs @@ -22,9 +22,12 @@ namespace KinectUtils get { return _maxNbOfFrames; } private set { _maxNbOfFrames = value; } } - public override void TestGesture(Body body) + public bool TestGesture(Body body) { - + TestInitialConditions(body); + TestPosture(body); + TestRunningGesture(body); + TestEndConditions(body); } abstract protected bool TestInitialConditions(Body body); abstract protected bool TestPosture(Body body); diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index 339375f..b1e2cce 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -1,14 +1,18 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; using Lib; +using Microsoft.Kinect; namespace KinectUtils { static class GestureManager { + static private BodyFrameReader bodyFrameReader; + static private Body[] bodies; static event EventHandler GestureRecognized; static KinectManager KinectManager { get; set; } static List KnownGestures { get; set; } @@ -30,11 +34,32 @@ namespace KinectUtils } static public void StartAcquiringFrames(KinectManager kinectManager) { - throw new NotImplementedException(); + bodyFrameReader = kinectManager.Sensor.BodyFrameSource.OpenReader(); + bodyFrameReader.FrameArrived += Reader_BodyFrameArrived; } - static public void StopAcquiringFrames() + + static private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) { - throw new NotImplementedException(); + using (var bodyframe = e.FrameReference.AcquireFrame()) + { + if (bodyframe != null) + { + bodyframe.GetAndRefreshBodyData(bodies); + foreach (var body in bodies) + { + if (body.IsTracked) + { + foreach(var gesture in KnownGestures) + { + if (gesture.TestGesture(body)) + { + + } + } + } + } + } + } } } } -- 2.36.3 From e33c10ed5b33309c12accfb06fe9a60fd731f2e6 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 7 Feb 2024 09:16:09 +0100 Subject: [PATCH 05/17] UPDATE(KinectUtils) --- Sources/KinectUtils/GestureManager.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index b1e2cce..8d703c9 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -25,7 +25,10 @@ namespace KinectUtils { foreach (var gesture in gestures) { - KnownGestures.Add(gesture); + if (!gestures.Contains(gesture)) + { + KnownGestures.Add(gesture); + } } } static public void RemoveGesture(BaseGesture gesture) @@ -51,10 +54,7 @@ namespace KinectUtils { foreach(var gesture in KnownGestures) { - if (gesture.TestGesture(body)) - { - - } + gesture.TestGesture(body); } } } -- 2.36.3 From 6a1611d5113842a71b09b78f350d54814ca2ce52 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 7 Feb 2024 09:37:40 +0100 Subject: [PATCH 06/17] UPDATE(KinectUtils): ADD several changed to make the gestureManager work but still some development to do to test --- Sources/KinectUtils/AllGesturesFactory.cs | 8 +++---- Sources/KinectUtils/BaseGesture.cs | 2 +- Sources/KinectUtils/Gesture.cs | 2 +- Sources/KinectUtils/IGestureFactory.cs | 2 +- Sources/KinectUtils/KinectUtils.csproj | 4 ---- Sources/KinectUtils/Posture.cs | 2 +- .../LibMyGesturesBank/AllGesturesFactory.cs | 22 +++++++++++++++++++ 7 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 Sources/LibMyGesturesBank/AllGesturesFactory.cs diff --git a/Sources/KinectUtils/AllGesturesFactory.cs b/Sources/KinectUtils/AllGesturesFactory.cs index 719a15e..7305108 100644 --- a/Sources/KinectUtils/AllGesturesFactory.cs +++ b/Sources/KinectUtils/AllGesturesFactory.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using MyGesturesBank; +using KinectUtils; -namespace KinectUtils +namespace MyGesturesBank { public class AllGesturesFactory : IGestureFactory { @@ -13,8 +13,8 @@ namespace KinectUtils { return new List { - new SwipeRightHand(), - new ClapHands() + //new SwipeRightHand(), + //new ClapHands() // Ajoutez d'autres gestes ici }; } diff --git a/Sources/KinectUtils/BaseGesture.cs b/Sources/KinectUtils/BaseGesture.cs index c414db6..5b69899 100644 --- a/Sources/KinectUtils/BaseGesture.cs +++ b/Sources/KinectUtils/BaseGesture.cs @@ -9,7 +9,7 @@ namespace KinectUtils public event EventHandler GestureRecognized; // Nom du geste - marqué comme virtual pour permettre la substitution - public string GestureName { get; protected set; } + public virtual string GestureName { get; protected set; } // Méthode abstraite pour tester le geste public abstract void TestGesture(Body body); diff --git a/Sources/KinectUtils/Gesture.cs b/Sources/KinectUtils/Gesture.cs index b05249e..d6dc9d1 100644 --- a/Sources/KinectUtils/Gesture.cs +++ b/Sources/KinectUtils/Gesture.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace KinectUtils { - abstract class Gesture : BaseGesture + public abstract class Gesture : BaseGesture { public bool IsRecognitionRunning { get; set; } diff --git a/Sources/KinectUtils/IGestureFactory.cs b/Sources/KinectUtils/IGestureFactory.cs index f78c453..b01155f 100644 --- a/Sources/KinectUtils/IGestureFactory.cs +++ b/Sources/KinectUtils/IGestureFactory.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace KinectUtils { - interface IGestureFactory + public interface IGestureFactory { IEnumerable CreateGestures(); } diff --git a/Sources/KinectUtils/KinectUtils.csproj b/Sources/KinectUtils/KinectUtils.csproj index 8343b15..c64f282 100644 --- a/Sources/KinectUtils/KinectUtils.csproj +++ b/Sources/KinectUtils/KinectUtils.csproj @@ -52,10 +52,6 @@ - - {2496dfb1-eb55-47a1-a780-211e079b289d} - MyGesturesBank - {0751c83e-7845-4e5f-a5d3-e11aba393aca} Lib diff --git a/Sources/KinectUtils/Posture.cs b/Sources/KinectUtils/Posture.cs index a95b2c6..d64b672 100644 --- a/Sources/KinectUtils/Posture.cs +++ b/Sources/KinectUtils/Posture.cs @@ -16,7 +16,7 @@ namespace KinectUtils if (TestPosture(body)) { // Posture is recognized - OnGestureRecognized(); + OnGestureRecognized(body); } } diff --git a/Sources/LibMyGesturesBank/AllGesturesFactory.cs b/Sources/LibMyGesturesBank/AllGesturesFactory.cs new file mode 100644 index 0000000..8b62daa --- /dev/null +++ b/Sources/LibMyGesturesBank/AllGesturesFactory.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using KinectUtils; + +namespace MyGesturesBank +{ + public class AllGesturesFactory : IGestureFactory + { + public IEnumerable CreateGestures() + { + return new List + { + new SwipeRightHand(), + //new ClapHands() + // Ajoutez d'autres gestes ici + }; + } + } +} -- 2.36.3 From 212f603f6feae71779419c8b5a1b8dbe2a341655 Mon Sep 17 00:00:00 2001 From: "louis.dufour" Date: Wed, 7 Feb 2024 08:57:46 +0100 Subject: [PATCH 07/17] Add(dev): GestureName --- Sources/LibMyGesturesBank/TwoHandsDragon.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/LibMyGesturesBank/TwoHandsDragon.cs b/Sources/LibMyGesturesBank/TwoHandsDragon.cs index db3a161..c047067 100644 --- a/Sources/LibMyGesturesBank/TwoHandsDragon.cs +++ b/Sources/LibMyGesturesBank/TwoHandsDragon.cs @@ -18,6 +18,8 @@ namespace MyGesturesBank return handRight.Y > head.Y && handLeft.Y > head.Y; } + + public override string GestureName => "Hands on Head"; } } -- 2.36.3 From 1ac8d06ad598fd876118e9d90735d3d9c88e0080 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 7 Feb 2024 09:53:06 +0100 Subject: [PATCH 08/17] UPDATE(dev): add test in program --- Sources/ConsoleApp/Program.cs | 67 ++----------------- Sources/KinectUtils/AllGesturesFactory.cs | 22 ------ Sources/KinectUtils/GestureManager.cs | 1 + Sources/KinectUtils/KinectUtils.csproj | 1 - .../LibMyGesturesBank/AllGesturesFactory.cs | 6 +- .../LibMyGesturesBank/MyGesturesBank.csproj | 1 + 6 files changed, 12 insertions(+), 86 deletions(-) delete mode 100644 Sources/KinectUtils/AllGesturesFactory.cs diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index b8f5ea7..55f5346 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -1,4 +1,5 @@ -using Lib; +using KinectUtils; +using Lib; using Microsoft.Kinect; using MyGesturesBank; using System; @@ -16,68 +17,10 @@ namespace ConsoleApp { static void Main(string[] args) { + AllGesturesFactory allGesturesFactory = new AllGesturesFactory(); + GestureManager.AddGestures(allGesturesFactory); KinectManager kinectManager = new KinectManager(); - Canvas skeletonCanvas = null; - KinectStreamsFactory Factory = new KinectStreamsFactory(kinectManager, skeletonCanvas); - BodyImageStream CurrentKinectStream = (BodyImageStream)Factory[KinectStreams.Body]; - CurrentKinectStream.Start(); - PostureHandUp handUpPosture = new PostureHandUp(); - PostureHandsOnHead handsOnHeadPosture = new PostureHandsOnHead(); - handUpPosture.GestureRecognized += (sender, e) => - { - Console.WriteLine("Posture Hand Up reconnue !"); - }; - handsOnHeadPosture.GestureRecognized += (sender, e) => - { - Console.WriteLine("Posture Hands On Head reconnue !"); - }; - Body body = null; - while (true) - { - body = CurrentKinectStream.Bodies.FirstOrDefault(); - if (body != null) - { - handUpPosture.TestGesture(body); - handsOnHeadPosture.TestGesture(body); - } - Thread.Sleep(50); - } - // 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(); - - // // 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 !"); - // }; - - // // Boucle pour tester les postures - // while (true) - // { - // Body body = kinecManager.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 - // } - //} - + GestureManager.StartAcquiringFrames(kinectManager); } } } diff --git a/Sources/KinectUtils/AllGesturesFactory.cs b/Sources/KinectUtils/AllGesturesFactory.cs deleted file mode 100644 index 7305108..0000000 --- a/Sources/KinectUtils/AllGesturesFactory.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using KinectUtils; - -namespace MyGesturesBank -{ - public class AllGesturesFactory : IGestureFactory - { - public IEnumerable CreateGestures() - { - return new List - { - //new SwipeRightHand(), - //new ClapHands() - // Ajoutez d'autres gestes ici - }; - } - } -} diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index cece5c1..0708df7 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; diff --git a/Sources/KinectUtils/KinectUtils.csproj b/Sources/KinectUtils/KinectUtils.csproj index c64f282..fa4c521 100644 --- a/Sources/KinectUtils/KinectUtils.csproj +++ b/Sources/KinectUtils/KinectUtils.csproj @@ -42,7 +42,6 @@ - diff --git a/Sources/LibMyGesturesBank/AllGesturesFactory.cs b/Sources/LibMyGesturesBank/AllGesturesFactory.cs index 8b62daa..0df7c54 100644 --- a/Sources/LibMyGesturesBank/AllGesturesFactory.cs +++ b/Sources/LibMyGesturesBank/AllGesturesFactory.cs @@ -14,7 +14,11 @@ namespace MyGesturesBank return new List { new SwipeRightHand(), - //new ClapHands() + new PostureHandsOnHead(), + new PostureHandUp(), + new RightHandUp(), + new SwipeRightHand(), + new TwoHandsDragon(), // Ajoutez d'autres gestes ici }; } diff --git a/Sources/LibMyGesturesBank/MyGesturesBank.csproj b/Sources/LibMyGesturesBank/MyGesturesBank.csproj index 562b0da..1cd793f 100644 --- a/Sources/LibMyGesturesBank/MyGesturesBank.csproj +++ b/Sources/LibMyGesturesBank/MyGesturesBank.csproj @@ -44,6 +44,7 @@ + -- 2.36.3 From 854c6edc20b5b04ace69d1fa6ce244ecdc561eea Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 7 Feb 2024 09:54:49 +0100 Subject: [PATCH 09/17] add gestureName to display --- Sources/KinectUtils/Gesture.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/KinectUtils/Gesture.cs b/Sources/KinectUtils/Gesture.cs index d6dc9d1..9c850d8 100644 --- a/Sources/KinectUtils/Gesture.cs +++ b/Sources/KinectUtils/Gesture.cs @@ -22,6 +22,7 @@ namespace KinectUtils if (TestInitialConditions(body)) { IsRecognitionRunning = true; + Console.WriteLine(GestureName); currentFrameCount = 0; } } -- 2.36.3 From 42c338e18c4d4a83be653c7d62d147bfa04255f9 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 14 Feb 2024 11:00:40 +0100 Subject: [PATCH 10/17] UPDATE(dev): recognition works --- Sources/ConsoleApp/Program.cs | 10 ++++++- Sources/KinectUtils/GestureManager.cs | 26 +++++++++++-------- Sources/LibMyGesturesBank/ClapHands.cs | 1 + Sources/LibMyGesturesBank/PostureHandUp.cs | 1 + .../LibMyGesturesBank/PostureHandsOnHead .cs | 1 + Sources/LibMyGesturesBank/RightHandUp.cs | 1 + Sources/LibMyGesturesBank/SwipeRightHand.cs | 1 + Sources/LibMyGesturesBank/TwoHandsDragon.cs | 1 + 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index 55f5346..ccf4e43 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -17,10 +17,18 @@ namespace ConsoleApp { static void Main(string[] args) { + // allumer la kinect + KinectManager kinectManager = new KinectManager(); + kinectManager.StartSensor(); + Console.WriteLine("Kinect démarre"); + AllGesturesFactory allGesturesFactory = new AllGesturesFactory(); GestureManager.AddGestures(allGesturesFactory); - KinectManager kinectManager = new KinectManager(); GestureManager.StartAcquiringFrames(kinectManager); + while (true) + { + //Thread.Sleep(50); + } } } } diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index 0708df7..1831e27 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -20,11 +20,7 @@ namespace KinectUtils public static void AddGestures(IGestureFactory factory) { - var gestures = factory.CreateGestures(); - foreach (var gesture in gestures) - { - AddGesture(gesture); - } + KnownGestures = (List)factory.CreateGestures(); } public static void AddGesture(BaseGesture gesture) { @@ -55,14 +51,23 @@ namespace KinectUtils { if (bodyframe != null) { - bodyframe.GetAndRefreshBodyData(bodies); - foreach (var body in bodies) + + if(bodies == null) { - if (body.IsTracked) + bodies = new Body[bodyframe.BodyCount]; + } + bodyframe.GetAndRefreshBodyData(bodies); + if (bodies != null) { - foreach(var gesture in KnownGestures) + foreach (var body in bodies) { - gesture.TestGesture(body); + if (body.IsTracked) + { + foreach (var gesture in KnownGestures) + { + gesture.TestGesture(body); + } + } } } } @@ -70,4 +75,3 @@ namespace KinectUtils } } } -} diff --git a/Sources/LibMyGesturesBank/ClapHands.cs b/Sources/LibMyGesturesBank/ClapHands.cs index c1b6e81..dc01856 100644 --- a/Sources/LibMyGesturesBank/ClapHands.cs +++ b/Sources/LibMyGesturesBank/ClapHands.cs @@ -39,6 +39,7 @@ namespace LibMyGesturesBank // 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 38f5861..661249b 100644 --- a/Sources/LibMyGesturesBank/PostureHandUp.cs +++ b/Sources/LibMyGesturesBank/PostureHandUp.cs @@ -13,6 +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 239eee1..a1835f2 100644 --- a/Sources/LibMyGesturesBank/PostureHandsOnHead .cs +++ b/Sources/LibMyGesturesBank/PostureHandsOnHead .cs @@ -17,6 +17,7 @@ namespace MyGesturesBank 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 8de2ef5..3d8c90b 100644 --- a/Sources/LibMyGesturesBank/RightHandUp.cs +++ b/Sources/LibMyGesturesBank/RightHandUp.cs @@ -17,6 +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 c4c8eeb..44291b0 100644 --- a/Sources/LibMyGesturesBank/SwipeRightHand.cs +++ b/Sources/LibMyGesturesBank/SwipeRightHand.cs @@ -55,6 +55,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 db3a161..6b5400a 100644 --- a/Sources/LibMyGesturesBank/TwoHandsDragon.cs +++ b/Sources/LibMyGesturesBank/TwoHandsDragon.cs @@ -18,6 +18,7 @@ namespace MyGesturesBank return handRight.Y > head.Y && handLeft.Y > head.Y; } + public override string GestureName => "Two Hands Dragon"; } } -- 2.36.3 From 5df113740e7bbe2ac7d10d07cf711fc6cfee5bc1 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 14 Feb 2024 11:30:52 +0100 Subject: [PATCH 11/17] Update(dev): posture works --- Sources/KinectUtils/GestureManager.cs | 8 +++++-- Sources/KinectUtils/Posture.cs | 23 +++++++++++++++---- .../LibMyGesturesBank/AllGesturesFactory.cs | 3 +-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index 1831e27..ddce0a3 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -16,11 +16,15 @@ namespace KinectUtils static private Body[] bodies; static event EventHandler GestureRecognized; static KinectManager KinectManager { get; set; } - static List KnownGestures { get; set; } + static List KnownGestures { get; set; } = new List(); public static void AddGestures(IGestureFactory factory) { - KnownGestures = (List)factory.CreateGestures(); + var gestures = (List)factory.CreateGestures(); + foreach (var gesture in gestures) + { + AddGesture(gesture); + } } public static void AddGesture(BaseGesture gesture) { diff --git a/Sources/KinectUtils/Posture.cs b/Sources/KinectUtils/Posture.cs index d64b672..34c6a30 100644 --- a/Sources/KinectUtils/Posture.cs +++ b/Sources/KinectUtils/Posture.cs @@ -9,16 +9,31 @@ namespace KinectUtils { public abstract class Posture : BaseGesture { + // Déclaration des événements + public event Action PostureRecognized; + public event Action PostureUnrecognized; + + private bool wasRecognized = false; // Pour garder l'état de reconnaissance précédent + protected abstract bool TestPosture(Body body); public override void TestGesture(Body body) { - if (TestPosture(body)) + bool isRecognized = TestPosture(body); // Teste la posture actuelle + + if (isRecognized && !wasRecognized) { - // Posture is recognized - OnGestureRecognized(body); + // La posture est reconnue et ne l'était pas au frame précédent + PostureRecognized?.Invoke(body); + Console.WriteLine(GestureName); + } + else if (!isRecognized && wasRecognized) + { + // La posture n'est plus reconnue alors qu'elle l'était au frame précédent + PostureUnrecognized?.Invoke(body); } - } + wasRecognized = isRecognized; // Mise à jour de l'état de reconnaissance pour le prochain frame + } } } diff --git a/Sources/LibMyGesturesBank/AllGesturesFactory.cs b/Sources/LibMyGesturesBank/AllGesturesFactory.cs index 0df7c54..5802cfe 100644 --- a/Sources/LibMyGesturesBank/AllGesturesFactory.cs +++ b/Sources/LibMyGesturesBank/AllGesturesFactory.cs @@ -13,11 +13,10 @@ namespace MyGesturesBank { return new List { - new SwipeRightHand(), + //new SwipeRightHand(), new PostureHandsOnHead(), new PostureHandUp(), new RightHandUp(), - new SwipeRightHand(), new TwoHandsDragon(), // Ajoutez d'autres gestes ici }; -- 2.36.3 From 7a0e038241f45731bfe0859798991f68402acbb2 Mon Sep 17 00:00:00 2001 From: "johan.lachenal" Date: Wed, 14 Feb 2024 11:57:50 +0100 Subject: [PATCH 12/17] 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 13/17] 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 From e808ac0b2a41a2586bcfb32cb30599f10624a34c Mon Sep 17 00:00:00 2001 From: Louis DUFOUR Date: Sat, 17 Feb 2024 20:42:05 +0100 Subject: [PATCH 14/17] Update(dev): pas de TwoHandsDragon ici --- .../LibMyGesturesBank/MyGesturesBank.csproj | 1 - Sources/LibMyGesturesBank/TwoHandsDragon.cs | 24 ------------------- 2 files changed, 25 deletions(-) delete mode 100644 Sources/LibMyGesturesBank/TwoHandsDragon.cs diff --git a/Sources/LibMyGesturesBank/MyGesturesBank.csproj b/Sources/LibMyGesturesBank/MyGesturesBank.csproj index 1cd793f..3113c9f 100644 --- a/Sources/LibMyGesturesBank/MyGesturesBank.csproj +++ b/Sources/LibMyGesturesBank/MyGesturesBank.csproj @@ -51,7 +51,6 @@ - diff --git a/Sources/LibMyGesturesBank/TwoHandsDragon.cs b/Sources/LibMyGesturesBank/TwoHandsDragon.cs deleted file mode 100644 index 6b5400a..0000000 --- a/Sources/LibMyGesturesBank/TwoHandsDragon.cs +++ /dev/null @@ -1,24 +0,0 @@ -using KinectUtils; -using Microsoft.Kinect; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MyGesturesBank -{ - public class TwoHandsDragon : Posture - { - protected override bool TestPosture(Body body) - { - var handRight = body.Joints[JointType.HandRight].Position; - var handLeft = body.Joints[JointType.HandLeft].Position; - var head = body.Joints[JointType.Head].Position; - - return handRight.Y > head.Y && handLeft.Y > head.Y; - } - public override string GestureName => "Two Hands Dragon"; - } - -} -- 2.36.3 From eb52f7c7863367cb27fc655cf0af7d8de34a257b Mon Sep 17 00:00:00 2001 From: Louis DUFOUR Date: Sat, 17 Feb 2024 21:24:24 +0100 Subject: [PATCH 15/17] =?UTF-8?q?Fix(dev):=20ClapHands=20=C3=A7a=20marche?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/ConsoleApp/Program.cs | 2 +- .../LibMyGesturesBank/AllGesturesFactory.cs | 7 ++- Sources/LibMyGesturesBank/ClapHands.cs | 43 +++++++++++++------ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Sources/ConsoleApp/Program.cs b/Sources/ConsoleApp/Program.cs index ccf4e43..c7fe846 100644 --- a/Sources/ConsoleApp/Program.cs +++ b/Sources/ConsoleApp/Program.cs @@ -27,7 +27,7 @@ namespace ConsoleApp GestureManager.StartAcquiringFrames(kinectManager); while (true) { - //Thread.Sleep(50); + Thread.Sleep(50); } } } diff --git a/Sources/LibMyGesturesBank/AllGesturesFactory.cs b/Sources/LibMyGesturesBank/AllGesturesFactory.cs index dee370c..9726123 100644 --- a/Sources/LibMyGesturesBank/AllGesturesFactory.cs +++ b/Sources/LibMyGesturesBank/AllGesturesFactory.cs @@ -14,10 +14,9 @@ namespace MyGesturesBank return new List { //new SwipeRightHand(), - new PostureHandsOnHead(), - new PostureHandUp(), - new RightHandUp(), - new TwoHandsDragon(), + //new PostureHandsOnHead(), + //new PostureHandUp(), + //new RightHandUp(), new ClapHands(), // Ajoutez d'autres gestes ici }; diff --git a/Sources/LibMyGesturesBank/ClapHands.cs b/Sources/LibMyGesturesBank/ClapHands.cs index 5df0fda..243d7fe 100644 --- a/Sources/LibMyGesturesBank/ClapHands.cs +++ b/Sources/LibMyGesturesBank/ClapHands.cs @@ -11,9 +11,13 @@ namespace MyGesturesBank { public class ClapHands : Gesture { + private CameraSpacePoint previousRightHandPosition; + private CameraSpacePoint previousLeftHandPosition; + private bool isClapInitiated = false; + protected override bool TestInitialConditions(Body body) { - // Les conditions initiales ne sont pas strictes pour un clap, on commence donc directement avec TestPosture et TestRunningGesture + // Initial conditions are not strict for a clap, start directly with TestPosture and TestRunningGesture return true; } @@ -22,25 +26,40 @@ namespace MyGesturesBank var handRight = body.Joints[JointType.HandRight].Position; 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; + if (!isClapInitiated) + { + previousRightHandPosition = handRight; + previousLeftHandPosition = handLeft; + isClapInitiated = true; + return false; // Wait for the next frame to start evaluation + } + + // Check if hands moved closer since the last frame + bool handsMovingCloser = + (handRight.X - previousRightHandPosition.X) < 0 && + (handLeft.X - previousLeftHandPosition.X) > 0; + + // Update positions for next comparison + previousRightHandPosition = handRight; + previousLeftHandPosition = handLeft; + + // Consider hand movement towards each other as part of the posture check + return handsMovingCloser && Math.Abs(handRight.Y - handLeft.Y) < 0.2f; } protected override bool TestRunningGesture(Body body) { - var handRight = body.Joints[JointType.HandRight].Position; - var handLeft = body.Joints[JointType.HandLeft].Position; - - // Vérifie si les mains se sont rencontrées au centre - return Math.Abs(handRight.X - handLeft.X) < 0.1f; + // This method could be used to check for the continuation of the gesture, if necessary + return true; } protected override bool TestEndConditions(Body body) { - // Le clap est un geste instantané, donc si les conditions sont remplies une fois, le geste est considéré comme terminé - return true; + var handRight = body.Joints[JointType.HandRight].Position; + var handLeft = body.Joints[JointType.HandLeft].Position; + + // End condition based on proximity of hands + return Math.Abs(handRight.X - handLeft.X) < 0.1f; } public override string GestureName => "Clap Hands"; -- 2.36.3 From 76ea8f36cc12b1a4a6286d77b6e6b323001093b9 Mon Sep 17 00:00:00 2001 From: Louis DUFOUR Date: Sat, 17 Feb 2024 22:45:31 +0100 Subject: [PATCH 16/17] =?UTF-8?q?Update(dev):=20SwipeRightHand=20=C3=A0=20?= =?UTF-8?q?la=20fa=C3=A7ont=20T=20pose?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LibMyGesturesBank/AllGesturesFactory.cs | 8 +++---- Sources/LibMyGesturesBank/SwipeRightHand.cs | 23 +++++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Sources/LibMyGesturesBank/AllGesturesFactory.cs b/Sources/LibMyGesturesBank/AllGesturesFactory.cs index 9726123..d3ddf6d 100644 --- a/Sources/LibMyGesturesBank/AllGesturesFactory.cs +++ b/Sources/LibMyGesturesBank/AllGesturesFactory.cs @@ -13,10 +13,10 @@ namespace MyGesturesBank { return new List { - //new SwipeRightHand(), - //new PostureHandsOnHead(), - //new PostureHandUp(), - //new RightHandUp(), + new SwipeRightHand(), + new PostureHandsOnHead(), + new PostureHandUp(), + new RightHandUp(), new ClapHands(), // Ajoutez d'autres gestes ici }; diff --git a/Sources/LibMyGesturesBank/SwipeRightHand.cs b/Sources/LibMyGesturesBank/SwipeRightHand.cs index 0b80634..ab50dcb 100644 --- a/Sources/LibMyGesturesBank/SwipeRightHand.cs +++ b/Sources/LibMyGesturesBank/SwipeRightHand.cs @@ -11,6 +11,7 @@ namespace MyGesturesBank public class SwipeRightHand : Gesture { private float previousX = float.NaN; + private bool gestureStarted = false; protected override bool TestInitialConditions(Body body) { @@ -18,7 +19,17 @@ namespace MyGesturesBank var shoulderRight = body.Joints[JointType.ShoulderRight].Position; // Conditions initiales : main droite au niveau ou à droite de l'épaule droite, mais pas trop éloignée - return handRight.X >= shoulderRight.X && handRight.X - shoulderRight.X < 0.5f; + bool initialCondition = handRight.X >= shoulderRight.X && handRight.X - shoulderRight.X < 0.4f; + + if (initialCondition && !gestureStarted) + { + // Si les conditions initiales sont remplies et que le geste n'a pas encore commencé, + // initialiser previousX et marquer le geste comme commencé + previousX = handRight.X; + gestureStarted = true; + return false; // Attendre le prochain frame pour commencer l'évaluation + } + return initialCondition; } protected override bool TestPosture(Body body) @@ -32,6 +43,8 @@ namespace MyGesturesBank protected override bool TestRunningGesture(Body body) { + if (!gestureStarted) return false; // Assurer que le geste a commencé correctement + var handRight = body.Joints[JointType.HandRight].Position.X; if (!float.IsNaN(previousX)) @@ -52,7 +65,13 @@ namespace MyGesturesBank var spineBase = body.Joints[JointType.SpineBase].Position; // 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 + if (handRight.X > spineBase.X + 0.8f) // Ajustez cette valeur selon le besoin + { + gestureStarted = false; // Réinitialiser l'état du geste + previousX = float.NaN; // Préparer pour la prochaine détection + return true; + } + return false; } public override string GestureName => "Swipe Right Hand"; -- 2.36.3 From 14a4beb21dd3f79e55c6ce7804f948bbe35df372 Mon Sep 17 00:00:00 2001 From: Louis DUFOUR Date: Sun, 18 Feb 2024 11:49:43 +0100 Subject: [PATCH 17/17] =?UTF-8?q?Add(dev):=20le=20mapping=20impl=C3=A9ment?= =?UTF-8?q?er,=20mais=20pas=20test=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/KinectUtils/BaseMapping.cs | 71 ++++++++++++++++--- Sources/KinectUtils/GestureManager.cs | 24 +++++-- .../LibMyGesturesBank/MyGesturesBank.csproj | 1 + 3 files changed, 78 insertions(+), 18 deletions(-) diff --git a/Sources/KinectUtils/BaseMapping.cs b/Sources/KinectUtils/BaseMapping.cs index a37b1a4..c335df1 100644 --- a/Sources/KinectUtils/BaseMapping.cs +++ b/Sources/KinectUtils/BaseMapping.cs @@ -7,31 +7,80 @@ using System.Threading.Tasks; namespace KinectUtils { - class BaseMapping + public abstract class BaseMapping { - public void SubscribeToStartGesture(BaseGesture gesture) + protected bool running = false; + public event EventHandler> OnMapping; + + protected BaseMapping(BaseGesture startGesture, BaseGesture endGesture) + { + SubscribeToStartGesture(startGesture); + SubscribeToEndGesture(endGesture); + // Assuming GestureManager has an event that provides processed body data + GestureManager.ProcessedBodyFrameArrived += OnProcessedBodyFrameArrived; + } + + protected abstract T Mapping(Body body); + + protected bool TestMapping(Body body, out T output) + { + output = default(T); + if (running) + { + output = Mapping(body); + return true; + } + return false; + } + + protected void SubscribeToStartGesture(BaseGesture gesture) { - throw new NotImplementedException(); + gesture.GestureRecognized += (sender, e) => { if (!running) running = true; }; } - public void SubscribeToEndGesture(BaseGesture gesture) + protected void SubscribeToEndGesture(BaseGesture gesture) { - throw new NotImplementedException(); + gesture.GestureRecognized += (sender, e) => { if (running) running = false; }; } - public void SubscribeToToggleGesture(BaseGesture gesture) + // Assuming your GestureManager provides a similar event + protected void OnProcessedBodyFrameArrived(object sender, ProcessedBodyFrameEventArgs e) { - throw new NotImplementedException(); + foreach (var body in e.Bodies) + { + if (body.IsTracked) + { + T output; + if (TestMapping(body, out output)) + { + OnMapping?.Invoke(this, new OnMappingEventArgs(output)); + } + } + } } + } + + public class OnMappingEventArgs : EventArgs + { + public T MappedValue { get; private set; } - protected T Mapping(Body body) + public OnMappingEventArgs(T mappedValue) { - throw new NotImplementedException(); + MappedValue = mappedValue; } + } + + // Custom EventArgs to be used within your application logic + // This is not from the Kinect SDK, but rather a custom class to pass along processed body data + public class ProcessedBodyFrameEventArgs : EventArgs + { + public Body[] Bodies { get; private set; } + public Body ClosestBody { get; private set; } - public bool TestMapping(Body body, out T output) + public ProcessedBodyFrameEventArgs(Body[] bodies, Body closestBody) { - throw new NotImplementedException(); + Bodies = bodies; + ClosestBody = closestBody; } } } diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index d4761cf..1541fb3 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -14,7 +14,12 @@ namespace KinectUtils { private static BodyFrameReader bodyFrameReader; private static Body[] bodies; + static event EventHandler GestureRecognized; + public static event EventHandler BodyFrameArrived; + public static event EventHandler ProcessedBodyFrameArrived; + + static KinectManager KinectManager { get; set; } static List KnownGestures { get; set; } = new List(); @@ -62,19 +67,24 @@ namespace KinectUtils bodies = new Body[bodyframe.BodyCount]; } bodyframe.GetAndRefreshBodyData(bodies); - if (bodies != null) + + Body closestBody = bodies.Where(b => b.IsTracked) + .OrderBy(b => b.Joints[JointType.SpineBase].Position.Z) + .FirstOrDefault(); + + ProcessedBodyFrameArrived?.Invoke(sender, new ProcessedBodyFrameEventArgs(bodies, closestBody)); + + 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); } } } + } } } diff --git a/Sources/LibMyGesturesBank/MyGesturesBank.csproj b/Sources/LibMyGesturesBank/MyGesturesBank.csproj index 3113c9f..3cea259 100644 --- a/Sources/LibMyGesturesBank/MyGesturesBank.csproj +++ b/Sources/LibMyGesturesBank/MyGesturesBank.csproj @@ -42,6 +42,7 @@ + -- 2.36.3