From ec2bff1e45627f5fc08bf2ecd8b6c4de701297a6 Mon Sep 17 00:00:00 2001 From: lobroda Date: Fri, 9 Feb 2024 13:30:30 +0100 Subject: [PATCH] more gestures --- KinectUtils/GestureManager.cs | 26 +++-- MyGestureBank/MyGestureBank.csproj | 4 + MyGestureBank/PostureHandDownLeft.cs | 49 ++++++++++ MyGestureBank/PostureHandDownRight.cs | 49 ++++++++++ MyGestureBank/PostureHandUpLeft.cs | 7 +- MyGestureBank/PostureHandUpRight.cs | 6 +- MyGestureBank/PostureTwoHandsDown.cs | 51 ++++++++++ MyGestureBank/PostureTwoHandsUp.cs | 50 ++++++++++ PostureTester/PostureTester.csproj | 4 + PostureTester/Program.cs | 135 +++++++++++++++----------- 10 files changed, 318 insertions(+), 63 deletions(-) create mode 100644 MyGestureBank/PostureHandDownLeft.cs create mode 100644 MyGestureBank/PostureHandDownRight.cs create mode 100644 MyGestureBank/PostureTwoHandsDown.cs create mode 100644 MyGestureBank/PostureTwoHandsUp.cs diff --git a/KinectUtils/GestureManager.cs b/KinectUtils/GestureManager.cs index f7760df..3d799a3 100644 --- a/KinectUtils/GestureManager.cs +++ b/KinectUtils/GestureManager.cs @@ -3,15 +3,17 @@ using Microsoft.Kinect; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace KinectUtils { - public class GestureManager + public static class GestureManager { - private static bool isAcquiringFrame; + private static bool isAcquiringFrame = false; // false by default private static BodyFrameReader bodyFrameReader; // Properties @@ -32,7 +34,7 @@ namespace KinectUtils public static void AddGestures(BaseGesture[] baseGestures) // params ??? { - throw new NotImplementedException(); + KnownGestures.AddRange(baseGestures); } public static void RemoveGesture(BaseGesture baseGesture) @@ -45,8 +47,8 @@ namespace KinectUtils { if (!isAcquiringFrame) { - KinectManager.KinectSensor.Open(); - bodyFrameReader = KinectManager.KinectSensor.BodyFrameSource.OpenReader(); + manager.KinectSensor.Open(); + bodyFrameReader = manager.KinectSensor.BodyFrameSource.OpenReader(); bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived; isAcquiringFrame = true; } @@ -56,7 +58,7 @@ namespace KinectUtils { if (isAcquiringFrame) { - KinectManager.KinectSensor.Close(); + manager.KinectSensor.Close(); bodyFrameReader.FrameArrived -= BodyFrameReader_FrameArrived; isAcquiringFrame = false; } @@ -75,7 +77,17 @@ namespace KinectUtils { if (body.IsTracked) { - //postureHandUpRight.TestGesture(body); + foreach (var gesture in KnownGestures) + { + if (gesture != null) + { + gesture.TestGesture(body); + } + else + { + Console.WriteLine("gesture null"); + } + } } } } diff --git a/MyGestureBank/MyGestureBank.csproj b/MyGestureBank/MyGestureBank.csproj index 77bc008..2ae77cf 100644 --- a/MyGestureBank/MyGestureBank.csproj +++ b/MyGestureBank/MyGestureBank.csproj @@ -44,9 +44,13 @@ + + + + diff --git a/MyGestureBank/PostureHandDownLeft.cs b/MyGestureBank/PostureHandDownLeft.cs new file mode 100644 index 0000000..03f1d89 --- /dev/null +++ b/MyGestureBank/PostureHandDownLeft.cs @@ -0,0 +1,49 @@ +using KinectUtils; +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MyGestureBank +{ + /// + /// The right hand down posture. + /// + public class PostureHandDownLeft : Posture + { + public PostureHandDownLeft() + { + GestureName = "Hand Down Left"; + } + + /// + /// The test gesture method. + /// + /// The body + public override void TestGesture(Body body) + { + if (TestPosture(body)) + { + Console.WriteLine("Gesture recognized, hand down left"); + Thread.Sleep(1000); + + OnGestureRecognized(); + } + } + + /// + /// The test posture method. + /// + /// + /// + /// + protected override bool TestPosture(Body body) + { + // Check if the left hand is below the left hip + return body.Joints[JointType.HandLeft].Position.Y < body.Joints[JointType.HipLeft].Position.Y; + } + } +} diff --git a/MyGestureBank/PostureHandDownRight.cs b/MyGestureBank/PostureHandDownRight.cs new file mode 100644 index 0000000..6f49955 --- /dev/null +++ b/MyGestureBank/PostureHandDownRight.cs @@ -0,0 +1,49 @@ +using KinectUtils; +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MyGestureBank +{ + /// + /// The right hand down posture. + /// + public class PostureHandDownRight : Posture + { + public PostureHandDownRight() + { + GestureName = "Hand Down Right"; + } + + /// + /// The test gesture method. + /// + /// The body + public override void TestGesture(Body body) + { + if (TestPosture(body)) + { + Console.WriteLine("Gesture recognized, hand down right"); + Thread.Sleep(1000); + + OnGestureRecognized(); + } + } + + /// + /// The test posture method. + /// + /// + /// + /// + protected override bool TestPosture(Body body) + { + // Check if right hand is below the right hip + return body.Joints[JointType.HandRight].Position.Y < body.Joints[JointType.HipRight].Position.Y; + } + } +} diff --git a/MyGestureBank/PostureHandUpLeft.cs b/MyGestureBank/PostureHandUpLeft.cs index 348eaa1..35acda0 100644 --- a/MyGestureBank/PostureHandUpLeft.cs +++ b/MyGestureBank/PostureHandUpLeft.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace MyGestureBank @@ -26,6 +27,9 @@ namespace MyGestureBank { if (TestPosture(body)) { + Console.WriteLine("Gesture recognized, hand up left"); + Thread.Sleep(1000); + OnGestureRecognized(); } } @@ -39,7 +43,8 @@ namespace MyGestureBank protected override bool TestPosture(Body body) { // Check if the left hand is above the left shoulder - return body.Joints[JointType.HandLeft].Position.Y > body.Joints[JointType.ShoulderLeft].Position.Y; + return body.Joints[JointType.HandLeft].Position.Y > body.Joints[JointType.SpineShoulder].Position.Y && + body.Joints[JointType.HandRight].Position.Y < body.Joints[JointType.SpineShoulder].Position.Y; } } } diff --git a/MyGestureBank/PostureHandUpRight.cs b/MyGestureBank/PostureHandUpRight.cs index 03f0f2b..bd880f7 100644 --- a/MyGestureBank/PostureHandUpRight.cs +++ b/MyGestureBank/PostureHandUpRight.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Kinect; +using System.Threading; namespace MyGestureBank { @@ -29,6 +30,8 @@ namespace MyGestureBank { if(TestPosture(body)) { + Console.WriteLine("Gesture recognized, hand up right"); + Thread.Sleep(1000); OnGestureRecognized(); } } @@ -40,7 +43,8 @@ namespace MyGestureBank /// A boolean indicating wheter the posture was detected or not. protected override bool TestPosture(Body body) { - return body.Joints[JointType.HandRight].Position.Y > body.Joints[JointType.ShoulderRight].Position.Y; + return body.Joints[JointType.HandRight].Position.Y > body.Joints[JointType.SpineShoulder].Position.Y && + body.Joints[JointType.HandLeft].Position.Y < body.Joints[JointType.SpineShoulder].Position.Y; } } } diff --git a/MyGestureBank/PostureTwoHandsDown.cs b/MyGestureBank/PostureTwoHandsDown.cs new file mode 100644 index 0000000..7c565e0 --- /dev/null +++ b/MyGestureBank/PostureTwoHandsDown.cs @@ -0,0 +1,51 @@ +using KinectUtils; +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MyGestureBank +{ + /// + /// The two in the middle. + /// + public class PostureTwoHandsDown : Posture + { + public PostureTwoHandsDown() + { + GestureName = "Both hands middle."; + } + + /// + /// The test gesture method. + /// + /// The body + public override void TestGesture(Body body) + { + if (TestPosture(body)) + { + Console.WriteLine("Gesture recognized, both hands middle"); + Thread.Sleep(1000); + OnGestureRecognized(); + } + } + + /// + /// The test posture method. + /// + /// + /// + /// + protected override bool TestPosture(Body body) + { + // Check if two hands are up + return body.Joints[JointType.HandRight].Position.Y < body.Joints[JointType.SpineShoulder].Position.Y && + body.Joints[JointType.HandRight].Position.Y > body.Joints[JointType.HipRight].Position.Y && + body.Joints[JointType.HandLeft].Position.Y < body.Joints[JointType.SpineShoulder].Position.Y && + body.Joints[JointType.HandLeft].Position.Y > body.Joints[JointType.HipLeft].Position.Y; + } + } +} diff --git a/MyGestureBank/PostureTwoHandsUp.cs b/MyGestureBank/PostureTwoHandsUp.cs new file mode 100644 index 0000000..3e91f54 --- /dev/null +++ b/MyGestureBank/PostureTwoHandsUp.cs @@ -0,0 +1,50 @@ +using KinectUtils; +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MyGestureBank +{ + /// + /// The two hands up posture. + /// + public class PostureTwoHandsUp : Posture + { + public PostureTwoHandsUp() + { + GestureName = "Two Hands Up"; + } + + /// + /// The test gesture method. + /// + /// The body + public override void TestGesture(Body body) + { + if (TestPosture(body)) + { + Console.WriteLine("Gesture recognized, two hands up"); + Thread.Sleep(1000); + + OnGestureRecognized(); + } + } + + /// + /// The test posture method. + /// + /// + /// + /// + protected override bool TestPosture(Body body) + { + // Check if two hands are up + return body.Joints[JointType.HandRight].Position.Y > body.Joints[JointType.ShoulderRight].Position.Y && + body.Joints[JointType.HandLeft].Position.Y > body.Joints[JointType.ShoulderLeft].Position.Y; + } + } +} diff --git a/PostureTester/PostureTester.csproj b/PostureTester/PostureTester.csproj index 3026d1c..d183f6d 100644 --- a/PostureTester/PostureTester.csproj +++ b/PostureTester/PostureTester.csproj @@ -54,6 +54,10 @@ + + {E527438A-DFA2-4EC6-9891-D4956152B093} + KinectConnection + {2BC300E4-D3C1-4E17-A011-380EDB793182} KinectUtils diff --git a/PostureTester/Program.cs b/PostureTester/Program.cs index 0683629..776ed39 100644 --- a/PostureTester/Program.cs +++ b/PostureTester/Program.cs @@ -12,63 +12,90 @@ namespace PostureTester { public class Program { - private static KinectSensor kinectSensor; - private static PostureHandUpRight postureHandUpRight = new PostureHandUpRight(); - - static void Main(string[] args) { - kinectSensor = KinectSensor.GetDefault(); - postureHandUpRight.GestureRecognized += PostureHandUpRight_GestureRecognized; - - if (kinectSensor != null) - { - kinectSensor.Open(); - - // Utilisation du bloc using pour bodyFrameReader - using (var bodyFrameReader = kinectSensor.BodyFrameSource.OpenReader()) - { - if (bodyFrameReader != null) - { - // Abonnement à l'événement FrameArrived - bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived; - - Console.WriteLine("Lecture des données du corps en cours... Appuyez sur une touche pour quitter."); - Console.ReadKey(); - } - } - } - - if (kinectSensor != null) - { - kinectSensor.Close(); - kinectSensor = null; - } - } + PostureHandUpRight postureHandUpRight = new PostureHandUpRight(); + PostureHandUpLeft postureHandUpLeft = new PostureHandUpLeft(); + PostureHandDownLeft postureHandDownLeft = new PostureHandDownLeft(); + PostureHandDownRight postureHandDownRight = new PostureHandDownRight(); + PostureTwoHandsDown postureTwoHandsDown = new PostureTwoHandsDown(); + PostureTwoHandsUp postureTwoHandsUp = new PostureTwoHandsUp(); - private static void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) - { - using (var bodyFrame = e.FrameReference.AcquireFrame()) - { - if (bodyFrame != null) - { - Body[] bodies = new Body[bodyFrame.BodyCount]; - bodyFrame.GetAndRefreshBodyData(bodies); - - foreach (Body body in bodies) - { - if (body.IsTracked) - { - postureHandUpRight.TestGesture(body); - } - } - } - } - } - private static void PostureHandUpRight_GestureRecognized(object sender, GestureRecognizedEventArgs e) - { - Console.WriteLine("Posture Hand Up Right reconnue !"); + BaseGesture[] gestures = new BaseGesture[6]; + gestures[0] = postureHandUpLeft; + gestures[1] = postureHandUpRight; + gestures[2] = postureHandDownLeft; + gestures[3] = postureHandDownRight; + gestures[4] = postureTwoHandsDown; + gestures[5] = postureTwoHandsUp; + + GestureManager.AddGestures(gestures); + + GestureManager.StartAcquiringFrames(GestureManager.KinectManager); + + // Keep the program running until a key is pressed + Console.WriteLine("Press any key to exit..."); + Console.ReadKey(); } - } + + /* private static KinectSensor kinectSensor; + private static PostureHandUpRight postureHandUpRight = new PostureHandUpRight(); + + + static void Main(string[] args) + { + kinectSensor = KinectSensor.GetDefault(); + postureHandUpRight.GestureRecognized += PostureHandUpRight_GestureRecognized; + + if (kinectSensor != null) + { + kinectSensor.Open(); + + // Utilisation du bloc using pour bodyFrameReader + using (var bodyFrameReader = kinectSensor.BodyFrameSource.OpenReader()) + { + if (bodyFrameReader != null) + { + // Abonnement à l'événement FrameArrived + bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived; + + Console.WriteLine("Lecture des données du corps en cours... Appuyez sur une touche pour quitter."); + Console.ReadKey(); + } + } + } + + if (kinectSensor != null) + { + kinectSensor.Close(); + kinectSensor = null; + } + } + + private static void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e) + { + using (var bodyFrame = e.FrameReference.AcquireFrame()) + { + if (bodyFrame != null) + { + Body[] bodies = new Body[bodyFrame.BodyCount]; + bodyFrame.GetAndRefreshBodyData(bodies); + + foreach (Body body in bodies) + { + if (body.IsTracked) + { + postureHandUpRight.TestGesture(body); + } + } + } + } + } + + private static void PostureHandUpRight_GestureRecognized(object sender, GestureRecognizedEventArgs e) + { + Console.WriteLine("Posture Hand Up Right reconnue !"); + }*/ +} }