middleware/src/Tracking/Gestures/GestureDetector.cs
changeset 0 6fefd4afe506
child 3 92f19af39024
equal deleted inserted replaced
-1:000000000000 0:6fefd4afe506
       
     1 using System;
       
     2 using System.Collections.Generic;
       
     3 using System.Linq;
       
     4 using System.Text;
       
     5 using Microsoft.Kinect;
       
     6 using System.Drawing;
       
     7 
       
     8 namespace Trakers.Tracking.Gestures
       
     9 {
       
    10     public class GestureDetector
       
    11     {
       
    12         //Historique des positions du squelette.
       
    13         protected List<List<Joint>> history = new List<List<Joint>>();
       
    14         //protected JointCollection previousSkeleton;
       
    15 
       
    16         //Voici les ID des noeuds d'un squelette.
       
    17         protected int hipCenterID = 0, spineID = 1, shoulderCenterID = 2, headID = 3;
       
    18         protected int shoulderLeftID = 4, elbowLeftID = 5, wristLeftID = 6, handLeftID = 7;
       
    19         protected int shoulderRightID = 8, elbowRightID = 9, wristRightID = 10, handRightID = 11;
       
    20         protected int hipLeftID = 12, kneeLeftID = 13, ankleLeftID = 14, footLeftID = 15;
       
    21         protected int hipRightID = 16, kneeRightID = 17, ankleRightID = 18, footRightID = 19;
       
    22 
       
    23         //Elements nécessaires à la reconnaissance du geste :
       
    24         //Distance du parcours du geste (va dépendre de la distance de l'utilisateur).
       
    25         protected float refDistance;
       
    26         //Position de départ pour le geste.
       
    27         protected SkeletonPoint startPoint;
       
    28         //Estimation du temps que prend la gesture (en s).
       
    29         protected float gesturePeriod = 1;
       
    30         //Estimation du nombre d'indexes par seconde (framerate).
       
    31         protected int indexesPerSecond = 30;
       
    32         //Estimation du nombre de positions du squelette à vérifier dans l'historique.
       
    33         protected int indexesToCheck;// = gesturePeriod * indexesPerSecond;
       
    34 
       
    35         public GestureDetector()
       
    36         {
       
    37             
       
    38         }
       
    39 
       
    40         //Setters.
       
    41         public void setRefDistance(float _refDistance)
       
    42         {
       
    43             refDistance = _refDistance;
       
    44         }
       
    45         public void setStartPoint(SkeletonPoint _startPoint)
       
    46         {
       
    47             startPoint = _startPoint;
       
    48         }
       
    49         public void setGesturePeriod(float _gesturePeriod)
       
    50         {
       
    51             gesturePeriod = _gesturePeriod;
       
    52         }
       
    53         public void setIndexesPerSecond(int _indexesPerSecond)
       
    54         {
       
    55             indexesPerSecond = _indexesPerSecond;
       
    56         }
       
    57         public void setIndexesToCheck(int _indexesToCheck)
       
    58         {
       
    59             indexesToCheck = _indexesToCheck;
       
    60         }
       
    61 
       
    62         //Getters.
       
    63         public float getRefDistance()
       
    64         {
       
    65             return refDistance;
       
    66         }
       
    67         public SkeletonPoint getStartPoint()
       
    68         {
       
    69             return startPoint;
       
    70         }
       
    71         public float getGesturePeriod()
       
    72         {
       
    73             return gesturePeriod;
       
    74         }
       
    75         public int getIndexesPerSecond()
       
    76         {
       
    77             return indexesPerSecond;
       
    78         }
       
    79         public int getIndexesToCheck()
       
    80         {
       
    81             return indexesToCheck;
       
    82         } 
       
    83 
       
    84         //Stocke les gestes du skelette pour ~3 seconds (si on prend en compte que le framerate de la Kinect
       
    85         //est de ~30 fps.
       
    86         public void UpdateSkeletonHistory(List<Joint> latestSkeleton)
       
    87         {
       
    88             /*if (previousSkeleton == null)
       
    89                 previousSkeleton = latestSkeleton;
       
    90             else if (previousSkeleton != latestSkeleton)
       
    91             {*/
       
    92                 history.Add(latestSkeleton);
       
    93                 /*if (history.Count > 3)
       
    94                 {
       
    95                     System.Console.WriteLine(history[0][0].Position.X + " " + history[1][0].Position.X + " " + history[2][0].Position.X);
       
    96                 }*/
       
    97                 if (history.Count > 90)
       
    98                 {
       
    99                     history.RemoveAt(0);
       
   100                 }
       
   101             //}
       
   102         }
       
   103 
       
   104         /*public bool CheckForHandWave()
       
   105         {
       
   106             //Crée un historique de squelette local, puisque l'historique est mis à jour toutes les ~1/30 s.
       
   107             List<Skeleton> history = new List<Skeleton>(history);
       
   108             //Si il n'y a pas assez de positions dans l'historique local pour vérifier le geste.
       
   109             if (history.Count < indexesToCheck)
       
   110                 return false;
       
   111 
       
   112             float refDistance = Math.Abs(history[0].Joints.ElementAt(spineID).Position.Y - history[0].Joints.ElementAt(hipCenterID).Position.Y);
       
   113             float startPos = history[history.Count - indexesToCheck].Joints.ElementAt(handRightID).Position.X;
       
   114             bool movedRight = false;
       
   115             for (int i = history.Count - indexesToCheck + 1; i < history.Count; i++)
       
   116             {
       
   117                 // Throughout the gesture period, right hand should be above theelbow, below the head and hand should be higher than the wrist
       
   118                 if (!(history[i].Joints[JointID.HandRight].Position.Y > history[i].Joints[JointID.ElbowRight].Position.Y + refDistance &&
       
   119                 history[i].Joints[JointID.HandRight].Position.Y < history[i].Joints[JointID.Head].Position.Y &&
       
   120                 history[i].Joints[JointID.HandRight].Position.Y > history[i].Joints[JointID.WristRight].Position.Y + refDistance / 4))
       
   121                 {
       
   122                     return false;
       
   123                 }
       
   124                 // If the previous condition was met, check if the hand has moved to the right
       
   125                 if (history[i].Joints[JointID.HandRight].Position.X >= startPos + refDistance / 2 && !movedRight)
       
   126                 {
       
   127                     movedRight = true;
       
   128                 }
       
   129                 // If the hand did move to the right, check if it returned near the original position
       
   130                 if (movedRight && history[i].Joints[JointID.HandRight].Position.X <= startPos + refDistance / 5)
       
   131                 {
       
   132                     skeletonHistory.Clear();
       
   133                     return true;
       
   134                 }
       
   135             }
       
   136                 return false;
       
   137         }*/
       
   138     }
       
   139 }