Advertisement
Apidcloud

Untitled

Apr 6th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1.         static Int64 lastFrameID = 0;
  2.         static int handID = -1;
  3.         static float storedSphereRadius = -1;
  4.  
  5.         static float timer = 1;         //Initialize a 2 second timer
  6.         static float TIMER = 1;
  7.  
  8.         static float comparisonValue = 0.65f;
  9.  
  10.         enum CustomGestures { None, Closing, Opening } ;
  11.  
  12.         static void processFrameTest(Frame frame, GameTime gameTime)
  13.         {
  14.             if (frame.Id == lastFrameID || frame.Hands.Count <= 0) return;
  15.  
  16.             Hand hand;
  17.             bool knownHand = false;
  18.             CustomGestures detectedGesture = CustomGestures.None;
  19.  
  20.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  21.             timer -= elapsed;
  22.  
  23.             //if timer expired
  24.             if (timer < 0)
  25.             {
  26.                 //Reset Timer and stored values in order to retrieve new ones
  27.                 handID = -1;
  28.                 storedSphereRadius = -1;
  29.                 timer = TIMER;
  30.             }
  31.  
  32.             // get hand
  33.             if (handID == -1)
  34.                 hand = frame.Hands[0];
  35.             else
  36.             {
  37.                 knownHand = true;
  38.                 hand = frame.Hand(handID);
  39.             }
  40.  
  41.             // is hand invalid?
  42.             if (!hand.IsValid)
  43.             {
  44.                 handID = -1;
  45.                 storedSphereRadius = -1;
  46.                 timer = -1;
  47.                 return;
  48.             }
  49.             else
  50.             {
  51.                 handID = hand.Id;
  52.             }
  53.  
  54.             // if it's the same hand, then we have a sphereRadius value
  55.             // from which we can do a comparison
  56.             if (knownHand && timer >= 0 && timer < TIMER)
  57.             {
  58.                 //Console.WriteLine(sphereRadius.ToString() + "::::" + hand.SphereRadius.ToString());
  59.  
  60.                 // if actual hand's sphere radius is around 65% from stored one,
  61.                 // then we can assume the that the hand is closed
  62.                 if (hand.SphereRadius < storedSphereRadius * comparisonValue)
  63.                 {
  64.                     detectedGesture = CustomGestures.Closing;
  65.  
  66.                     Console.WriteLine("Closing");
  67.  
  68.                 }
  69.                 else if (hand.SphereRadius * comparisonValue > storedSphereRadius)
  70.                 {
  71.                     detectedGesture = CustomGestures.Opening;
  72.  
  73.                     Console.WriteLine("Opening");
  74.                 }
  75.             }
  76.             // in this case, it means we have a new Hand object,
  77.             // hence the need to restore sphereRadius value
  78.             else if (!knownHand)
  79.             {
  80.                 storedSphereRadius = hand.SphereRadius;
  81.             }
  82.  
  83.             // reset since we detected either a closing or an opening gesture
  84.             if (detectedGesture != CustomGestures.None)
  85.             {
  86.                 handID = -1;
  87.                 storedSphereRadius = -1;
  88.                 timer = -1;
  89.             }
  90.  
  91.             lastFrameID = frame.Id;
  92.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement