CVirtuous
CVirtuous

Reputation: 213

Kinect 2 failing to 'AcquireLatestFrame'

I am using the following code from a sample I found online. It seems to pick up one frame but subsequently never succeeds to 'AcquireLatestFrame'. I have the exact same problem with the body reader. Can anyone see an issue that might be causing this?

IKinectSensor* pSensor;
HRESULT hResult = S_OK;
hResult = GetDefaultKinectSensor(&pSensor);
if (FAILED(hResult)) {
    std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
    return -1;
}

hResult = pSensor->Open();
if (FAILED(hResult)) {
    std::cerr << "Error : IKinectSensor::Open()" << std::endl;
    return -1;
}

// Source
IColorFrameSource* pColorSource;
hResult = pSensor->get_ColorFrameSource(&pColorSource);
if (FAILED(hResult)) {
    std::cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
    return -1;
}

// Reader
IColorFrameReader* pColorReader;
hResult = pColorSource->OpenReader(&pColorReader);
if (FAILED(hResult)) {
    std::cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
    return -1;
}

// Description
IFrameDescription* pDescription;
hResult = pColorSource->get_FrameDescription(&pDescription);
if (FAILED(hResult)) {
    std::cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
    return -1;
}

int width = 0;
int height = 0;
pDescription->get_Width(&width); // 1920
pDescription->get_Height(&height); // 1080
unsigned int bufferSize = width * height * 4 * sizeof(unsigned char);

cv::Mat bufferMat(height, width, CV_8UC4);
cv::Mat colorMat(height / 2, width / 2, CV_8UC4);
cv::namedWindow("Color");

while (1) {
    // Frame
    IColorFrame* pColorFrame = nullptr;
    hResult = pColorReader->AcquireLatestFrame(&pColorFrame);
    if (SUCCEEDED(hResult)) {
        hResult = pColorFrame->CopyConvertedFrameDataToArray(bufferSize, reinterpret_cast<BYTE*>(bufferMat.data), ColorImageFormat::ColorImageFormat_Bgra);
        if (SUCCEEDED(hResult)) {
            cv::resize(bufferMat, colorMat, cv::Size(), 0.5, 0.5);
        }
    }
    else
    {
        cout << "Can't aquire latest frame.\n";
    }

    cv::imshow("Color", colorMat);

    if (cv::waitKey(30) == VK_ESCAPE) {
        break;
    }
}

if (pSensor) {
    pSensor->Close();
}

cv::destroyAllWindows();

Upvotes: 2

Views: 1370

Answers (1)

CVirtuous
CVirtuous

Reputation: 213

I wasn't releasing the pColorFrame. Doing so solved the issue.

Upvotes: 4

Related Questions