MS Kinect FaceTracker正在创建IFTResult

MS Kinect FaceTracker creating IFTResult

本文关键字:创建 IFTResult Kinect FaceTracker MS      更新时间:2023-10-16

我有一个相当简单的应用程序,它包含以下内容:

context->mFaceTracker = FTCreateFaceTracker();
hr = context->mFaceTracker->Initialize( &mVideoCameraConfig, &mDepthCameraConfig, NULL, NULL );

它运行良好并返回S_ OK,并且mFaceTracker(据我所知)已正确初始化。然而,下一行是:

hr = context->mFaceTracker->CreateFTResult( &context->mFTResult );

它总是返回FT_ERROR_UNINITIALIZED,不分配指针,这让我很困惑。我尝试了许多不同的策略来实现这一点,从改变设备和检测器的线程工作方式,到将我的FTcontext对象从类更改为结构以匹配样本,所有这些都没有成功。Kinect SDK的示例都很好,但尝试在我自己的应用程序中使用它们似乎没有效果,尽管我仔细镜像了它们如何初始化设备和人脸跟踪器。我很好奇是否有其他人在初始化IFTFaceTracker或IFTResult时遇到过这种或类似的问题。此外,除了测试Initialize()返回的HRESULT之外,我还想知道如何测试IFTFaceTracker以获得正确的初始化。提前感谢

===编辑

我收到了一些关于更多代码的请求。它建立在Cinder上,并将此块用于Cinder:https://github.com/BanTheRewind/Cinder-KinectSdk

我不能发布所有的代码,但我已经在这里发布了至少大部分相关的Kinect初始化代码:

void Kinect::start( const DeviceOptions &deviceOptions ) 
    {
        if ( !mCapture ) {
            // Copy device options
            mDeviceOptions  = deviceOptions;
            string deviceId = mDeviceOptions.getDeviceId();
            int32_t index   = mDeviceOptions.getDeviceIndex();
            // Clamp device index
            if ( index >= 0 ) {
                index = math<int32_t>::clamp( index, 0, math<int32_t>::max( getDeviceCount() - 1, 0 ) );
            }
            // Initialize device instance
            long hr = S_OK;
            if ( index >= 0 ) {
                hr = NuiCreateSensorByIndex( index, &mSensor );
                if ( FAILED( hr ) ) {
                    trace( "Unable to create device instance " + toString( index ) + ": " );
                    error( hr );
                    return;
                }
            } else if ( deviceId.length() > 0 ) {
                _bstr_t id = deviceId.c_str();
                hr = NuiCreateSensorById( id, &mSensor );
                if ( FAILED( hr ) ) {
                    trace( "Unable to create device instance " + deviceId + ":" );
                    error( hr );
                    return;
                }
            } else {
                trace( "Invalid device name or index." );
                return;
            }
            // Check device
            hr = mSensor != 0 ? mSensor->NuiStatus() : E_NUI_NOTCONNECTED;
            if ( hr == E_NUI_NOTCONNECTED ) {
                error( hr );
                return;
            }
            // Get device name and index
            if ( mSensor != 0 ) {
                mDeviceOptions.setDeviceIndex( mSensor->NuiInstanceIndex() );
                BSTR id = ::SysAllocString( mSensor->NuiDeviceConnectionId() ); 
                _bstr_t idStr( id );
                if ( idStr.length() > 0 ) {
                    std::string str( idStr );
                    mDeviceOptions.setDeviceId( str );
                }
                ::SysFreeString( id );
            } else {
                index = -1;
                deviceId = "";
            }
            flags |= NUI_INITIALIZE_FLAG_USES_COLOR;
            }
            hr = mSensor->NuiInitialize( flags );
            if ( FAILED( hr ) ) {
                trace( "Unable to initialize device " + mDeviceOptions.getDeviceId() + ":" );
                error( hr );
                return;
            }
                hr = mSensor->NuiSkeletonTrackingEnable( 0, flags );
                if ( FAILED( hr ) ) {
                    trace( "Unable to initialize skeleton tracking for device " + mDeviceOptions.getDeviceId() + ": " );
                    error( hr );
                    return;
                }
            mIsSkeletonDevice = true;
            mThread = CreateThread(NULL, 0, &Kinect::StaticThread, (PVOID)this, 0, 0);
        }
    }
    DWORD WINAPI Kinect::StaticThread(PVOID lpParam)
    {
        Kinect* device = static_cast<Kinect*>(lpParam);
        if (device)
        {
            return device->run();
        }
        return 0;
    }
void run() {
if(mSensor) {
if(mEnabledFaceTracking)
                {
                    if(mNeedFaceTracker) {
                        mFaceTracker = new FaceTracker( 
                        mDeviceOptions.getVideoSize().x,
                        mDeviceOptions.getVideoSize().y,
                        mDeviceOptions.getDepthSize().x,
                        mDeviceOptions.getDepthSize().y,
                        1.0,
                        1 );
                        mNeedFaceTracker = false;
                    }

                    // make sure we have both color && depth buffers to work with
                    if(newDepth || newVideo)
                    {
                        FT_SENSOR_DATA sensorData(mFTColorImage, mFTDepthImage);
                        FT_VECTOR3D hint[2]; // this is initialized elsewhere
                        mFaceTracker->checkFaces( (NUI_SKELETON_FRAME*) &skeletonFrame, mFTColorImage, mFTDepthImage, 1.0, 0);
                        if(mFaceTracker->getNumFaces() > 0) {
                            cout << " we have a face " << mFaceTracker->getNumFaces() << endl;
                            mNewFaceTrackData = true;
                            mFaceData.clear();
                            for( int i = 0; i < mFaceTracker->getNumFaces(); i++) {
                                Face newFace;
                                mFaceTracker->getProjectedShape(0, newFace.scale, newFace.rotation, newFace.transform, newFace.screenPositions);
                                mFaceData.push_back(newFace);
                            }
                        }
                    }
                }
Sleep( 8 );
}
}

看起来你从来没有调用(或省略了包括在代码示例中的)NuiImageStreamOpen(),比如这个来自SingleFace示例的代码片段,Init方法中的KinectSensor.cpp:

hr = NuiImageStreamOpen(
    colorType,
    colorRes,
    0,
    2,
    m_hNextVideoFrameEvent,
    &m_pVideoStreamHandle );
if (FAILED(hr))
{
    return hr;
}
hr = NuiImageStreamOpen(
    depthType,
    depthRes,
    (bNearMode)? NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE : 0,
    2,
    m_hNextDepthFrameEvent,
    &m_pDepthStreamHandle );

在调用CreateFTResult()之前调用它们可能会修复未初始化的错误。

此外,您可以调用CreateThread(),然后调用run(),但没有while循环,因此线程几乎会立即退出,当然没有足够的时间让Kinect开始向FaceTracking提供数据。

看起来您没有包含线程或事件循环,该循环检查传感器是否有新数据,更新mFTColorImage和mFTDepthImage,并设置newDepth和newVideo标志。这可能与您在上面创建的线程相同(前提是您创建while循环,并忽略需要Kinect数据的性能或其他类),也可能与SingleFace Kinect SDK示例中的线程不同。