Advertisement
TwiNNeR

Kinect sample

Dec 11th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.14 KB | None | 0 0
  1. using Metrilus.Aiolos;
  2. using Metrilus.Aiolos.Core;
  3. using Metrilus.Aiolos.Kinect;
  4. using Microsoft.Kinect;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12.  
  13. namespace Metrilus.AiolosK4W.Samples.API
  14. {
  15.     /// <summary>
  16.     /// This is a basic sample to show how to acquire finger data using Metrilus Aiolos.
  17.     /// </summary>
  18.     public class Program
  19.     {
  20.         private static int cursorTop;
  21.         private static int cursorLeft;
  22.         private static KinectSensor kinectSensor;
  23.         private static int width;
  24.         private static int height;
  25.         private static MultiSourceFrameReader reader;
  26.         private static ushort[] depthFrameData;
  27.         private static ushort[] irFrameData;
  28.         private static Body[] bodies;
  29.         private static CoordinateMapper coordinateMapper;
  30.         private static MultiSourceFrameReference frameReference;
  31.         private static object updateLock = new object();
  32.         private static AutoResetEvent dataAvailable = new AutoResetEvent(false);
  33.         private static bool isConnected;
  34.         private static KinectEngine engine = new KinectEngine();
  35.  
  36.         private static void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
  37.         {
  38.             lock (updateLock)
  39.             {
  40.                 frameReference = e.FrameReference;
  41.             }
  42.             dataAvailable.Set();
  43.         }
  44.  
  45.         /// <summary>
  46.         /// Update to get a new frame.
  47.         /// This code is similar to the code in the Kinect SDK samples.
  48.         /// </summary>
  49.         private static void Update()
  50.         {
  51.             if (!isConnected)
  52.                 return;
  53.  
  54.             dataAvailable.WaitOne();
  55.  
  56.             MultiSourceFrame multiSourceFrame = null;
  57.             DepthFrame depthFrame = null;
  58.             InfraredFrame irFrame = null;
  59.             BodyFrame bodyFrame = null;
  60.  
  61.             lock (updateLock)
  62.             {
  63.                 try
  64.                 {
  65.                     if (frameReference != null)
  66.                     {
  67.                         multiSourceFrame = frameReference.AcquireFrame();
  68.  
  69.                         if (multiSourceFrame != null)
  70.                         {
  71.                             DepthFrameReference depthFrameReference = multiSourceFrame.DepthFrameReference;
  72.                             InfraredFrameReference irFrameReference = multiSourceFrame.InfraredFrameReference;
  73.                             BodyFrameReference bodyFrameReference = multiSourceFrame.BodyFrameReference;
  74.  
  75.                             depthFrame = depthFrameReference.AcquireFrame();
  76.                             irFrame = irFrameReference.AcquireFrame();
  77.  
  78.                             if ((depthFrame != null) && (irFrame != null))
  79.                             {
  80.                                 FrameDescription depthFrameDescription = depthFrame.FrameDescription;
  81.                                 FrameDescription irFrameDescription = irFrame.FrameDescription;
  82.  
  83.                                 int depthWidth = depthFrameDescription.Width;
  84.                                 int depthHeight = depthFrameDescription.Height;
  85.                                 int irWidth = irFrameDescription.Width;
  86.                                 int irHeight = irFrameDescription.Height;
  87.  
  88.                                 // verify data and write the new registered frame data to the display bitmap
  89.                                 if (((depthWidth * depthHeight) == depthFrameData.Length) &&
  90.                                     ((irWidth * irHeight) == irFrameData.Length))
  91.                                 {
  92.                                     depthFrame.CopyFrameDataToArray(depthFrameData);
  93.                                     irFrame.CopyFrameDataToArray(irFrameData);
  94.                                 }
  95.  
  96.                                 if (bodyFrameReference != null)
  97.                                 {
  98.                                     bodyFrame = bodyFrameReference.AcquireFrame();
  99.  
  100.                                     if (bodyFrame != null)
  101.                                     {
  102.                                         if (bodies == null || bodies.Length < bodyFrame.BodyCount)
  103.                                         {
  104.                                             bodies = new Body[bodyFrame.BodyCount];
  105.                                         }
  106.                                         using (bodyFrame)
  107.                                         {
  108.                                             bodyFrame.GetAndRefreshBodyData(bodies);
  109.                                         }
  110.                                     }
  111.                                 }
  112.                             }
  113.                         }
  114.                     }
  115.                 }
  116.                 catch (Exception)
  117.                 {
  118.                     // ignore if the frame is no longer available
  119.                 }
  120.                 finally
  121.                 {
  122.                     if (depthFrame != null)
  123.                     {
  124.                         depthFrame.Dispose();
  125.                         depthFrame = null;
  126.                     }
  127.  
  128.                     if (irFrame != null)
  129.                     {
  130.                         irFrame.Dispose();
  131.                         irFrame = null;
  132.                     }
  133.                     if (bodyFrame != null)
  134.                     {
  135.                         bodyFrame.Dispose();
  136.                         bodyFrame = null;
  137.                     }
  138.                     if (multiSourceFrame != null)
  139.                     {
  140.                         multiSourceFrame = null;
  141.                     }
  142.                 }
  143.             }
  144.         }
  145.  
  146.         /// <summary>
  147.         /// Main - This is where you call Aiolos
  148.         /// </summary>
  149.         /// <param name="args"></param>
  150.         private static void Main(string[] args)
  151.         {
  152.             // Do some stuff with the console.
  153.             Console.Clear();
  154.             cursorLeft = Console.CursorLeft;
  155.             cursorTop = Console.CursorTop;
  156.  
  157.             try
  158.             {
  159.                 Console.SetCursorPosition(cursorLeft, cursorTop + 1);
  160.                 Console.WriteLine("Initializing AiolosK4W - Make sure the Kinect SDK detects your body.");
  161.  
  162.                 // Connect to the Kinect.
  163.                 ConnectKinect();
  164.                 do
  165.                 {
  166.                     while (!Console.KeyAvailable)
  167.                     {
  168.                         Console.SetCursorPosition(cursorLeft, cursorTop + 2);
  169.  
  170.                         // Get a new frame.
  171.                         Update();
  172.                            
  173.                         // Call Aiolos
  174.                         // The only thing you have to do is to pass
  175.                         // * width,
  176.                         // * height,
  177.                         // * the raw IR data,
  178.                         // * the raw depth data,
  179.                         // * the bodies and
  180.                         // * the coordinateMapper.
  181.                         // All this data is provided by the Kinect SDK.
  182.                         // A structured including all the hand data is returned.
  183.                         KinectHand[] hands = engine.DetectFingerJoints(width, height, irFrameData, depthFrameData, bodies, coordinateMapper);
  184.  
  185.                         int numHandPrinted = 0;
  186.                         for (int i = 0; i < hands.Length; i++)
  187.                         {
  188.                             if (hands[i] == null)
  189.                                 continue;
  190.                      
  191.                             // Print the finger data.
  192.                             PrintHand(hands[i], i, numHandPrinted);
  193.                             numHandPrinted++;
  194.                         }
  195.                     }
  196.                 } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
  197.             }
  198.             catch (Exception ex)
  199.             {
  200.                 Console.WriteLine(ex.Message);
  201.             }
  202.         }
  203.  
  204.         /// <summary>
  205.         /// Connect to the Kinect
  206.         /// see Kinect SDK samples
  207.         /// </summary>
  208.         private static void ConnectKinect()
  209.         {
  210.             // for Alpha, one sensor is supported
  211.             kinectSensor = KinectSensor.GetDefault();
  212.  
  213.             if (kinectSensor != null)
  214.             {
  215.                 // open the sensor
  216.                 kinectSensor.Open();
  217.  
  218.                 FrameDescription frameDescription = kinectSensor.DepthFrameSource.FrameDescription;
  219.  
  220.                 width = frameDescription.Width;
  221.                 height = frameDescription.Height;
  222.  
  223.                 // open the reader for the depth frames
  224.                 reader = kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);
  225.  
  226.                 // allocate space to put the pixels being received and converted
  227.                 depthFrameData = new ushort[frameDescription.Width * frameDescription.Height];
  228.                 irFrameData = new ushort[frameDescription.Width * frameDescription.Height];
  229.                 coordinateMapper = kinectSensor.CoordinateMapper;
  230.  
  231.                 if (reader != null)
  232.                 {
  233.                     reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;
  234.                 }
  235.                 isConnected = true;
  236.             }
  237.             else
  238.             {
  239.                 // TODO: throw exception
  240.             }
  241.         }
  242.  
  243.         /// <summary>
  244.         /// Print the hand to the console.
  245.         /// </summary>
  246.         /// <param name="hand"></param>
  247.         /// <param name="handNumber"></param>
  248.         /// <param name="outputPos"></param>
  249.         private static void PrintHand(KinectHand hand, int handNumber, int outputPos)
  250.         {
  251.             int firstLine = cursorTop + 3 + (outputPos * 7);
  252.             Console.SetCursorPosition(cursorLeft, firstLine);
  253.             Console.Write("Hand: " + handNumber);
  254.             for (int fingerIdx = 0; fingerIdx < 5; fingerIdx++)
  255.             {
  256.                 try
  257.                 {
  258.                     Color c = Color.FromArgb(180, Engine.FingerColors[fingerIdx]);
  259.  
  260.                     Array f = Enum.GetValues(typeof(Hand.FingerJointType));
  261.                     Array fNames = Enum.GetNames(typeof(Hand.FingerJointType));
  262.                     int idxInEnum = fingerIdx * 3;
  263.                     Microsoft.Kinect.DepthSpacePoint[] p = new Microsoft.Kinect.DepthSpacePoint[3];
  264.                     string[] jointNames = new string[3];
  265.                     for (int j = 0; j < 3; j++)
  266.                     {
  267.                         Hand.FingerJointType jt = (Hand.FingerJointType)f.GetValue(idxInEnum + j);
  268.                         p[j] = hand.FingerJoints[jt];
  269.                         jointNames[j] = (string)fNames.GetValue(idxInEnum + j);
  270.                         Console.SetCursorPosition(cursorLeft + j * 25, firstLine + fingerIdx+1);
  271.                         Console.Write(jointNames[j] + ": " + p[j].X.ToString("0.0") + ";" + p[j].Y.ToString("0.0") + "\n");
  272.                     }
  273.                 }
  274.                 catch (Exception)
  275.                 {
  276.                 }
  277.             }
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement