Advertisement
Guest User

Untitled

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