none
Face Not Being Tracked - Kinect c++ RRS feed

  • Question

  • Hello,
    I've been trying to track face, but it's not detecting them I suppose.
    The code :

    #include "stdafx.h"
    
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    #include "NuiApi.h"
    #include "NuiImageCamera.h""
    #include "NuiImageBuffer.h"
    #include "FaceTrackLib.h"
    
    using namespace std;
    
    
    
    #define COLOR_WIDTH 640
    #define COLOR_HIGHT 480
    #define DEPTH_WIDTH 320
    #define DEPTH_HIGHT 240
    
    BYTE * colorD;
    USHORT * depthD;
    
    int drawColor(HANDLE h)
    {
    	const NUI_IMAGE_FRAME * pImageFrame = NULL;
    	HRESULT hr = NuiImageStreamGetNextFrame(h, 0, &pImageFrame);
    	if (FAILED(hr))
    	{
    		cout << "Get Image Frame Failed" << endl;
    		return -1;
    	}
    	INuiFrameTexture * pTexture = pImageFrame->pFrameTexture;
    	//int x = pTexture->Pitch();
    	//cout << x<<endl;
    	NUI_LOCKED_RECT LockedRect;
    	pTexture->LockRect(0, &LockedRect, NULL, 0);
    	if (LockedRect.Pitch != 0)
    	{
    		BYTE * pBuffer = (BYTE*)LockedRect.pBits;
    		colorD = pBuffer;
    
    	}
    	NuiImageStreamReleaseFrame(h, pImageFrame);
    	return 0;
    }
    
    int drawDepth(HANDLE h)
    {
    	const NUI_IMAGE_FRAME * pImageFrame = NULL;
    	HRESULT hr = NuiImageStreamGetNextFrame(h, 0, &pImageFrame);
    	if (FAILED(hr))
    	{
    		cout << "Get Image Frame Failed" << endl;
    		return -1;
    	}
    	//  temp1 = depth;
    	INuiFrameTexture * pTexture = pImageFrame->pFrameTexture;
    	NUI_LOCKED_RECT LockedRect;
    	pTexture->LockRect(0, &LockedRect, NULL, 0);
    	if (LockedRect.Pitch != 0)
    	{
    		USHORT * pBuff = (USHORT*)LockedRect.pBits;
    		depthD = pBuff;
    		
    	}
    	NuiImageStreamReleaseFrame(h, pImageFrame);
    	return 0;
    }
    
    
    int main(int argc, char * argv[])
    {
    
    
    	HRESULT hr = NuiInitialize(
    		NUI_INITIALIZE_FLAG_USES_DEPTH
    		| NUI_INITIALIZE_FLAG_USES_COLOR);
    
    	if (hr != S_OK)
    	{
    		cout << "NuiInitialize failed" << endl;
    		return hr;
    	}
    
    	HANDLE h1 = CreateEvent(NULL, TRUE, FALSE, NULL);
    	HANDLE 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;
    	}
    
    	HANDLE h3 = CreateEvent(NULL, TRUE, FALSE, NULL);
    	HANDLE h4 = NULL;
    	hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH,
    		NUI_IMAGE_RESOLUTION_320x240, 0, 2, h3, &h4);
    	if (FAILED(hr))
    	{
    		cout << "Could not open depth stream video" << endl;
    		return hr;
    	}
    
    	IFTFaceTracker* pFT = FTCreateFaceTracker();
    	if (!pFT)
    	{
    	}
    
    	FT_CAMERA_CONFIG videoCameraConfig = { 640, 480, NUI_CAMERA_COLOR_NOMINAL_FOCAL_LENGTH_IN_PIXELS };
    
    	FT_CAMERA_CONFIG depthCameraConfig = { 320, 240, NUI_CAMERA_DEPTH_NOMINAL_FOCAL_LENGTH_IN_PIXELS };
    
    	// Initialize the face tracker
    	HRESULT hr2 = pFT->Initialize(&videoCameraConfig, &depthCameraConfig, NULL, NULL);
    	if (FAILED(hr2))
    	{
    		cout << "face tracker not started!" << endl;
    	}
    
    	IFTResult* pFTResult = NULL;
    	hr2 = pFT->CreateFTResult(&pFTResult);
    	if (FAILED(hr2))
    	{
    		// Handle errors
    	}
    
    	// Prepare image interfaces that hold RGB and depth data
    	IFTImage* pColorFrame = FTCreateImage();
    	IFTImage* pDepthFrame = FTCreateImage();
    	if (!pColorFrame || !pDepthFrame)
    	{
    		// Handle errors
    	}
    
    	pColorFrame->Attach(640, 480, colorD, FTIMAGEFORMAT_UINT8_R8G8B8, 640 * 4);
    	pDepthFrame->Attach(320, 240, depthD, FTIMAGEFORMAT_UINT16_D13P3, 320 * 2);
    
    	FT_SENSOR_DATA sensorData;
    
    	sensorData.pVideoFrame = pColorFrame;
    	sensorData.pDepthFrame = pDepthFrame;
    	sensorData.ZoomFactor = 1.0f;
    	sensorData.ViewOffset.x = 0;
    	sensorData.ViewOffset.y = 0;
    	bool isFaceTracked = false;
    
    
    
    	while (true)
    	{
    		// Call Kinect API to fill videoCameraFrameBuffer and depthFrameBuffer with RGB and depth data
    		WaitForSingleObject(h1, INFINITE);
    		drawColor(h2);
    		WaitForSingleObject(h3, INFINITE);
    		drawDepth(h4);
    
    		// Check if we are  already tracking a face
    		if (!isFaceTracked)
    		{
    			hr2 = pFT->StartTracking(&sensorData, NULL, NULL, pFTResult);
    			if (SUCCEEDED(hr2))
    			{
    				isFaceTracked = true;
    				cout << "yes" << endl;
    			}
    			else
    			{
    				cout << "no!" << endl;
    				// No faces found
    				isFaceTracked = false;
    			}
    		}
    		else
    		{
    			hr2 = pFT->ContinueTracking(&sensorData, NULL, pFTResult);
    			if (FAILED(hr2))
    			{
    				cout << "lost" << endl;
    				// Lost the face
    				isFaceTracked = false;
    			}
    		}
    
    		//do something 
    	}
    
    	// Clean up
    	pFTResult->Release();
    	pColorFrame->Release();
    	pDepthFrame->Release();
    	pFT->Release();
    
    	NuiShutdown();
    
    	return 0;
    
    }
    
    
    

    It gets stuck in the inner else statement of the while loop in main i.e. keeps on outputing "no!". I think, I'm not passing the buffers to the attach function correctly. Is it so ?

    Can anyone please point out any other error which I might be doing ?

    I'm using kinect for windows sdk v1.8.

    Thanks ! 


    Monday, August 31, 2015 12:13 PM