diff --git a/KinectUtils/Gesture.cs b/KinectUtils/Gesture.cs new file mode 100644 index 0000000..e94935b --- /dev/null +++ b/KinectUtils/Gesture.cs @@ -0,0 +1,29 @@ +using Microsoft.Kinect; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace KinectUtils +{ + public abstract class Gesture : BaseGesture + { + public bool IsTesting; + + protected int MinNbOfFrames; + + protected int MaxNbOfFrames; + + private int mCurrentFrameCount; + + 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/KinectUtils/KinectUtils.csproj b/KinectUtils/KinectUtils.csproj index 4778cf1..bcd5d71 100644 --- a/KinectUtils/KinectUtils.csproj +++ b/KinectUtils/KinectUtils.csproj @@ -45,6 +45,7 @@ + diff --git a/MyGestureBank/MyGestureBank.csproj b/MyGestureBank/MyGestureBank.csproj index 2ae77cf..5ad288c 100644 --- a/MyGestureBank/MyGestureBank.csproj +++ b/MyGestureBank/MyGestureBank.csproj @@ -51,6 +51,7 @@ + diff --git a/MyGestureBank/SoccerShootGesture .cs b/MyGestureBank/SoccerShootGesture .cs new file mode 100644 index 0000000..f6a75df --- /dev/null +++ b/MyGestureBank/SoccerShootGesture .cs @@ -0,0 +1,84 @@ +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 +{ + public class SoccerShootGesture : Gesture + { + private CameraSpacePoint lastLeftFootPosition; + private CameraSpacePoint lastRightFootPosition; + + public SoccerShootGesture() + { + MinNbOfFrames = 10; + MaxNbOfFrames = 30; + } + + public override void TestGesture(Body body) + { + if (TestPosture(body)) + { + Console.WriteLine("Gesture recognized, shooting motion"); + Thread.Sleep(1000); + + OnGestureRecognized(); + } + } + + protected override bool TestEndConditions(Body body) + { + float threshold = 0.05f; + + bool areFeetClose = Math.Abs(body.Joints[JointType.FootLeft].Position.X - body.Joints[JointType.FootRight].Position.X) < threshold; + return areFeetClose; + } + + protected override bool TestInitialConditions(Body body) + { + CameraSpacePoint currentLeftFootPosition = body.Joints[JointType.FootLeft].Position; + CameraSpacePoint currentRightFootPosition = body.Joints[JointType.FootRight].Position; + CameraSpacePoint currentHipPosition = body.Joints[JointType.SpineBase].Position; + CameraSpacePoint currentHeadPosition = body.Joints[JointType.Head].Position; + + bool isWithinDistanceX = Math.Abs(currentLeftFootPosition.X - currentRightFootPosition.X) < Math.Abs(currentHipPosition.Y - currentHeadPosition.Y); + bool isWithinRangeY = IsFootBetweenHeadAndSpinBase(body.Joints[JointType.FootRight].Position, body.Joints[JointType.Head].Position, body.Joints[JointType.SpineBase].Position); + + return isWithinDistanceX && isWithinRangeY; + } + + protected override bool TestPosture(Body body) + { + bool isWithinRangeY = IsFootBetweenHeadAndSpinBase(body.Joints[JointType.FootRight].Position, body.Joints[JointType.Head].Position, body.Joints[JointType.SpineBase].Position); + return isWithinRangeY; + } + + protected override bool TestRunningGesture(Body body) + { + CameraSpacePoint currentLeftFootPosition = body.Joints[JointType.FootLeft].Position; + CameraSpacePoint currentRightFootPosition = body.Joints[JointType.FootRight].Position; + + bool areFeetCloserThanLastFrame = + Math.Abs(currentLeftFootPosition.X) <= lastLeftFootPosition.X + 0.1f && + Math.Abs(currentRightFootPosition.X) <= lastRightFootPosition.X + 0.1f; + + lastLeftFootPosition = currentLeftFootPosition; + lastRightFootPosition = currentRightFootPosition; + + return areFeetCloserThanLastFrame; + } + + private bool IsFootBetweenHeadAndSpinBase(CameraSpacePoint footPosition, CameraSpacePoint headPosition, CameraSpacePoint hipPosition) + { + float maxY = headPosition.Y; + float minY = hipPosition.Y; + + return footPosition.Y < maxY && footPosition.Y > minY; + } + } +} diff --git a/PostureTester/Program.cs b/PostureTester/Program.cs index 776ed39..4b2f713 100644 --- a/PostureTester/Program.cs +++ b/PostureTester/Program.cs @@ -14,21 +14,24 @@ namespace PostureTester { static void Main(string[] args) { - PostureHandUpRight postureHandUpRight = new PostureHandUpRight(); + /*PostureHandUpRight postureHandUpRight = new PostureHandUpRight(); PostureHandUpLeft postureHandUpLeft = new PostureHandUpLeft(); PostureHandDownLeft postureHandDownLeft = new PostureHandDownLeft(); PostureHandDownRight postureHandDownRight = new PostureHandDownRight(); PostureTwoHandsDown postureTwoHandsDown = new PostureTwoHandsDown(); - PostureTwoHandsUp postureTwoHandsUp = new PostureTwoHandsUp(); + PostureTwoHandsUp postureTwoHandsUp = new PostureTwoHandsUp();*/ + SoccerShootGesture soccerShootGesture = new SoccerShootGesture(); - BaseGesture[] gestures = new BaseGesture[6]; - gestures[0] = postureHandUpLeft; + BaseGesture[] gestures = new BaseGesture[1]; + /*gestures[0] = postureHandUpLeft; gestures[1] = postureHandUpRight; gestures[2] = postureHandDownLeft; gestures[3] = postureHandDownRight; gestures[4] = postureTwoHandsDown; - gestures[5] = postureTwoHandsUp; + gestures[5] = postureTwoHandsUp;*/ + + gestures[0] = soccerShootGesture; GestureManager.AddGestures(gestures);