Search code examples
c++kinectframe-ratekinect-sdkkinect-v2

Kinect 2 - AcquireLatestFrame() fails most of the time


The following C++ code is given which continuously fetches the latest frame from a Kinect 2.

int main()
{
    setupKinect();
    acquireFrames();  
    return 0;
}

template<class Interface>
inline static void safeRelease(Interface *&interfaceToRelease)
{
    if (interfaceToRelease != nullptr) {
        interfaceToRelease->Release();
        interfaceToRelease = nullptr;
    }
}

void acquireFrames() {
    while (true) {
        if (bodyFrameReader != nullptr) {
            IBodyFrame* bodyFrame = nullptr;
            HRESULT hr = bodyFrameReader->AcquireLatestFrame(&bodyFrame);
            if (SUCCEEDED(hr)) {
                // processing bodyFrame 
            } else {    
                // acquiring frame failed   
            }
            safeRelease(bodyFrame);
        }
    }
}

void setupKinect() {
    IKinectSensor * sensor = nullptr;
    HRESULT hr = GetDefaultKinectSensor(&sensor);
    if (SUCCEEDED(hr)) {
        hr = sensor->Open();
        if (SUCCEEDED(hr)) {
            IBodyFrameSource* bodyFrameSource = nullptr;
            hr = sensor->get_BodyFrameSource(&bodyFrameSource);
            if (SUCCEEDED(hr)) {
                hr = bodyFrameSource->OpenReader(&bodyFrameReader);

            }
            safeRelease(bodyFrameSource);
        }
    }
    safeRelease(sensor);
}

Why does AcquireLatestFrame most often returns a failed HRESULT? Some testing has revealed that the function succeeds only about 30 times per second, so it seems that a certain frame will be acquired/returned at most once by this function (Kinect framerate is 30 fps). Is this right?


Solution

  • Yes, you are right.

    Source: see the “30hz” under “Depth sensing” in the table here: (you may need to scroll down a bit)

    https://developer.microsoft.com/en-us/windows/kinect/hardware

    The documentation of the function says:

    Return value

    Type: HRESULT

    Returns S_OK if successful; otherwise, returns a failure code.

    (source: https://msdn.microsoft.com/en-us/library/microsoft.kinect.kinect.ibodyframereader.acquirelatestframe.aspx)

    The failure HRESULT code which it returns most of the time is E_PENDING. That implies that the new frame is not ready yet.


    To answer your question: Why does AcquireLatestFrame most often returns a failed HRESULT?

    Because there is no need to process the same input data more than once (you would just waste your CPU time by computing the same results over and over).