/// <summary> /// Start processing stream /// </summary> /// <returns>Indicates success or failure</returns> bool UKinect::setFaceStream() { pFaceTracker[0] = FTCreateFaceTracker(); pFaceTracker[1] = FTCreateFaceTracker(); if (!pFaceTracker[0] || !pFaceTracker[1]) { cerr << "[UKinect] ERROR: Can not create face tracker." << endl; return false; } FT_CAMERA_CONFIG colorConfig; FT_CAMERA_CONFIG depthConfig; hr = getColorConfiguration(&colorConfig); if (FAILED(hr)) { cerr << "[UKinect] ERROR: Can not get color configuration (face tracker)." << endl; return false; } hr = getDepthConfiguration(&depthConfig); if (FAILED(hr)) { cerr << "[UKinect] ERROR: Can not get depth configuration (face tracker)." << endl; return false; } for (int i = 0; i < 2; i++) { // Initialize the face tracker hr = pFaceTracker[i]->Initialize(&colorConfig, &depthConfig, NULL, NULL); if (FAILED(hr)) { cerr << "[UKinect] ERROR: Can not initialize face tracker "<< i << endl; return false; } if (!pFTResult[i]) { // Create a face tracking result interface hr = pFaceTracker[i]->CreateFTResult(&pFTResult[i]); if (FAILED(hr)) { cerr << "[UKinect] ERROR: Can not create face tracking result interface " << i << endl; return false; } hr = pFaceTracker[i]->CreateFTResult(&pFTResult_copy[i]); if (FAILED(hr)) { cerr << "[UKinect] ERROR: Can not create copy of face tracking result interface " << i << endl; return false; } } } faceBin.image.width = colorConfig.Width; faceBin.image.height = colorConfig.Height; faceBin.image.size = colorConfig.Width * colorConfig.Height * 3; cout << "[UKinect] INFO: Face stream has been created" << endl; return true; }
void FaceTrack::initialize(void) { try { HRESULT hr; face = FTCreateFaceTracker(); if(!face) std::cerr << "CreateFaceTracker Failed" << std::endl; FT_CAMERA_CONFIG color_config; color_config.Width = w; color_config.Height = h; color_config.FocalLength = NUI_CAMERA_COLOR_NOMINAL_FOCAL_LENGTH_IN_PIXELS * 2.f; FT_CAMERA_CONFIG depth_config; depth_config.Width = w; depth_config.Height = h; depth_config.FocalLength = NUI_CAMERA_DEPTH_NOMINAL_FOCAL_LENGTH_IN_PIXELS * 2.f; hr = face->Initialize(&color_config, &depth_config, NULL, NULL); if(FAILED(hr)) std::cerr << "FaceTracker Initialize Failed" << std::endl; hr = face->CreateFTResult(&face_result); if(FAILED(hr) || !face_result) std::cerr << "FaceTracker Result Create Failed" << std::endl; } catch(std::exception &e) { std::cerr << e.what() << std::endl; } }
void initFaceTracker(void) { HRESULT hr; pFaceTracker = FTCreateFaceTracker(NULL); // We don't use any options. if(!pFaceTracker) { STDERR("Could not create the face tracker.\r\n"); return; } FT_CAMERA_CONFIG videoConfig; videoConfig.Width = 640; videoConfig.Height = 480; videoConfig.FocalLength = NUI_CAMERA_COLOR_NOMINAL_FOCAL_LENGTH_IN_PIXELS; // 640x480 //videoConfig.FocalLength = NUI_CAMERA_COLOR_NOMINAL_FOCAL_LENGTH_IN_PIXELS * 2.f; // 1280x960 FT_CAMERA_CONFIG depthConfig; depthConfig.Width = 320; depthConfig.Height = 240; //depthConfig.FocalLength = NUI_CAMERA_DEPTH_NOMINAL_FOCAL_LENGTH_IN_PIXELS / 4.f; // 80x 60 depthConfig.FocalLength = NUI_CAMERA_DEPTH_NOMINAL_FOCAL_LENGTH_IN_PIXELS; // 320x240 //depthConfig.FocalLength = NUI_CAMERA_DEPTH_NOMINAL_FOCAL_LENGTH_IN_PIXELS * 2.f; // 640x480 hr = pFaceTracker->Initialize(&videoConfig, &depthConfig, NULL, NULL); if(!pFaceTracker) { STDERR("Could not initialize the face tracker.\r\n"); return; } hr = pFaceTracker->CreateFTResult(&pFTResult); if (FAILED(hr) || !pFTResult) { STDERR("Could not initialize the face tracker result.\r\n"); return; } iftColorImage = FTCreateImage(); if (!iftColorImage || FAILED(hr = iftColorImage->Allocate(videoConfig.Width, videoConfig.Height, FTIMAGEFORMAT_UINT8_B8G8R8X8))) { STDERR("Could not create the color image.\r\n"); return; } iftDepthImage = FTCreateImage(); if (!iftDepthImage || FAILED(hr = iftDepthImage->Allocate(320, 240, FTIMAGEFORMAT_UINT16_D13P3))) { STDERR("Could not create the depth image.\r\n"); return; } lastTrackSucceeded = false; faceScale = 0; }
// Include main Kinect SDK .h file #include "NuiAPI.h" // Include the face tracking SDK .h file #include "FaceTrackLib.h" // Create an instance of a face tracker IFTFaceTracker* pFT = FTCreateFaceTracker(); if(!pFT) { // Handle errors } // Initialize cameras configuration structures. // IMPORTANT NOTE: resolutions and focal lengths must be accurate, since it affects tracking precision! // It is better to use enums defined in NuiAPI.h // Video camera config with width, height, focal length in pixels // NUI_CAMERA_COLOR_NOMINAL_FOCAL_LENGTH_IN_PIXELS focal length is computed for 640x480 resolution // If you use different resolutions, multiply this focal length by the scaling factor FT_CAMERA_CONFIG videoCameraConfig = {640, 480, NUI_CAMERA_COLOR_NOMINAL_FOCAL_LENGTH_IN_PIXELS}; // Depth camera config with width, height, focal length in pixels // NUI_CAMERA_COLOR_NOMINAL_FOCAL_LENGTH_IN_PIXELS focal length is computed for 320x240 resolution // If you use different resolutions, multiply this focal length by the scaling factor FT_CAMERA_CONFIG depthCameraConfig = {320, 240, NUI_CAMERA_DEPTH_NOMINAL_FOCAL_LENGTH_IN_PIXELS}; // Initialize the face tracker HRESULT hr = pFT->Initialize(&videoCameraConfig, &depthCameraConfig, NULL, NULL); if( FAILED(hr) ) {
DWORD WINAPI FTHelper2::FaceTrackingThread() { FT_CAMERA_CONFIG videoConfig; FT_CAMERA_CONFIG depthConfig; FT_CAMERA_CONFIG* pDepthConfig = NULL; // Try to get the Kinect camera to work HRESULT hr = m_KinectSensor.Init(m_depthType, m_depthRes, m_bNearMode, FALSE, m_colorType, m_colorRes, m_bSeatedSkeleton); if (SUCCEEDED(hr)) { m_KinectSensorPresent = TRUE; m_KinectSensor.GetVideoConfiguration(&videoConfig); m_KinectSensor.GetDepthConfiguration(&depthConfig); pDepthConfig = &depthConfig; } else { m_KinectSensorPresent = FALSE; MessageBoxW(m_hWnd, L"Could not initialize the Kinect sensor.\n", L"Face Tracker Initialization Error\n", MB_OK); return 1; } m_UserContext = new FTHelperContext[m_nbUsers]; if (m_UserContext != 0) { memset(m_UserContext, 0, sizeof(FTHelperContext)*m_nbUsers); } else { MessageBoxW(m_hWnd, L"Could not allocate user context array.\n", L"Face Tracker Initialization Error\n", MB_OK); return 2; } for (UINT i=0; i<m_nbUsers; i++) { // Try to start the face tracker. m_UserContext[i].m_pFaceTracker = FTCreateFaceTracker(_opt); if (!m_UserContext[i].m_pFaceTracker) { MessageBoxW(m_hWnd, L"Could not create the face tracker.\n", L"Face Tracker Initialization Error\n", MB_OK); return 3; } hr = m_UserContext[i].m_pFaceTracker->Initialize(&videoConfig, pDepthConfig, NULL, NULL); if (FAILED(hr)) { WCHAR path[512], buffer[1024]; GetCurrentDirectoryW(ARRAYSIZE(path), path); wsprintf(buffer, L"Could not initialize face tracker (%s) for user %d.\n", path, i); MessageBoxW(m_hWnd, buffer, L"Face Tracker Initialization Error\n", MB_OK); return 4; } m_UserContext[i].m_pFaceTracker->CreateFTResult(&m_UserContext[i].m_pFTResult); if (!m_UserContext[i].m_pFTResult) { MessageBoxW(m_hWnd, L"Could not initialize the face tracker result for user %d.\n", L"Face Tracker Initialization Error\n", MB_OK); return 5; } m_UserContext[i].m_LastTrackSucceeded = false; } // Initialize the RGB image. m_colorImage = FTCreateImage(); if (!m_colorImage || FAILED(hr = m_colorImage->Allocate(videoConfig.Width, videoConfig.Height, FTIMAGEFORMAT_UINT8_B8G8R8X8))) { return 6; } if (pDepthConfig) { m_depthImage = FTCreateImage(); if (!m_depthImage || FAILED(hr = m_depthImage->Allocate(depthConfig.Width, depthConfig.Height, FTIMAGEFORMAT_UINT16_D13P3))) { return 7; } } SetCenterOfImage(NULL); while (m_ApplicationIsRunning) { CheckCameraInput(); InvalidateRect(m_hWnd, NULL, FALSE); UpdateWindow(m_hWnd); Sleep(16); } return 0; }