void DepthSensor::createInstance() { int count = 0; NuiGetSensorCount(&count); if (count == 0) { throw std::runtime_error("no valid Kinect is connected"); } // creat kinect NuiCreateSensorByIndex(0, &mNuiSensor); // get kinect status HRESULT status = mNuiSensor->NuiStatus(); if (status != S_OK) { throw std::runtime_error("Kinect is not ready to work"); } // Create and initialize a new vtk image renderer // We'll use this to draw the data we receive from the Kinect to the screen mDrawDepth = new vtkImageRender(); if (!mDrawDepth->Initialize(cDepthWidth, cDepthHeight, cDepthWidth * sizeof(long))) { throw std::runtime_error("Failed to initialize the vtk draw device."); } }
/// <summary> /// Create the first connected Kinect found /// </summary> /// <returns>indicates success or failure</returns> HRESULT CSkeletonBasics::CreateFirstConnected() { INuiSensor * pNuiSensor; int iSensorCount = 0; HRESULT hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL != m_pNuiSensor) { // Initialize the Kinect and specify that we'll be using skeleton hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_SKELETON); if (SUCCEEDED(hr)) { // Create an event that will be signaled when skeleton data is available m_hNextSkeletonEvent = CreateEventW(NULL, TRUE, FALSE, NULL); // Open a skeleton stream to receive skeleton data hr = m_pNuiSensor->NuiSkeletonTrackingEnable(m_hNextSkeletonEvent, 0); } } if (NULL == m_pNuiSensor || FAILED(hr)) { SetStatusMessage(L"No ready Kinect found!"); return E_FAIL; } return hr; }
Kinect1::Kinect1() : KinectInterface(1280, 960, 640, 480), sensor(NULL), colorBuffer(new UINT32[colorWidth * colorHeight]), depthBuffer(new UINT16[depthWidth * depthHeight]) { // Initialize both arrays to 0 for (int i = 0; i < colorWidth * colorHeight; i++) { colorBuffer[i] = 0; } for (int i = 0; i < depthWidth * depthHeight; i++) { depthBuffer[i] = 0; } int numSensors, result; // Make sure sensor exists result = NuiGetSensorCount(&numSensors); if (result >= 0 && numSensors > 0) { result = NuiCreateSensorByIndex(0, &sensor); if (result >= 0) { // Initialize sensor with both color and depth sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH); // Open color stream sensor->NuiImageStreamOpen( // 1280 x 960 color NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_1280x960, // Do not use near mode 0, // Buffer for 2 frames 2, // No handler for events NULL, // Get the color stream &colorStream); // Open depth stream sensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, // Change this argument to NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE for near mode 0, 2, NULL, &depthStream); } } }
bool initializeKinect() { int numKinects = 0; HRESULT hr = NuiGetSensorCount( &numKinects ); if ( FAILED(hr) || numKinects<=0 ) { std::cout << "No Kinect device found." << std::endl; return false; } hr = NuiCreateSensorByIndex( 0, &context ); if ( FAILED(hr) ) { std::cout << "Failed to connect to Kinect device." << std::endl; return false; } DWORD nuiFlags = NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX; hr = context->NuiInitialize( nuiFlags ); if ( FAILED(hr) ) { std::cout << "Failed to intialize Kinect: " << std::hex << (long)hr << std::dec << std::endl; return false; } hr = context->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, NULL, &colorStreamHandle ); if ( FAILED(hr) ) { std::cout << "Unable to create color stream: " << std::hex << (long)hr << std::dec << std::endl; return false; } hr = context->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_640x480, 0, 2, NULL, &depthStreamHandle ); if ( FAILED(hr) ) { std::cout << "Unable to create depth stream: " << std::hex << (long)hr << std::dec << std::endl; return false; } hr = context->NuiSkeletonTrackingEnable( NULL, 0 ); if ( FAILED(hr) ) { std::cout << "Unable to start tracking skeleton." << std::endl; return false; } return true; }
HRESULT KinectManager::initialize() { // reset vars // kinectInitialized = false; kinectFailed = false; // create vars // int numSensors; //get number of sensors HRESULT hr = NuiGetSensorCount(&numSensors); if (SUCCEEDED(hr)) { if (numSensors == 0) { //NO KINECTS CONNECTED hr = MAKE_HRESULT(0, 0, -1); kinectFailed = true; cout << "No Kinect sensors found\n"; } else { cout << "Found sensor...\n"; //get the first kinect and its status NuiCreateSensorByIndex(0, &pKinect); hr = pKinect->NuiStatus(); } } if (SUCCEEDED(hr)) { //kinect ready cout << "Kinect is ready, starting now\n"; kinectInitialized = true; startKinect(); } else if (hr != S_NUI_INITIALIZING) { kinectFailed = true; cout << "Kinect failed and is not initializing\n"; } return hr; }
virtual bool start( uint32_t startGenerators, uint32_t configureImages ) { if( m_IsInitialized ) return false; m_GeneratorConfig = startGenerators; m_ImageConfig = configureImages; //initialization ---------------------------------------------------------> _2REAL_LOG(info) << "_2Real: Initializing Microsoft Kinect SDK" << std::endl; HRESULT status = 0; //get number of devices int deviceCount = 0; if( FAILED( status = NuiGetSensorCount( &deviceCount ) ) ) { throwError( "_2Real: Error when trying to enumerate devices\n" ); } //abort if no devices found if( ( m_NumDevices = deviceCount ) == 0 ) { _2REAL_LOG(error) << "_2Real: No devices found" << std::endl; return false; } _2REAL_LOG(info) << "_2Real: detected number of sensors: " << deviceCount << std::endl; std::stringstream ss; //initialization putting device instances to m_devices vector INuiSensor* devicePtr; for( int i = 0; i < deviceCount; ++i ) { //creating instance if( FAILED( status = NuiCreateSensorByIndex( i, &devicePtr ) ) ) { throwError(("_2Real: Error when trying to create device: " + i)); } //saving to vector ss.str( "" ); ss << "kinect_device_" << i; m_Devices.push_back( new WSDKDevice( devicePtr, startGenerators, configureImages, ss.str().c_str() ) ); } m_IsInitialized = 1; _2REAL_LOG(info) << "_2Real: Initialization: OK" << std::endl; return true; }
HRESULT KinectImpl::initDevice() { INuiSensor * pNuiSensor=NULL; int iSensorCount = 0; HRESULT hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL != m_pNuiSensor) { hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_AUDIO | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR ); } return hr; }
INuiSensor* GetNuiPointer() { int sensorCount = 0; INuiSensor *nuiSensor = NULL; HRESULT hr = NuiGetSensorCount(&sensorCount); if (FAILED(hr)) { throw "Failed to get NUI sensor count"; } // Look at each Kinect sensor for (int i=0; i<sensorCount; i++) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &nuiSensor); if (FAILED(hr)) continue; // Get the status of the sensor, and if connected, then we can initialize it hr = nuiSensor->NuiStatus(); if (S_OK == hr) break; // This sensor wasn't OK, so release it since we're not using it nuiSensor->Release(); } // If we couldn't get an instance, return failure if (NULL == nuiSensor) { throw "Failed to get NUI Sensor instance"; } // Initialize the Kinect and specify that we'll be using depth hr = nuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX); if (FAILED(hr)) return NULL; else return nuiSensor; }
HRESULT KinectManager::initialize() { INuiSensor * nui; int nuiCount = 0; HRESULT hr; NuiSetDeviceStatusCallback(OnSensorStatusChanged, NULL); hr = NuiGetSensorCount(&nuiCount); if ( FAILED(hr)) { return hr; } // Look at each kinect sensor for (int i = 0; i < nuiCount; i++) { // Create the sensor so we can check status, if we can't create it, move on. hr = NuiCreateSensorByIndex(i, &nui); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it. hr = nui->NuiStatus(); if (S_OK == hr) { nuiList.push_front(nui); } // This sensor was not okay, so we release it (into the wild!) since we're not using it. nui->Release(); } return hr; }
/// <summary> /// Enumerate and construct all the sensors when the app starts up /// </summary> void CMainWindow::EnumerateSensors() { int iCount = 0; HRESULT hr = NuiGetSensorCount(&iCount); if (FAILED(hr)) { return; } for (int i = 0; i < iCount; ++i) { INuiSensor* pNuiSensor = nullptr; if (SUCCEEDED(NuiCreateSensorByIndex(i, &pNuiSensor))) { UpdateMainWindow(pNuiSensor->NuiDeviceConnectionId(), pNuiSensor->NuiStatus()); } SafeRelease(pNuiSensor); } }
/// <summary> /// Funcion que crea la primera conexion de la aplicacion con Kinect. Comprueba e inicializa los sensores. /// </summary> /// <returns>Devuelve un HRESULT con las banderas de los flags inicializados y los que no.</returns> HRESULT CreateFirstConnected() { INuiSensor * pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } // Initialize the Kinect and specify that we'll be using depth DWORD dwFlags = 0; if (m_bProcessColor) dwFlags |= NUI_INITIALIZE_FLAG_USES_COLOR; if (m_bProcessDepth) m_bUserDetection ? (dwFlags |= NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX) : (dwFlags |= NUI_INITIALIZE_FLAG_USES_DEPTH); if (m_bProcessSkeleton) dwFlags |= NUI_INITIALIZE_FLAG_USES_SKELETON; hr = m_pNuiSensor->NuiInitialize(dwFlags); return hr; }
// called once on initialization void SensorManager::CreateListOfAvailableSensors() { // gets the count of sensors from Kinect for Windows API int iCount = 0; HRESULT hr = NuiGetSensorCount( &iCount ); if( FAILED(hr) ) { return; } // if there are no sensors, create a temporary one if( 0 == iCount ) { // when the first sensor is connected, it will be added // with the correct id if( m_kinectSensors.size() == 0 ) { AddSensorToList( m_wcTempPortID ); } } // continue with the known sensors to the list for (int i = 0; i < iCount; ++i) { INuiSensor* pNui = nullptr; ComSmartPtr<INuiSensor> pNuiPtr; if( SUCCEEDED( NuiCreateSensorByIndex(i, &pNui) ) ) { pNuiPtr.Attach(pNui); // Setting up smart pointer for sensor. const WCHAR* wcPortID = pNuiPtr->NuiDeviceConnectionId(); // contains the connections string for the device // plugged into a specific USB port. // removing the sensor and using a different port will have // a different ID. AddSensorToList( wcPortID ); } } }
bool initKinect() { // Get a working kinect sensor int numSensors; if (NuiGetSensorCount(&numSensors) < 0 || numSensors < 1) return false; if (NuiCreateSensorByIndex(0, &sensor) < 0) return false; // Initialize sensor sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR); sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, // Depth camera or rgb camera? NUI_IMAGE_RESOLUTION_640x480, // Image resolution 0, // Image stream flags, e.g. near mode 2, // Number of frames to buffer NULL, // Event handle &depthStream); sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, // Depth camera or rgb camera? NUI_IMAGE_RESOLUTION_640x480, // Image resolution 0, // Image stream flags, e.g. near mode 2, // Number of frames to buffer NULL, // Event handle &rgbStream); return sensor; }
bool initKinect() { // Get a working kinect sensor int numSensors; if (NuiGetSensorCount(&numSensors) < 0 || numSensors < 1) return false; if (NuiCreateSensorByIndex(0, &sensor) < 0) return false; // Initialize sensor sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON); sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, // Depth camera or rgb camera? NUI_IMAGE_RESOLUTION_640x480, // Image resolution 0, // Image stream flags, e.g. near mode 2, // Number of frames to buffer NULL, // Event handle &depthStream); sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, // Depth camera or rgb camera? NUI_IMAGE_RESOLUTION_640x480, // Image resolution 0, // Image stream flags, e.g. near mode 2, // Number of frames to buffer NULL, // Event handle &rgbStream); sensor->NuiSkeletonTrackingEnable(NULL, 0); // NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT for only upper body return sensor; }
bool KinectInput::initSensor() { INuiSensor* tempSensor; int numSensors = 0; if (NuiGetSensorCount(&numSensors) != S_OK ) { std::cout << "Kinect Error: Sensor lookup failed, Probably no sensors connected" << std::endl; std::getchar(); return false; } // Loop through all sensors for (int i = 0; i < numSensors; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next if (NuiCreateSensorByIndex(i, &tempSensor) < 0) { continue; } // Get the status of the sensor, and if connected, then we can initialize it if (tempSensor->NuiStatus() == S_OK) { kinectSensor = tempSensor; break; } // This sensor wasn't OK, so release it since we're not using it tempSensor->Release(); } if (kinectSensor != NULL) { // Initialize the Kinect and specify that we'll be using skeleton if (kinectSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_SKELETON) >= 0) { // Create an event that will be signaled when skeleton data is available nextSkeletonUpdate = CreateEventW(NULL, TRUE, FALSE, NULL); // Open a skeleton stream to receive skeleton data if (kinectSensor->NuiSkeletonTrackingEnable(nextSkeletonUpdate, 0) < 0) { std::cout << "Kinect Error: Couldn't open a skeleton stream" << std::endl; std::getchar(); return false; } } else { std::cout << "Kinect Error: Sensor failed initialization" << std::endl; std::getchar(); return false; } } else { std::cout << "Kinect Error: No sensor could be initialized" << std::endl; std::getchar(); return false; } return true; }
size_t KinectHelperImplMSW::GetDeviceCount() { int result(0); if(FAILED(NuiGetSensorCount(&result))) return 0; return (size_t)result; }
/*! //\par Description: // Sets all memeber to their default values, // reads configuration from settings file and tries to find first connected kinect device. // //\par Return value: // True value is a expected value. When this method returns false then something wrong is with // with kinect sensor. It can be disconnected or drivers are incompatible. First thing to do // in this case is to check green led on kinect sensor. It should shines all the time. Flashing // is not allowed. // //\retval true kinect is ready to work //\retval false kinect is not found */ bool CKinectHandler::createFirstConnectedKinectSensor() { qDebug() << "createFirstConnectedKinectSensor()"; INuiSensor * pSensor; int iSensorCount = 0; // HRESULT - just standard long HRESULT hr = NuiGetSensorCount( & iSensorCount ); if ( FAILED( hr ) ) // checks if hr is less than 0 { qDebug() << "Cannot get available kinect sensor count"; return false; } // checks all sensors and takes first one which can be created for ( int i = 0 ; i < iSensorCount ; i++ ) { hr = NuiCreateSensorByIndex( i, & pSensor ); if ( FAILED( hr ) ) { qDebug() << "Cannot create sensor with index: " << i << ", check next index"; continue; } hr = pSensor->NuiStatus(); if ( hr == S_OK ) // sensor is connected, it can be initialized now { m_pSensor = pSensor; qDebug() << "Sensor with index: " << i << " is connected - breaking loop"; break; } // release not ok sensor pSensor->Release(); } if ( m_pSensor != NULL ) { hr = m_pSensor->NuiInitialize( NUI_INITIALIZE_FLAG_USES_SKELETON ); // in order to use skeleton if ( SUCCEEDED( hr ) ) { // create event to inform application about new skeleton frame m_hNextSkeletonEvent = CreateEventW( NULL, TRUE, FALSE, NULL ); // open skeleton stream to get skeleton data hr = m_pSensor->NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, 0 ); } } if ( m_pSensor == NULL || FAILED( hr ) ) { qDebug() << "No ready kinect found"; m_kinectDetails.insert( "connection", false ); return false; } m_kinectDetails.insert( "connection", true ); m_kinectDetails.insert( "skeleton_found", false ); return true; }
// In current version, we can't set the resolution // All sensors of Kinect is activated. HRESULT Kinect::createFirstConnected(){ INuiSensor * pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL != m_pNuiSensor) { // Initialize the Kinect and specify that we'll be using color, depth and skeletion hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON); if (SUCCEEDED(hr)) { // Create an event that will be signaled when depth data is available m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_640x480, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE|NUI_IMAGE_STREAM_FLAG_DISTINCT_OVERFLOW_DEPTH_VALUES, //NUI_IMAGE_STREAM_FRAME_LIMIT_MAXIMUM, 2, m_hNextDepthFrameEvent, &m_hDepthStreamHandle); //m_pNuiSensor->NuiImageStreamSetImageFrameFlags(&m_hDepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE|NUI_IMAGE_STREAM_FLAG_DISTINCT_OVERFLOW_DEPTH_VALUES); } if (SUCCEEDED(hr)) { // Open a color image stream to receive depth frames m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextColorFrameEvent, &m_hColorStreamHandle); } if (SUCCEEDED(hr)){ // Enable the skeleton tracking m_hNextSkeletonEvent = CreateEventW(NULL, TRUE, FALSE, NULL); //hr = m_pNuiSensor->NuiSkeletonTrackingEnable(m_hNextSkeletonEvent, 0); hr = m_pNuiSensor->NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE | NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT); } } if (NULL == m_pNuiSensor || FAILED(hr)) { return E_FAIL; } m_bInitialized = true; return hr; }
bool KinectCapture::InitCapture() { INuiSensor * pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { std::cout << "No ready Kinect found!" << std::endl; return false; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_NuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (nullptr != m_NuiSensor) { // Initialize the Kinect and specify that we'll be using depth hr = m_NuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR); if (SUCCEEDED(hr)) { // Open a depth image stream to receive depth frames hr = m_NuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, m_depthImageResolution, 0, 2, NULL, &m_DepthStreamHandle); if (SUCCEEDED(hr)) { // Open a color image stream to receive color frames hr = m_NuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, m_colorImageResolution, 0, 2, NULL, &m_ColorStreamHandle); } if (FAILED(hr)) { std::cout << "Couldn't open color stream" << std::endl; } } } if (m_NearMode) { // Set near mode based on our internal state HRESULT nearHr = m_NuiSensor->NuiImageStreamSetImageFrameFlags(m_DepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE); } if (nullptr == m_NuiSensor || FAILED(hr)) { std::cout << "No ready Kinect found!" << std::endl; return false; } return (S_OK == hr); }
HRESULT KinectSensor::createFirstConnected() { INuiSensor* pNuiSensor = NULL; HRESULT hr = S_OK; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr) ) { return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL == m_pNuiSensor) { return E_FAIL; } // Initialize the Kinect and specify that we'll be using depth //hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX); hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH); if (FAILED(hr) ) { return hr; } // Create an event that will be signaled when depth data is available m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, //NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, cDepthResolution, (8000 << NUI_IMAGE_PLAYER_INDEX_SHIFT), 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle); if (FAILED(hr) ) { return hr; } // Create an event that will be signaled when color data is available m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a color image stream to receive color frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, cColorResolution, 0, 2, m_hNextColorFrameEvent, &m_pColorStreamHandle ); if (FAILED(hr) ) { return hr; } INuiColorCameraSettings* colorCameraSettings; HRESULT hrFlag = m_pNuiSensor->NuiGetColorCameraSettings(&colorCameraSettings); if (hr != E_NUI_HARDWARE_FEATURE_UNAVAILABLE) { m_kinect4Windows = true; } //TODO MATTHIAS: does this function have to be called every frame? USHORT* test = new USHORT[getDepthWidth()*getDepthHeight()]; // Get offset x, y coordinates for color in depth space // This will allow us to later compensate for the differences in location, angle, etc between the depth and color cameras m_pNuiSensor->NuiImageGetColorPixelCoordinateFrameFromDepthPixelFrameAtResolution( cColorResolution, cDepthResolution, getDepthWidth()*getDepthHeight(), test, getDepthWidth()*getDepthHeight()*2, m_colorCoordinates ); SAFE_DELETE_ARRAY(test); // Start with near mode on (if possible) m_bNearMode = false; if (m_kinect4Windows) { toggleNearMode(); } //toggleAutoWhiteBalance(); return hr; }
KinectImageSource::KinectImageSource(int device) : ImageSource(lexical_cast<string>(device)), frame() { #ifdef WITH_MSKINECT_SDK m_pColorStreamHandle = INVALID_HANDLE_VALUE; /// Create the first connected Kinect found INuiSensor * pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { std::cout << "Error getting sensor count. No Kinect plugged in?" << hr << std::endl; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL != m_pNuiSensor) { // Initialize the Kinect and specify that we'll be using color hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR); if (SUCCEEDED(hr)) { // Create an event that will be signaled when color data is available //m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a color image stream to receive color frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, NULL, &m_pColorStreamHandle); } } if (NULL == m_pNuiSensor || FAILED(hr)) { std::cout << "No ready Kinect found!" << std::endl; } //std::cout << "hr: " << hr << std::endl; #else std::cerr << "Error! This is the Microsoft Kinect SDK interface and not available under Linux." << std::endl; #endif }
/// <summary> /// Create the first connected Kinect found. /// </summary> /// <returns>S_OK on success, otherwise failure code.</returns> HRESULT KinectReader::CreateFirstConnected() { INuiSensor * pNuiSensor = NULL; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL != m_pNuiSensor) { // Initialize the Kinect and specify that we'll be using audio signal // Initialize the Kinect and specify that we'll be using depth hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_AUDIO | NUI_INITIALIZE_FLAG_USES_DEPTH); if (SUCCEEDED(hr)) { // Create an event that will be signaled when depth data is available m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle); } } if (NULL == m_pNuiSensor || FAILED(hr)) { std::cout<<"No ready Kinect found!"<<std::endl; m_pNuiSensor = NULL; //SetStatusMessage(L"No ready Kinect found!"); return E_FAIL; } printf("Kinect found. \n"); /* Speech start */ hr = InitializeAudioStream(); if (FAILED(hr)) { printf("Could not initialize audio stream.\n"); return hr; } printf("Initialize Audio Stream. \n"); hr = CreateSpeechRecognizer(); if (FAILED(hr)) { printf("Could not create speech recognizer. Please ensure that Microsoft Speech SDK and other sample requirements are installed.\n"); return hr; } printf("Create Speech Recognizer. \n"); hr = LoadSpeechGrammar(); if (FAILED(hr)) { printf("Could not load speech grammar. Please ensure that grammar configuration file was properly deployed.\n"); return hr; } printf("Start Speech Recognition. \n"); hr = StartSpeechRecognition(); if (FAILED(hr)) { printf("Could not start recognizing speech. \n"); return hr; } /* Speech end */ return hr; }
int Init() { //Make sure our image types are the same as the OpenNI image types. //assert(sizeof(XnRGB24Pixel) == sizeof(ColorPixel)); //assert(sizeof(XnDepthPixel) == sizeof(DepthPixel)); //assert(sizeof(XnStatus) == sizeof(int)); INuiSensor* nuiSensorPtr; int ret; // Get number of sensors int sensorCount = 0; ret = NuiGetSensorCount(&sensorCount); if(ret < 0) { return -1; } // Connect to first sensor for(int i=0; i<sensorCount; i++) { // Create sensor so we can check the status ret = NuiCreateSensorByIndex(i, &nuiSensorPtr); if(ret < 0) continue; // Get the status of the sensor ret = nuiSensorPtr->NuiStatus(); if(ret < 0) { (void)nuiSensorPtr->Release(); nuiSensorPtr = NULL; continue; } sensor = nuiSensorPtr; } // Make sure we have a sensor if(sensor == NULL) { return -1; } // Initialize Kinect ret = sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH); if(ret < 0) { return -1; } // Create an event that will be signaled when color data is available colorImageReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Create an event that will be signaled when depth data is available depthImageReadyEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a color image stream to receive color frames ret = sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, colorImageReadyEvent, &colorStream); if(ret < 0) { return -1; } // Open a depth image stream to receive depth frames ret = sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0, 2, depthImageReadyEvent, &depthStream); if(ret < 0) { return -1; } return 0; }
// TODO: allow for customizing which streams are initialized HRESULT SimpleKinect::Initialize() { INuiSensor * pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } // Find the correct sensor cout << "SimpleKinect: Finding sensor..." << endl; for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } // Open depth stream if (NULL != m_pNuiSensor) { cout << "SimpleKinect: Opening depth stream.." << endl; // Initialize the Kinect and specify that we'll be using depth, player index and color hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON ); if (SUCCEEDED(hr)) { // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_640x480, 0, 2, NULL, &m_pDepthStreamHandle); } else { cerr << "SimpleKinect: Failed to open depth and player stream!" << endl; return E_FAIL; } // open color stream cout << "SimpleKinect: Opening color stream.." << endl; hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, NULL, &m_pColorStreamHandle); if(!SUCCEEDED(hr)) { cerr << "SimpleKinect: Failed to open color stream!" << endl; return E_FAIL; } cout << "SimpleKinect: Opening skeleton stream.." << endl; // open skeleton stream if(HasSkeletalEngine(m_pNuiSensor)) { hr = m_pNuiSensor->NuiSkeletonTrackingEnable(NULL, NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE | NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT | NUI_SKELETON_TRACKING_FLAG_SUPPRESS_NO_FRAME_DATA); if(!SUCCEEDED(hr)) { cerr << "SimpleKinect: Failed to open skeleton stream!" << endl; return E_FAIL; } } else { cerr << "SimpleKinect: Sensor does not have skeletal engine!" << endl; } } if (NULL == m_pNuiSensor || FAILED(hr)) { cerr << "SimpleKinect: No ready Kinect found!" << endl; return E_FAIL; } else { BSTR uniqueId = m_pNuiSensor->NuiUniqueId(); cout << "SimpleKinect Connected to sensor " << uniqueId << endl; } m_isInitialized = true; return hr; }
int main(void) { //Set the error callback glfwSetErrorCallback(error_callback); //Initialize GLFW if (!glfwInit()) { exit(EXIT_FAILURE); } //Set the GLFW window creation hints - these are optional //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Request a specific OpenGL version //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Request a specific OpenGL version //glfwWindowHint(GLFW_SAMPLES, 4); //Request 4x antialiasing //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Create a window and create its OpenGL context window = glfwCreateWindow(960, 720, "Test Window", NULL, NULL); //If the window couldn't be created if (!window) { fprintf(stderr, "Failed to open GLFW window.\n"); glfwTerminate(); //exit(EXIT_FAILURE); } //This function makes the context of the specified window current on the calling thread. glfwMakeContextCurrent(window); //Sets the key callback glfwSetKeyCallback(window, key_callback); //Initialize GLEW GLenum err = glewInit(); //If GLEW hasn't initialized if (err != GLEW_OK) { fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); return -1; } //Set a background color glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glfwSetCursorPos(window, 1024 / 2, 768 / 2); GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); // Create and compile our GLSL program from the shaders GLuint red = LoadShaders("SimpleTransform.vertexshader", "SingleColorRed.fragmentshader"); GLuint grid = LoadShaders("SimpleTransform.vertexshader", "SingleColorGrid.fragmentshader"); glBindFragDataLocation(red, 0, "red"); glBindFragDataLocation(grid, 1, "grid"); // Get a handle for our "MVP" uniform GLuint MatrixID = glGetUniformLocation(red, "MVP"); // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 1000.0f); // Or, for an ortho camera : //glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates // Camera matrix glm::mat4 View = glm::lookAt( glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space glm::vec3(0, 0, 0), // and looks at the origin glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down) ); static const GLfloat g_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, }; static const GLushort g_element_buffer_data[] = { 0, 1, 2 }; GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); static const GLfloat g_triangle_buffer_data[] = { -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, }; GLuint triangle; glGenBuffers(1, &triangle); glBindBuffer(GL_ARRAY_BUFFER, triangle); glBufferData(GL_ARRAY_BUFFER, sizeof(g_triangle_buffer_data), g_triangle_buffer_data, GL_STATIC_DRAW); // Enable depth test glEnable(GL_DEPTH_TEST); // Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_SMOOTH);//OPENGL INSTANTIATION HRESULT hr; NUI_IMAGE_FRAME depthFrame; HANDLE hDepth; INuiSensor* pNuiSensor = NULL; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) return hr; for (int i = 0; i < iSensorCount; i++) { INuiSensor* tempSensor; hr = NuiCreateSensorByIndex(i, &tempSensor); if (FAILED(hr)) continue; hr = tempSensor->NuiStatus(); if (S_OK == hr) { pNuiSensor = tempSensor; break; } tempSensor->Release(); } for (int i = 0; i < 2048; i++) { depthLookUp[i] = rawDepthToMeters(i); } rotation = getRotationMatrix(theta, psi, fi); pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH); pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_320x240, 0, 2, NULL, &hDepth);//KINECT INSTANTIATION cout << "Starting Main Loop"; static double lastTime = glfwGetTime(); //Main Loop do { double currentTime = glfwGetTime(); float deltaTime = float(currentTime - lastTime); //Clear color buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(grid); modelMatrix(MatrixID); hr = pNuiSensor->NuiImageStreamGetNextFrame(hDepth, 0, &depthFrame); if (!FAILED(hr)) { INuiFrameTexture* pTexture; NUI_LOCKED_RECT LockedRect; hr = pNuiSensor->NuiImageFrameGetDepthImagePixelFrameTexture( hDepth, &depthFrame, false, &pTexture); if (FAILED(hr)) { pNuiSensor->NuiImageStreamReleaseFrame(hDepth, &depthFrame); continue; } pTexture->LockRect(0, &LockedRect, NULL, 0);//Kinect Image Grab int skipX = 1; int skipY = 1; float scalar = 4.0f; if (LockedRect.Pitch != 0) { for (int x = 0; x < width; x += skipX) { for (int y = 0; y < height; y += skipY) { const NUI_DEPTH_IMAGE_PIXEL * pBufferRun = reinterpret_cast<const NUI_DEPTH_IMAGE_PIXEL *>(LockedRect.pBits) + x + y * width; //float depth = (float)(pBufferRun->depth); //glm::vec3 location = realWorld(depth, height - y, x, 500.0f, 1000.0f); //createCube(0.006f, location); Vector4 locationDepth = NuiTransformDepthImageToSkeleton(x, y, (short)(pBufferRun->depth << 3)); glm::vec3 locationDepthxyz = glm::vec3(locationDepth.x * scalar, locationDepth.y * scalar, locationDepth.z * scalar); createCube(0.009f, locationDepthxyz); } } } pTexture->UnlockRect(0); pTexture->Release(); pNuiSensor->NuiImageStreamReleaseFrame(hDepth, &depthFrame); } createGrid(); //Test drawings /* glUseProgram(red); modelMatrix(MatrixID); //createCube(0.05f, glm::vec3(1.0f,1.0f,1.0f)); // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); //createObject(vertexbuffer, GL_TRIANGLES, 3); //createObject(triangle, GL_TRIANGLES, 3); glDisableVertexAttribArray(0); */ //Swap buffers glfwSwapBuffers(window); //Get and organize events, like keyboard and mouse input, window resizing, etc... glfwPollEvents(); std::string title = "Title | DELTA TIME " + std::to_string(1.0f/deltaTime); const char* pszConstString = title.c_str(); glfwSetWindowTitle(window, pszConstString); lastTime = currentTime; } //Check if the ESC key had been pressed or if the window had been closed while (!glfwWindowShouldClose(window)); //Close OpenGL window and terminate GLFW glfwDestroyWindow(window); //Finalize and clean up GLFW glfwTerminate(); exit(EXIT_SUCCESS); }
/// <summary> /// Create the first connected Kinect found /// </summary> /// <returns>indicates success or failure</returns> HRESULT CKinectFusion::CreateFirstConnected() { INuiSensor * pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { std::cout << "No ready Kinect found!" << std::endl; return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (nullptr != m_pNuiSensor) { // Initialize the Kinect and specify that we'll be using depth hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH); if (SUCCEEDED(hr)) { // Create an event that will be signaled when depth data is available m_hNextDepthFrameEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr); // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, m_imageResolution, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle); } if (m_bNearMode) { // Set near mode based on our internal state HRESULT nearHr = m_pNuiSensor->NuiImageStreamSetImageFrameFlags(m_pDepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE); } } if (nullptr == m_pNuiSensor || FAILED(hr)) { std::cout << "No ready Kinect found!" << std::endl; return E_FAIL; } return hr; }
/// <summary> /// Create the first connected Kinect found /// </summary> /// <returns>S_OK on success, otherwise failure code</returns> HRESULT KinectEasyGrabber::CreateFirstConnected() { INuiSensor * pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL != m_pNuiSensor) { // Initialize the Kinect and specify that we'll be using depth #ifdef RECORD_PLAYER hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_COLOR); #else hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR); #endif if (SUCCEEDED(hr)) { // Create an event that will be signaled when depth data is available m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); #ifdef RECORD_PLAYER // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, cDepthResolution, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle); #else // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, cDepthResolution, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle); #endif // Create an event that will be signaled when color data is available m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a color image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, cColorResolution, 0, 2, m_hNextColorFrameEvent, &m_pColorStreamHandle); } } if (NULL == m_pNuiSensor || FAILED(hr)) { SetStatusMessage(L"No ready Kinect found!"); return E_FAIL; } m_pNuiSensor->NuiSkeletonTrackingDisable(); return hr; }
int32_t Kinect::getDeviceCount() { int32_t deviceCount = 0; NuiGetSensorCount( &deviceCount ); return deviceCount; }
void KinectDataMan::ShowColorDepth() { // init kinect and connect if( m_cvhelper.IsInitialized() ) return; m_cvhelper.SetColorFrameResolution(color_reso); m_cvhelper.SetDepthFrameResolution(depth_reso); HRESULT hr; // Get number of Kinect sensors int sensorCount = 0; hr = NuiGetSensorCount(&sensorCount); if (FAILED(hr)) { return; } // If no sensors, update status bar to report failure and return if (sensorCount == 0) { cerr<<"No kinect"<<endl; } // Iterate through Kinect sensors until one is successfully initialized for (int i = 0; i < sensorCount; ++i) { INuiSensor* sensor = NULL; hr = NuiCreateSensorByIndex(i, &sensor); if (SUCCEEDED(hr)) { hr = m_cvhelper.Initialize(sensor); if (SUCCEEDED(hr)) { // Report success cerr<<"Kinect initialized"<<endl; break; } else { // Uninitialize KinectHelper to show that Kinect is not ready m_cvhelper.UnInitialize(); return; } } } DWORD width, height; m_cvhelper.GetColorFrameSize(&width, &height); Size colorsz(width, height); Mat cimg(colorsz, m_cvhelper.COLOR_TYPE); m_cvhelper.GetDepthFrameSize(&width, &height); Size depthsz(width, height); Mat dimg(depthsz, m_cvhelper.DEPTH_RGB_TYPE); // start processing while(true) { // get color frame if( SUCCEEDED(m_cvhelper.UpdateColorFrame()) ) { HRESULT hr = m_cvhelper.GetColorImage(&cimg); if(FAILED(hr)) break; imshow("color", cimg); if( waitKey(10) == 'q' ) break; } if( SUCCEEDED(m_cvhelper.UpdateDepthFrame()) ) { HRESULT hr = m_cvhelper.GetDepthImageAsArgb(&dimg); if(FAILED(hr)) break; imshow("depth", dimg); if( waitKey(10) == 'q' ) break; } } destroyAllWindows(); }
HRESULT KinectSDKGrabber::CreateFirstConnected() { INuiSensor *pNuiSensor; HRESULT hr; int iSensorCount = 0; hr = NuiGetSensorCount(&iSensorCount); if (FAILED(hr)) { return hr; } std::cout << "Count" << std::endl; // Look at each Kinect sensor for (int i = 0; i < iSensorCount; ++i) { // Create the sensor so we can check status, if we can't create it, move on to the next hr = NuiCreateSensorByIndex(i, &pNuiSensor); if (FAILED(hr)) { continue; } std::cout << "Created" << std::endl; // Get the status of the sensor, and if connected, then we can initialize it hr = pNuiSensor->NuiStatus(); if (S_OK == hr) { m_pNuiSensor = pNuiSensor; break; } // This sensor wasn't OK, so release it since we're not using it pNuiSensor->Release(); } if (NULL != m_pNuiSensor) { // Initialize the Kinect and specify that we'll be using depth hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR); if (SUCCEEDED(hr)) { // Create an event that will be signaled when depth data is available m_hNextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // Open a depth image stream to receive depth frames hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle); m_pNuiSensor->NuiImageStreamSetImageFrameFlags(m_pDepthStreamHandle, NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE); std::cout << "Depth" << std::endl; } if (SUCCEEDED(hr)) { m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); hr = m_pNuiSensor->NuiImageStreamOpen( NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0, 2, m_hNextColorFrameEvent, &m_pColorStreamHandle); std::cout << "Color" << std::endl; } } if (NULL == m_pNuiSensor || FAILED(hr)) { return E_FAIL; } return hr; }