// Initialize reading data from kinect HRESULT KinectGrabber::Kinect_Init() { HRESULT hr; m_hNextVideoFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); m_hNextDepthFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); m_hNextSkeletonFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); m_hNextDepthPlayerFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR); if( FAILED( hr ) ) { printf("failed to inialize nui"); } hr = NuiSkeletonTrackingEnable( m_hNextSkeletonFrameEvent, 0 ); if( FAILED( hr ) ) { printf("failed to open skeleton tracking.");// MessageBoxResource(m_hWnd,IDS_ERROR_SKELETONTRACKING,MB_OK | MB_ICONHAND); return hr; } hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextVideoFrameEvent, &m_pVideoStreamHandle ); if( FAILED( hr ) ) { printf("failed to open NuiImagesStream"); return hr; } hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle ); if( FAILED( hr ) ) { printf("failed to open NuiImagesStream"); return hr; } //return hr; //audio init? //init other added parameters isSkeletonTracked=false; }
/** * @brief QKinect::init : initialisation de la Kinect */ void QKinect::init() { // Démarrage de la capture HRESULT res = NuiSkeletonTrackingEnable(NULL, 0); if(res != S_OK) throw std::logic_error("Cannot initialize Skeleton Tracking."); isInitialized = true; isRunning = true; }
// --- initialisation --- HRESULT Init(){ // Configure events to be listened on m_hNextDepthFrameEvent = CreateEvent( NULL, TRUE , FALSE, NULL ); m_hNextVideoFrameEvent = CreateEvent( NULL, TRUE , FALSE, NULL ); m_hNextSkeletonEvent = CreateEvent( NULL, TRUE , FALSE, NULL ); m_hEvNuiProcessStop = CreateEvent( NULL, FALSE, FALSE, NULL ); hEvents[0]=m_hEvNuiProcessStop; hEvents[1]=m_hNextDepthFrameEvent; hEvents[2]=m_hNextVideoFrameEvent; hEvents[3]=m_hNextSkeletonEvent; //printf("Kinect::Init() -- events created\n"); // initialisation HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR); if(FAILED(hr)) { MessageBox(NULL,L"Kinect initialisation failed.",L"Kinect Error",NULL); return hr; } //printf("Kinect::Init() -- done NuiInitialize\n"); hr = NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, 0 ); if(FAILED(hr)) return hr; //printf("Kinect::Init() -- done NuiSkeletonTrackingEnable\n"); hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextVideoFrameEvent, &m_pVideoStreamHandle ); if(FAILED(hr)) return hr; //printf("Kinect::Init() -- done NuiImageStreamOpen for video\n"); hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_320x240, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle ); if(FAILED(hr)) return hr; //printf("Kinect::Init() -- done NuiImageStreamOpen for depth\n"); return hr; }
int Read_Kinect::control_initialize() { int rtn = 0; //visualization_setup(); HRESULT hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON); if (hr != S_OK) { cout << "NuiInitialize failed" << endl; return hr; } h1 = CreateEvent(NULL, TRUE, FALSE, NULL); h2 = NULL; hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, h1, &h2); if (FAILED(hr)) { cout << "Could not open image stream video" << endl; return hr; } h3 = CreateEvent(NULL, TRUE, FALSE, NULL); h4 = NULL; hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_320x240, 0, 2, h3, &h4); if (FAILED(hr)) { cout << "Could not open depth stream video" << endl; return hr; } h5 = CreateEvent(NULL, TRUE, FALSE, NULL); hr = NuiSkeletonTrackingEnable(h5, 0); if (FAILED(hr)) { cout << "Could not open skeleton stream video" << endl; return hr; } return rtn; }
bool Kinect::init() { HRESULT hr; hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR); if(hr<0) return false; hr = NuiSkeletonTrackingEnable( _nextSkeletonEvent, 0 ); if(hr<0) return false; hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, _nextVideoFrameEvent, &_videoHandle ); if(hr<0) return false; hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_320x240, 0, 2, _nextDepthFrameEvent, &_depthHandle ); if(hr<0) return false; _events[0] = _nuiProcessStop; _events[1] = _nextDepthFrameEvent; _events[2] = _nextVideoFrameEvent; _events[3] = _nextSkeletonEvent; CreateThread(NULL,0,nuiProcessThread,this,0,NULL); _initialized = true; return true; }
Flyer::Flyer(PE::GameContext &context, PE::MemoryArena arena, PE::Handle hMyself, int &threadOwnershipMask) : Component(context, arena, hMyself) , m_prevCameraType(CameraManager::CameraType_Count) { // NUI testing #ifdef _XBOX // Could also try for a bit more smoothing ( 0.25f, 0.25f, 0.25f, 0.03f, 0.05f ); m_JointFilter.Init( 0.5f, 0.5f, 0.5f, 0.05f, 0.05f ); // create event which will be signaled when frame processing ends m_hFrameEndEvent = CreateEvent( NULL, FALSE, // auto-reset FALSE, // create unsignaled "NuiFrameEndEvent" ); if ( !m_hFrameEndEvent ) { ATG_PrintError( "Failed to create NuiFrameEndEvent\n" ); return; // return E_FAIL; } HRESULT hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX, NUI_INITIALIZE_DEFAULT_HARDWARE_THREAD ); if( FAILED( hr )) { ATG::NuiPrintError( hr, "NuiInitialize" ); return; // return E_FAIL; } // register frame end event with NUI hr = NuiSetFrameEndEvent( m_hFrameEndEvent, 0 ); if( FAILED(hr) ) { ATG::NuiPrintError( hr, "NuiSetFrameEndEvent" ); return; // return E_FAIL; } /* // Open the color stream hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 1, NULL, &m_hImage ); if( FAILED (hr) ) { ATG::NuiPrintError( hr, "NuiImageStreamOpen" ); return E_FAIL; } // Open the depth stream hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_320x240, 0, 1, NULL, &m_hDepth ); if( FAILED (hr) ) { ATG::NuiPrintError( hr, "NuiImageStreamOpen" ); return E_FAIL; } */ hr = NuiSkeletonTrackingEnable( NULL, 0 ); if( FAILED( hr )) { ATG::NuiPrintError( hr, "NuiSkeletonTrackingEnable" ); } m_pNuiJointConverterConstrained = new ATG::NuiJointConverter(); if( m_pNuiJointConverterConstrained == NULL ) { // return E_FAIL; return; } m_pNuiJointConverterConstrained->AddDefaultConstraints(); Handle hSN("SceneNode", sizeof(SceneNode)); m_pNuiSN = new(hSN) SceneNode(context, arena, hSN); m_pNuiSN->addDefaultComponents(); m_pNuiSN->m_base.setPos(Vector3(0.0f, 0, 25.0f)); m_pNuiSN->m_base.turnRight(1.2f * 3.1415f); RootSceneNode::Instance()->addComponent(hSN); for (int i = 0; i < XAVATAR_MAX_SKELETON_JOINTS; i++) { PE::Handle hMeshInstance("MeshInstance", sizeof(MeshInstance)); MeshInstance *pMeshInstance = new(hMeshInstance) MeshInstance(*m_pContext, m_arena, hMeshInstance); pMeshInstance->addDefaultComponents(); pMeshInstance->initFromFile("box.x_main_mesh.mesha", "Default", threadOwnershipMask); Handle hSN("SceneNode", sizeof(SceneNode)); m_sceneNodes[i] = new(hSN) SceneNode(context, arena, hSN); m_sceneNodes[i]->addDefaultComponents(); m_sceneNodes[i]->addComponent(hMeshInstance); m_pNuiSN->addComponent(hSN); if (m_pNuiJointConverterConstrained->MapAvatarJointToNUI_POSITION_INDEX(i) == NUI_SKELETON_POSITION_SHOULDER_RIGHT) { PE::Handle hMeshInstance("MeshInstance", sizeof(MeshInstance)); MeshInstance *pMeshInstance = new(hMeshInstance) MeshInstance(*m_pContext, m_arena, hMeshInstance); pMeshInstance->addDefaultComponents(); pMeshInstance->initFromFile("wings.x_rwing_mesh.mesha", "City", threadOwnershipMask); m_sceneNodes[i]->addComponent(hMeshInstance); } if (m_pNuiJointConverterConstrained->MapAvatarJointToNUI_POSITION_INDEX(i) == NUI_SKELETON_POSITION_SHOULDER_LEFT) { PE::Handle hMeshInstance("MeshInstance", sizeof(MeshInstance)); MeshInstance *pMeshInstance = new(hMeshInstance) MeshInstance(*m_pContext, m_arena, hMeshInstance); pMeshInstance->addDefaultComponents(); pMeshInstance->initFromFile("wings.x_lwing_mesh.mesha", "City", threadOwnershipMask); m_sceneNodes[i]->addComponent(hMeshInstance); } } { // put a camera here PE::Handle hMeshInstance("MeshInstance", sizeof(MeshInstance)); MeshInstance *pMeshInstance = new(hMeshInstance) MeshInstance(*m_pContext, m_arena, hMeshInstance); pMeshInstance->addDefaultComponents(); pMeshInstance->initFromFile("box.x_main_mesh.mesha", "Default", threadOwnershipMask); // we put camera in a scene node so we can rotate the camera within that scene node, but we could have also just added camera on its own Handle hSN("SceneNode", sizeof(SceneNode)); m_pCamSN = new(hSN) SceneNode(context, arena,hSN); m_pCamSN->addDefaultComponents(); m_pCamSN->addComponent(hMeshInstance); m_pNuiSN->addComponent(hSN); m_pCamSN->m_base.setPos(Vector3(0, +1.0f, +1.5f)); Handle hDebugCamera("Camera", sizeof(Camera)); Camera *debugCamera = new(hDebugCamera) Camera(context, arena, hDebugCamera, hSN); debugCamera->addDefaultComponents(); CameraManager::Instance()->setCamera(CameraManager::PLAYER, hDebugCamera); //SceneNode *pCamSN = debugCamera->getCamSceneNode(); } m_framesWithNoData = 0; m_framesWithData = 0; #endif // #ifdef _XBOX }
HRESULT KinectSensor::Init(NUI_IMAGE_TYPE depthType, NUI_IMAGE_RESOLUTION depthRes, BOOL bNearMode, BOOL bFallbackToDefault, NUI_IMAGE_TYPE colorType, NUI_IMAGE_RESOLUTION colorRes, BOOL bSeatedSkeletonMode) { HRESULT hr = E_UNEXPECTED; Release(); // Deal with double initializations. //do not support NUI_IMAGE_TYPE_COLOR_RAW_YUV for now if(colorType != NUI_IMAGE_TYPE_COLOR && colorType != NUI_IMAGE_TYPE_COLOR_YUV || depthType != NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX && depthType != NUI_IMAGE_TYPE_DEPTH) { return E_INVALIDARG; } m_VideoBuffer = FTCreateImage(); if (!m_VideoBuffer) { return E_OUTOFMEMORY; } DWORD width = 0; DWORD height = 0; NuiImageResolutionToSize(colorRes, width, height); hr = m_VideoBuffer->Allocate(width, height, FTIMAGEFORMAT_UINT8_B8G8R8X8); if (FAILED(hr)) { return hr; } m_DepthBuffer = FTCreateImage(); if (!m_DepthBuffer) { return E_OUTOFMEMORY; } NuiImageResolutionToSize(depthRes, width, height); hr = m_DepthBuffer->Allocate(width, height, FTIMAGEFORMAT_UINT16_D13P3); if (FAILED(hr)) { return hr; } m_FramesTotal = 0; m_SkeletonTotal = 0; for (int i = 0; i < NUI_SKELETON_COUNT; ++i) { m_HeadPoint[i] = m_NeckPoint[i] = FT_VECTOR3D(0, 0, 0); m_SkeletonTracked[i] = false; } m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hNextVideoFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hNextSkeletonEvent = CreateEvent(NULL, TRUE, FALSE, NULL); DWORD dwNuiInitDepthFlag = (depthType == NUI_IMAGE_TYPE_DEPTH)? NUI_INITIALIZE_FLAG_USES_DEPTH : NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX; hr = NuiInitialize(dwNuiInitDepthFlag | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR); if (FAILED(hr)) { return hr; } m_bNuiInitialized = true; DWORD dwSkeletonFlags = NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE; if (bSeatedSkeletonMode) { dwSkeletonFlags |= NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT; } hr = NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, dwSkeletonFlags ); if (FAILED(hr)) { return hr; } 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 ); if (FAILED(hr)) { if(bNearMode && bFallbackToDefault) { hr = NuiImageStreamOpen( depthType, depthRes, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle ); } if(FAILED(hr)) { return hr; } } // Start the Nui processing thread m_hEvNuiProcessStop=CreateEvent(NULL,TRUE,FALSE,NULL); m_hThNuiProcess=CreateThread(NULL,0,ProcessThread,this,0,NULL); return hr; }
HRESULT CSkeletalViewerApp::Nui_Init() { HRESULT hr; RECT rc; m_hNextDepthFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); m_hNextVideoFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); m_hNextSkeletonEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); GetWindowRect(GetDlgItem( m_hWnd, IDC_SKELETALVIEW ), &rc ); int width = rc.right - rc.left; int height = rc.bottom - rc.top; HDC hdc = GetDC(GetDlgItem( m_hWnd, IDC_SKELETALVIEW)); m_SkeletonBMP = CreateCompatibleBitmap( hdc, width, height ); m_SkeletonDC = CreateCompatibleDC( hdc ); ::ReleaseDC(GetDlgItem(m_hWnd,IDC_SKELETALVIEW), hdc ); m_SkeletonOldObj = SelectObject( m_SkeletonDC, m_SkeletonBMP ); hr = m_DrawDepth.CreateDevice( GetDlgItem( m_hWnd, IDC_DEPTHVIEWER ) ); if( FAILED( hr ) ) { MessageBoxResource( m_hWnd,IDS_ERROR_D3DCREATE,MB_OK | MB_ICONHAND); return hr; } hr = m_DrawDepth.SetVideoType( 320, 240, 320 * 4 ); if( FAILED( hr ) ) { MessageBoxResource( m_hWnd,IDS_ERROR_D3DVIDEOTYPE,MB_OK | MB_ICONHAND); return hr; } hr = m_DrawVideo.CreateDevice( GetDlgItem( m_hWnd, IDC_VIDEOVIEW ) ); if( FAILED( hr ) ) { MessageBoxResource( m_hWnd,IDS_ERROR_D3DCREATE,MB_OK | MB_ICONHAND); return hr; } hr = m_DrawVideo.SetVideoType( 640, 480, 640 * 4 ); if( FAILED( hr ) ) { MessageBoxResource( m_hWnd,IDS_ERROR_D3DVIDEOTYPE,MB_OK | MB_ICONHAND); return hr; } hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR); if( FAILED( hr ) ) { MessageBoxResource(m_hWnd,IDS_ERROR_NUIINIT,MB_OK | MB_ICONHAND); return hr; } hr = NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, 0 ); if( FAILED( hr ) ) { MessageBoxResource(m_hWnd,IDS_ERROR_SKELETONTRACKING,MB_OK | MB_ICONHAND); return hr; } hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextVideoFrameEvent, &m_pVideoStreamHandle ); if( FAILED( hr ) ) { MessageBoxResource(m_hWnd,IDS_ERROR_VIDEOSTREAM,MB_OK | MB_ICONHAND); return hr; } hr = NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_320x240, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle ); if( FAILED( hr ) ) { MessageBoxResource(m_hWnd,IDS_ERROR_DEPTHSTREAM,MB_OK | MB_ICONHAND); return hr; } // Start the Nui processing thread m_hEvNuiProcessStop=CreateEvent(NULL,FALSE,FALSE,NULL); m_hThNuiProcess=CreateThread(NULL,0,Nui_ProcessThread,this,0,NULL); //initialize background subtraction. initialdepth = (USHORT*)malloc(320*480*sizeof(USHORT)); FrameCount = 0; return hr; }