You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
KinectExercise_FrancoBroda/MyGestureBank/SoccerShootGesture .cs

85 lines
3.2 KiB

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;
}
}
}