Search code examples
c#multithreadingeventskinectkinect-sdk

Kinect v2 how to know if frames queue up


I'm doing some complex robotics vision with a kinect v2.0 camera. Sometimes my heavy optimised maths take a bit longer then 33ms. (depending on what is in front of the camera)

As with 30frames p/sec, my math needs to be fast, and it is quite fast. However still sometimes its just not fast enough, and i get a few ms behind. And then it seams that frames do queue up.

From what i understand is that a line such as

depthFrameReader.FrameArrived += DepthFrameReader_FrameArrived

Creates events on arrival of new frames, and those events can queue up. Well, i think those events are executed in parralel (or at least in another thread i think). As when i am using visual studio 2015 debugmode, i sometimes see that those threads take a bit longer then they should. And when this happens it all potentially can queues up. Like a traffic jam.

A frame as defined by:

private void DepthFrameReader_FrameArrived(object sender, DepthFrameArrivedEventArgs e)
    { // an in between function maybe to detect queue somehow..

       doDepthMath(e);
    }

    private void doDepthMath(DepthFrameArrivedEventArgs e)
    {
        var frameReference = e.FrameReference;
        {
            var frame = frameReference.AcquireFrame();

Has a property called, frame.RelativeTime() ...

Is that mend to be used to check if indeed the frame was created in shorter then 33ms interval ? ..but wouldnt that be always the case ??

In essence i'm looking for a way to simply drop frames if my math still is busy, but i'm not sure how to know this, since from my understanding of this, an event doesn't know about other triggered events.

Maybe using the inbetween function i could check if a calculation is ready? But i'm not sure how to know if those events queue up by code, outside from my debugging view in vs2015.


Solution

  • Why don't you use a flag to signal if your math calculation has been finished or not?

    private bool flagDoingMath = false;
    
    private void DepthFrameReader_FrameArrived(object sender, DepthFrameArrivedEventArgs e)
    {
        if(flagDoingMath) return;
    
        flagDoingMath = true;
    
        doDepthMath(e);
    
        flagDoingMath = false;
    }