Kinect Interaction C ++性能问题 [英] Kinect Interaction C++ performance problem
问题描述
亲爱的所有人!
我对交互流有以下问题。它发生在主循环(轮询函数)减少时。我正在使用Kinect SDK 1.8和K4W。我的所有代码都编译成动态库(.dll),然后加载到第三方软件URBI(基于
并行机制软件平台的事件来控制机器人)。有关它的更多信息,请访问:
I have a following problem with interaction stream. It occurs when main loop (poll functions) decrease. I'm using Kinect SDK 1.8 with K4W. All my code is compiled to dynamic library (.dll) and it is loaded to the third party software URBI (event based with parallelism mechanism software platform to control robots). More about it you can find here:
http://lirec.ict.pwr.wroc.pl/~flash/?q=node/110
http://lirec.ict.pwr.wroc.pl/~flash/?q=node/110
代码的重要部分是:
投票深度
bool UKinect::pollDepth()
{
if (NULL != sensor)
{
NUI_IMAGE_FRAME depthFrame = {0};
// Attempt to get the depth frame
hr = sensor->NuiImageStreamGetNextFrame(depthStream, 0, &depthFrame);
if (FAILED(hr))
{
cerr <<"[UKinect] WARNING: Depth pool." << endl;
return false;
}
INuiFrameTexture * pTexture = depthFrame.pFrameTexture;
NUI_LOCKED_RECT LockedRect;
// Lock the frame data so the Kinect knows not to modify it while we're reading it
pTexture->LockRect(0, &LockedRect, NULL, 0);
// Make sure we've received valid data
if (LockedRect.Pitch != 0)
{
// process depth image if interaction function enabled
if (interaction){
INuiFrameTexture* pDepthImagePixelFrame;
BOOL nearMode = depthNearMode.as<bool>();
sensor->NuiImageFrameGetDepthImagePixelFrameTexture(depthStream, &depthFrame, &nearMode, &pDepthImagePixelFrame);
INuiFrameTexture *pdTexture = pDepthImagePixelFrame;
NUI_LOCKED_RECT dLockedRect;
// Lock the frame data so the Kinect knows not to modify it while we're reading it
pdTexture->LockRect(0, &dLockedRect, NULL, 0);
hr = interactionStream->ProcessDepth(dLockedRect.size,PBYTE(dLockedRect.pBits),depthFrame.liTimeStamp);
if( FAILED( hr ) )
{
cerr <<"[UKinect] ERROR: Process depth failed (for interaction purpose)." << endl;
return false;
}
// We're done with the texture so unlock it
pdTexture->UnlockRect(0);
}
// We're done with the texture so unlock it
pTexture->UnlockRect(0);
}
// Release the frame
sensor->NuiImageStreamReleaseFrame(depthStream, &depthFrame);
return true;
}
cerr <<"[UKinect] ERROR: Depth pool error." << endl;
return false;
}
投票骨架
bool UKinect::pollSkeleton()
{
if (NULL != sensor)
{
//
// Attempt to get the color frame
hr = sensor->NuiSkeletonGetNextFrame(0, &skeletonFrame);
if (FAILED(hr))
{
cerr <<"[UKinect] WARNING: Skeleton pool." << endl;
return false;
}
//
// process skeleton frame if interaction function enabled
if (interaction) {
Vector4 v;
sensor->NuiAccelerometerGetCurrentReading(&v);
hr = interactionStream->ProcessSkeleton(NUI_SKELETON_COUNT,
skeletonFrame.SkeletonData,
&v,
skeletonFrame.liTimeStamp);
if( FAILED( hr ) )
{
cerr <<"[UKinect] ERROR: Process skeleton failed (for interaction purpose)." << endl;
return false;
}
}
UpdateTrackedSkeletons();
return true;
}
cerr <<"[UKinect] ERROR: Skeleton pool error." << endl;
return false;
}
投票互动
bool UKinect::pollInteraction()
{
NUI_USER_INFO user;
NUI_INTERACTION_FRAME Interaction_Frame;
hr = interactionStream->GetNextFrame( 0, &Interaction_Frame );
if (FAILED(hr))
{
cerr << hex << hr <<endl;
cerr <<"[UKinect] WARNING: Interaction pool." << endl;
return false;
}
for (int i = 0 ; i < NUI_SKELETON_COUNT; ++i)
{
user = Interaction_Frame.UserInfos[i];
if (user.SkeletonTrackingId != 0) break;
}
NUI_HANDPOINTER_INFO handLeft = user.HandPointerInfos[0];
NUI_HANDPOINTER_INFO handRight = user.HandPointerInfos[1];
NUI_HANDPOINTER_STATE stateLeft = (NUI_HANDPOINTER_STATE)handLeft.State;
NUI_HANDPOINTER_STATE stateRight = (NUI_HANDPOINTER_STATE)handRight.State;
interID=user.SkeletonTrackingId;
interLeftState = stateLeft;
interRightState = stateRight;
if (stateLeft!= NUI_HANDPOINTER_STATE_NOT_TRACKED)
{
interLeftX = handLeft.X;
interLeftY = handLeft.Y;
interLeftRawX = handLeft.RawX;
interLeftRawY = handLeft.RawY;
interLeftRawZ = handLeft.RawZ;
interLeftPress = handLeft.PressExtent;
if (handLeft.HandEventType>0) interLeftEvent = handLeft.HandEventType;
}
if (stateLeft!= NUI_HANDPOINTER_STATE_NOT_TRACKED)
{
interRightX = handRight.X;
interRightY = handRight.Y;
interRightRawX = handRight.RawX;
interRightRawY = handRight.RawY;
interRightRawZ = handRight.RawZ;
interRightPress = handRight.PressExtent;
if (handRight.HandEventType>0) interRightEvent = handRight.HandEventType;
}
return true;
}
现在有一个轮询功能
And now there is a polling function
bool UKinect::Poll(bool pwait)
{
if (NULL == sensor) {
cerr <<"[UKinect] ERROR: Device not initialized." << endl;
return false;
}
DWORD mSec=0;
if (pwait) mSec=100;
if (color && ( WAIT_OBJECT_0 == WaitForSingleObject(colorNextFrame, mSec) ))
{
pollColor();
}
if (depth && (WAIT_OBJECT_0 == WaitForSingleObject(depthNextFrame, mSec)) )
{
pollDepth();
}
if (skeleton && (WAIT_OBJECT_0 == WaitForSingleObject(skeletonNextFrame, mSec)))
{
pollSkeleton();
}
if (interaction && (WAIT_OBJECT_0 == WaitForSingleObject(interactionNextFrame, 0)))
{
pollInteraction();
}
return true;
}
有什么问题?当我在URBI中使用(pwait = false)调用Poll函数时,这样:
What is the problem? When I call Poll function with (pwait=false) in URBI like this:
loop {
kinect.Poll(false);
},
互动流效果很好!即使我可以放一些延迟
interation stream works great! Even I can put some delay inside
loop {
kinect.Poll(false);
sleep(10ms);
},
它也可以正常工作,但不会超过15毫秒!
it also works fine, but not longer than 15ms!
但如果我调用Poll函数为true参数(等待新数据),交互流不起作用。
But if I call Poll function with true argument (wait for a new data), interation stream doesnt work.
loop {
kinect.Poll(true);
},
甚至
loop {
kinect.Poll(false);
sleep(30ms);
},
也不起作用。
推荐答案
如果您没有使用消息泵而是使用紧密循环,则必须在其中休眠以便为操作系统调度程序运行其他线程留出时间。 Windows不是实时操作系统。
If you are not using a message pump but a tight loop, you must have a sleep in there to yield time for the operating system scheduler to run other threads. Windows is not a real-time operating system.
这篇关于Kinect Interaction C ++性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!