Nitin Labhishetty
Nitin Labhishetty

Reputation: 1348

Sharing kinect stream data between parallel threads

The problem I'm trying to solve is that I'm combining classes from different projects into a single project. The functions in these classes use KinectSensor variable which is a member of their class and recognize different gestures. I'm trying to run the gesture recognition functions in parallel threads but want to know how to pass kinect data to them.

Is there a method to share kinect sensor stream data (Color stream, depth stream etc.) data among threads of a single program running in parallel? Will a event triggered like "skeleton frame ready" be received by all the threads receiving the kinect stream data?

I'm using kinect 360, in c# using sdk 1.8

Upvotes: 2

Views: 480

Answers (1)

Vito Gentile
Vito Gentile

Reputation: 14366

The KinectSensor object represents a single Kinect, and a single Kinect can only be used by a single program.

If the two thread you are talking about are part of the same program, you can "share the streams" by simply share the access to the same object.

However, the color, depth and skeleton streams are obtained by registering callbacks methods to an event. So what you can do is the following:

  1. share the reference to the same KinectSensor object (preinitialized) to both threads;
  2. in each thread, register a callback to the depth, color and skeleton stream, which update the content of the streams in a variable that is local to the thread (or used only by one thread):

    // Reference to the single KinectSensor object
    private KinectSensor kinectSensor;
    
    // Local variables with depth, color and skeletal information
    private Skeleton[] skeleton_thread1;
    private Skeleton[] skeleton_thread2;
    private short[] depth_thread1;
    private short[] depth_thread2;
    private byte[] color_thread1;
    private byte[] color_thread2;
    
    // ...
    
    // Register callbacks (you must so this both in thread1 and thread2)
    // Assume that here we are refererring to thread1
    kinectSensor.ColorFrameReady += new EventHandler<ColorFrameReadyEventArgs>(kinectSensor_ColorFrameReady1);
    kinectSensor.DepthFrameReady += new EventHandler<DepthFrameReadyEventArgs>(kinectSensor_DepthFrameReady1);
    kinectSensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(kinectSensor_SkeletonFrameReady1);
    
    // ...
    
    private void kinectSensor_SkeletonFrameReady1(object sender, SkeletonFrameReadyEventArgs e)
    {
        this.skeletonFrame_thread1 = 
        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                this.skeleton_thread1 = new Skeleton[skeletonFrame.SkeletonArrayLength];
    
                skeletonFrame.CopySkeletonDataTo(this.skeleton_thread1);
    
                // Do stuff
            }
            else
            {
                // Do stuff
            }
        }
    }
    
    private void kinectSensor_ColorFrameReady1(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame())
        {
            if (colorImageFrame != null)
            {
                this.color_thread1 = new byte[colorImageFrame.PixelDataLength];
                colorImageFrame.CopyPixelDataTo(this.color_thread1);
    
                // Do Stuff
            }
            else
            {
                // Do stuff
            }
        }
    }
    
    private void kinectSensor_DepthFrameReady1(object sender, DepthImageFrameReadyEventArgs e)
    {
        using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())
        {
            if (depthImageFrame != null)
            {
                this.depth_thread1 = new short[depthImageFrame.PixelDataLength];
                depthImageFrame.CopyPixelDataTo(this.depth_thread1);
    
                // Do Stuff
            }
            else
            {
                // Do stuff
            }
        }
    }
    

Upvotes: 1

Related Questions