适用于Windows V2的Kinect

时间:2016-05-09 05:48:42

标签: kinect-sdk

在我的项目中,我需要检测多个手势,我制作了一个带有2个连续手势的.gbd文件。当.gbd文件有一个手势时,我可以检测到连续手势。当有2个手势时,我无法阅读任何一个手势。有人知道原因吗?在这里,我发布我的代码。我使用了sdk中的Body基础示例,并分别添加了必要的功能以避免内存泄漏。

//分离函数来初始化数据库,vgb框架源和vgb框架阅读器

void CBodyBasics :: InitializeVGB()

{

HRESULT hr;

std :: wstring sDbFile = L" ContinuousGestures.gbd&#34 ;;

hr = CreateVisualGestureBuilderDatabaseInstanceFromFile(sDbFile.c_str(),& m_pGestureDatabase);

if (SUCCEEDED(hr))
{
    OutputDebugString(L"Success DB\n");
}
else
{
    OutputDebugString(L"Failure DB\n");
}
for (int i = 0; i < BODY_COUNT; i++)
{
    // Source
    hr = CreateVisualGestureBuilderFrameSource(m_pKinectSensor, 0, &m_pVGBFrameSource[i]);
    if (SUCCEEDED(hr))
    {
        OutputDebugString(L"Source created\n");
    }
    else
    {
        OutputDebugString(L"Soure creation failed\n");
    }
    // Reader
    hr = m_pVGBFrameSource[i]->OpenReader(&m_pVGBFrameReader[i]);
    if (SUCCEEDED(hr))
    {
        OutputDebugString(L"reader opened\n");
    }
    else
    {
        OutputDebugString(L"No reader\n");
    }
}

}

//用于查找可用的手势计数,将可用的手势添加到源

    void CBodyBasics::CheckGestureMultiple()
{
    HRESULT hr;
    hr = m_pGestureDatabase->get_AvailableGesturesCount(&iGestureCount);
    //gesture count
    if (SUCCEEDED(hr))
    {
        OutputDebugString(L"gesture #\n");
    }
    else
    {
        OutputDebugString(L"0 gesture\n");
    }
    StringCchPrintf(smessage, _countof(smessage), L"Gesture Count %d \n", iGestureCount);
    OutputDebugString(smessage);
    hr = m_pGestureDatabase->get_AvailableGestures(iGestureCount, m_pgesturelist);
    //gesture list
    if (SUCCEEDED(hr) && m_pgesturelist != nullptr)
    {
        OutputDebugString(L"received available gestures\n");
        GestureType mtype;
        WCHAR sName[260];
        for (int i = 0; i < iGestureCount; ++i)
        {
            hr = m_pgesturelist[i]->get_GestureType(&mtype);
            //gesture type
            if (SUCCEEDED(hr))
            {
                OutputDebugString(L"gesture type\n");
                if (mtype == GestureType_Discrete)
                {
                    OutputDebugString(L"discrete\n");
                }
                else if (mtype == GestureType_Continuous)
                {
                    OutputDebugString(L"continuous\n");
                }
            }
            else
            {
                OutputDebugString(L"no gesture type\n");
            }
            hr = m_pgesturelist[i]->get_Name(260, sName);
            //gesture name
            if (SUCCEEDED(hr))
            {
                OutputDebugString(L"gesture name\n");
                StringCchPrintf(smessage, _countof(smessage), L"%s \t %d \n", sName, i);
                OutputDebugString(smessage);
            }
            else
            {
                OutputDebugString(L"no name\n");
            }
        }
    }
    else
    {
        OutputDebugString(L"not received gestures\n");
    }
    for (int i = 0; i < BODY_COUNT; ++i)
    {
        //add gestures to the source
        hr = m_pVGBFrameSource[i]->AddGestures(iGestureCount, m_pgesturelist);
        if (SUCCEEDED(hr))
        {
            OutputDebugString(L"Gesture added\n");
        }
        else
        {
            OutputDebugString(L"not added\n");
        }
    }
}

//用于读取vgb帧并在检测到手势时进行处理

void CBodyBasics::ReadVGBFrameMultiple(int t_bodyCount)

{     HRESULT hr;

for (int i = 0; i < t_bodyCount; i++)
{
    hr = m_pVGBFrameReader[i]->CalculateAndAcquireLatestFrame(&m_pVGBFrame);
    if (SUCCEEDED(hr) && m_pVGBFrame != nullptr)
    {
        OutputDebugString(L"vgb frame\n");
        BOOLEAN bGestureTracked = false;
        hr = m_pVGBFrame->get_IsTrackingIdValid(&bGestureTracked);
        if (SUCCEEDED(hr) && bGestureTracked)
        {
            OutputDebugString(L"valid gesture id and gesture tracked\n");
            GestureType gesturetype;
            m_pgesturelist[i]->get_GestureType(&gesturetype);
            switch (gesturetype)
            {
            case GestureType::GestureType_Discrete:

                hr = m_pVGBFrame->get_DiscreteGestureResult(m_pgesturelist[i], &m_pDiscreteResult);
                if (SUCCEEDED(hr) && m_pDiscreteResult != nullptr)
                {
                    OutputDebugString(L"discrete result\n");
                    BOOLEAN bDetected = false;
                    hr = m_pDiscreteResult->get_Detected(&bDetected);

                    if (SUCCEEDED(hr) && bDetected)
                    {
                        OutputDebugString(L"gesture detected\n");
                        StringCchPrintf(smessage, _countof(smessage), L"gesture detected %d\n", bDetected);
                        OutputDebugString(smessage);
                        float confidence = 0.0f;
                        hr = m_pDiscreteResult->get_Confidence(&confidence);
                        if (SUCCEEDED(hr) && confidence != 0.0f)
                        {
                            OutputDebugString(L"confidence is detected \t");
                            //if (confidence > 0.3f)
                            //{
                            StringCchPrintf(smessage, _countof(smessage), L"confidence value is %f\n", confidence);
                            OutputDebugString(smessage);
                            //}

                        }
                        else
                        {
                            OutputDebugString(L"no confidence\n");
                        }
                    }
                    else
                    {
                        OutputDebugString(L"gesture not detected\n");
                    }
                }
                else
                {
                    OutputDebugString(L"no discrete result\n");
                }


            case GestureType::GestureType_Continuous:

                hr = m_pVGBFrame->get_ContinuousGestureResult(m_pgesturelist[i], &m_pContinousResult);
                if (SUCCEEDED(hr) && m_pContinousResult != nullptr)
                {
                    OutputDebugString(L"continuous gesture\n");
                    WCHAR gesturename[100];
                    hr = m_pgesturelist[i]->get_Name(100, gesturename);
                    //if (gesturename == L"SwipeLeft")
                    //{
                    float progress = 0.0f;
                    OutputDebugString(L"get progress of continuous gesture \t");
                    hr = m_pContinousResult->get_Progress(&progress);
                    if (SUCCEEDED(hr) && progress != 0.0f)
                    {
                        StringCchPrintf(smessage, _countof(smessage), L"progress value is %f\n", progress);
                        OutputDebugString(smessage);
                    }
                    //}
                }
                else
                {
                    OutputDebugString(L"no continuous gesture\n");
                }

            default:
                break;
            }
        }
        else
        {
            OutputDebugString(L"invalid gesture id\n");
        }
    }
    else
    {
        OutputDebugString(L"no frame\n");
    }
}

}

我的代码有问题吗?我需要修改我的代码吗?谁能帮我?

0 个答案:

没有答案