none
c++ Programming. Problem getting ColorFrames RRS feed

  • Question

  • Hello guys,

    i am stuck with a problem and help would be very appreciated :) All i want is getting Color Data with a console application.

    The problem: I dont get events with

    WaitForSingleObject(m_hNextColorFrameEvent, INFINITE);
    
    and
    
    sensor->NuiImageStreamGetNextFrame(m_pColorStreamHandle, 100, &imageFrame)

    doesnt give me frames. In the sourcecode i changed INFINITE to 2000 for testing purposes and it gives me WAIT_TIMEOUTs

    I already tried to compare my code with examples like ColorBasics-D2D, DepthWithColor-D3D and SingleFace but i cant find an error myself.

    Thanks you!

    Sourcecode:

    // ConsoleApplication3.cpp : Defines the entry point for the console application.
    
    
    
    #include <Windows.h>
    #include <Ole2.h>
    #include <iostream>
    #include <string>
    
    #include <NuiApi.h>
    #include <NuiImageCamera.h>
    #include <NuiSensor.h>
    #include <FaceTrackLib.h>
    #include <limits>
    
    using namespace std;
    
    INuiSensor * sensor; //Kinect Sensor
    HANDLE       m_hNextDepthFrameEvent = INVALID_HANDLE_VALUE;
    HANDLE       m_pDepthStreamHandle = INVALID_HANDLE_VALUE;
    HANDLE       m_hNextColorFrameEvent = INVALID_HANDLE_VALUE;
    HANDLE       m_pColorStreamHandle = INVALID_HANDLE_VALUE;
    
    
    void PressEnterToContinue()
      {
    
       cout << endl <<"Bitte ENTER druecken um fortzusetzen";
       cin.ignore();
      }
    
    void timestamp()
    {
    	SYSTEMTIME lt;
        GetLocalTime(&lt);
    	printf("%02d:%02d:%02d:%04d: ", lt.wHour, lt.wMinute,lt.wSecond,lt.wMilliseconds);
    }
    
    bool initkinect() 
    {
    	int nummerSensors=0;	//Anzahl der Kinect Sensoren
    
    	if (FAILED(NuiGetSensorCount(&nummerSensors))) //Anzahl der Kinects bestimmen
    	{
    		cout << "Fehler beim Erkennen des Kinectsensors!" <<endl;
    		return false;
    	}
    		
    	cout << "Kinectsensoren gefunden: " << nummerSensors<<endl;
    
    	if (nummerSensors==0)
    	{
    		cout << "Kein Kinectsensor gefunden!" <<endl;
    		return false;
    	}
    
    	if (FAILED(NuiCreateSensorByIndex(0, &sensor))) //Interface zur Kinect erstellen
    	{
    		cout << "Fehler beim Erstellen der Sensorverknüpfung!" <<endl;
    		return false;
    	}
    
    	if (FAILED(sensor->NuiStatus())) //Status der Kinect auslesen
    	{
    		cout << "Kinectsensor ist nicht initialisierbar!" <<endl;
    		sensor->Release(); //Sensor war nicht OK, z.B. bereits initialisiert. Wird freigegeben.
    		return false;
    	}
    
    	if (FAILED(sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR))) //Kinect initialisieren
    	//if (FAILED(sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH))) //Kinect initialisieren
    	{
    		cout << "Fehler bei der Initialisierung des Kinect Sensors!" <<endl;
    
    		return false;
    	}
    
    	if (FAILED(sensor->NuiImageStreamOpen(		// Open a color image stream to receive color frames
                    NUI_IMAGE_TYPE_COLOR,
                    NUI_IMAGE_RESOLUTION_640x480,
                    0,
                    2,
                    m_hNextColorFrameEvent,
                    &m_pColorStreamHandle))) 
    	{
    		cout << "Fehler beim öffnen des ImageStreams für ColorFrames!" <<endl;
    		return false;
    	}
    
    	//if (FAILED(sensor->NuiImageStreamOpen(
    	//			NUI_IMAGE_TYPE_DEPTH,
    	//			NUI_IMAGE_RESOLUTION_640x480,
    	//			0,
    	//			2,
    	//			NULL,
    	//			&m_pDepthStreamHandle)))
    	//{
    	//	cout << "Fehler beim öffnen des ImageStreams für DepthStream" <<endl;
    	//	return false;
    	//}
    
    
    	if ((m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
    	{
    		//Para1: Optional Pointer to SECURITY_ATTRIBUTES
    		//Para2: bManuelReset -> Event muss manuell resetet werden. Wahrscheinlich passiert dies durch die Kinect
    		//Para3: bInitialState -> Anfangswert, somit nonsignaled
    		//Para4: lpName, Optional
    		cout << "Fehler beim Erstellen des Events für ColorFrame!" <<endl;
    		return false;
    	}
    
    
    	return true;
    }
    
    void free()
    {
    	if (NULL != sensor) //Falls sensor vorhanden -> Freigeben
        {
            sensor->NuiShutdown();
            sensor->Release();
        }
    
    	////Unsicher ob das auskommentiert sein darf
    	//if (m_hNextColorFrameEvent != INVALID_HANDLE_VALUE)
     //   {
     //       CloseHandle(m_hNextColorFrameEvent);
     //   }
    
    	//if (m_hNextColorFrameEvent != INVALID_HANDLE_VALUE)
     //   {
     //       CloseHandle(m_hNextColorFrameEvent);
     //   }
    }
    
    
    void ReadyColor()
    {
    	
    		NUI_IMAGE_FRAME imageFrame;
    		NUI_LOCKED_RECT LockedRect;
    
    		// Attempt to get the color frame
    
    		if (FAILED(sensor->NuiImageStreamGetNextFrame(m_pColorStreamHandle, 100, &imageFrame)))
    		{
    			timestamp();
    			cout << "ERROR: GetNextFrame Failed!"<< endl;
    			return;
    		}
    
    		// Lock the frame data so the Kinect knows not to modify it while we're reading it
    		if (FAILED(imageFrame.pFrameTexture->LockRect(0, &LockedRect, NULL, 0)))
    		{
    			timestamp();
    			cout << "ERROR: LockRect Failed!" << endl;
    			return;
    		}
    
    		///DO SOMETHING WITH THE COLOR IMAGE
    		timestamp();
    		cout << "Color Image ready!" << endl;
    		//cout << imageFrame.liTimeStamp
    		///DO SOMETHING WITH THE COLOR IMAGE
    
    
    		// Unlock the texture
    		if (FAILED(imageFrame.pFrameTexture->UnlockRect(0)))
    		{
    			timestamp();
    			cout << "ERROR: Unlockrect Failed!" << endl;
    			return;
    		}
    
    
    		// Release the frame
    		if (FAILED(sensor->NuiImageStreamReleaseFrame(m_pColorStreamHandle, &imageFrame)))
    		{
    			timestamp();
    			cout << "ERROR: Unlockrect Failed!" << endl;
    			return;
    		}
    
    }
    
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////// M A I N ///////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////
    int main()
    {
    	cout << "Programm gestartet" <<endl;
        cout << "------------------" <<endl;
    
    	if (initkinect()==false)
    	{
    		cout << "Allgeiner Fehler beim initialisieren des Kinect Sensors" << endl;
    	}
    	else //Alles ok, der Sensor ist bereit
    	{
    //		cout << "Kinect Audio USB ID: " << sensor->NuiAudioArrayId() << endl;
    //		cout << "Sensorstatus: "<< sensor->NuiStatus() << endl;
    	}
    
    	timestamp();
    	cout << "Main Loop erreicht" << endl <<endl;	
    
    	int i = 0;
    	DWORD waiting;
    	//Mainloop
    
    	while (true)
        {
    		cout << "In Loop number: " << i << endl;	
    	    waiting =  WaitForSingleObject(m_hNextColorFrameEvent, 2000);
    
    		if (waiting == WAIT_OBJECT_0)
    			cout << "Waitergebniss: WAIT_OBJECT_0" << endl;
    
    		if (waiting == WAIT_TIMEOUT)
    			cout << "Waitergebniss: WAIT_TIMEOUT" << endl;
    
    		if (waiting == WAIT_ABANDONED)
    			cout << "Waitergebniss: WAIT_ABANDONED" << endl;
    
    		if (waiting == WAIT_FAILED)
    			cout << "Waitergebniss: WAIT_FAILED" << endl;
    
    
    
    		ReadyColor();
    		i++;		//Abbruchbedingung
    		if (i>=5)
    			break;
    		cout <<endl;
        }
    
        PressEnterToContinue(); 
    	free();
    	return 0;
    }
    

    Friday, May 17, 2013 3:06 PM

Answers

All replies