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/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 1315626..d6dc9d1 100644 --- a/Sources/KinectUtils/Gesture.cs +++ b/Sources/KinectUtils/Gesture.cs @@ -7,36 +7,36 @@ using System.Threading.Tasks; namespace KinectUtils { - abstract class Gesture : BaseGesture + public abstract class Gesture : BaseGesture { - public bool IsTesting { get; protected set; } + public bool IsRecognitionRunning { get; set; } + protected int MinNbOfFrames = 10; // Exemple de valeur, ajustez selon le geste protected int MaxNbOfFrames = 50; // Exemple de valeur, ajustez selon le geste - private int mCurrentFrameCount = 0; - // public bool isRecognitionRunning { get; set; } + private int currentFrameCount = 0; public override void TestGesture(Body body) { - if (!IsTesting) + if (!IsRecognitionRunning) { if (TestInitialConditions(body)) { - IsTesting = true; - mCurrentFrameCount = 0; + IsRecognitionRunning = true; + currentFrameCount = 0; } } else { - mCurrentFrameCount++; + currentFrameCount++; - if (!TestPosture(body) || !TestRunningGesture(body) || mCurrentFrameCount > MaxNbOfFrames) + if (!TestPosture(body) || !TestRunningGesture(body) || currentFrameCount > MaxNbOfFrames) { - IsTesting = false; + IsRecognitionRunning = false; } - else if (TestEndConditions(body) && mCurrentFrameCount >= MinNbOfFrames) + else if (TestEndConditions(body) && currentFrameCount >= MinNbOfFrames) { OnGestureRecognized(body); - IsTesting = false; + IsRecognitionRunning = false; } } } @@ -47,3 +47,4 @@ namespace KinectUtils abstract protected bool TestEndConditions(Body body); } } + diff --git a/Sources/KinectUtils/GestureManager.cs b/Sources/KinectUtils/GestureManager.cs index 8599a34..cece5c1 100644 --- a/Sources/KinectUtils/GestureManager.cs +++ b/Sources/KinectUtils/GestureManager.cs @@ -1,16 +1,21 @@ 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 { public static class GestureManager { - public static event EventHandler GestureRecognized; - public static List KnownGestures = new List(); + static private BodyFrameReader bodyFrameReader; + static private Body[] bodies; + static event EventHandler GestureRecognized; + static KinectManager KinectManager { get; set; } + static List KnownGestures { get; set; } public static void AddGestures(IGestureFactory factory) { @@ -39,7 +44,29 @@ namespace KinectUtils static public void StartAcquiringFrames(KinectManager kinectManager) { - throw new NotImplementedException(); + bodyFrameReader = kinectManager.Sensor.BodyFrameSource.OpenReader(); + bodyFrameReader.FrameArrived += Reader_BodyFrameArrived; + } + + static private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e) + { + 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) + { + gesture.TestGesture(body); + } + } + } + } + } } } } 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/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/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 + }; + } + } +} 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";