Beispiel #1
0
PvStream * MLTCam::OpenStream( const PvDeviceInfo *aDeviceInfo )
{
    PvStream *aStream;
    PvResult lResult;

    // Open stream to the GigE Vision or USB3 Vision device
    qDebug() << "Opening stream to device.";
    aStream = aStream->CreateAndOpen( aDeviceInfo->GetConnectionID(), &lResult );
    if ( aStream == NULL )
    {
        cout << "Unable to stream from " << aDeviceInfo->GetDisplayID().GetAscii() << "." << endl;
    }

    return aStream;
}
Beispiel #2
0
int main( int aCount, const char ** aArgs )
{
    char lDeviceAddress[1024];
    char lMulticastAddress[1024];
    char lLocalAddress[1024];

    memset( lLocalAddress, 0, 1024 );
    sprintf( lMulticastAddress, "239.192.1.1" );
    memset( lDeviceAddress, 0, 1024 );

    bool lPassive = true;
    PvUInt32 lChannel = 0;
    PvUInt16 lHostPort = 1042;
    PvResult lResult;
    for ( int i=1; i<aCount; i++ )
	{
		std::string lString = aArgs[i];
        if ( lString.find( "--hostport" ) != std::string::npos )
        {
            sscanf( aArgs[i], "--hostport=%d", &lHostPort );
        }
        else if ( lString.find( "--localaddress" ) != std::string::npos )
        {
            sscanf( aArgs[i], "--localaddress=%s", lLocalAddress );
        }
        else if ( lString.find( "--multicastaddress" ) != std::string::npos )
        {
            sscanf( aArgs[i], "--multicastaddress=%s", lMulticastAddress );
        }
        else if ( lString.find( "--deviceaddress" ) != std::string::npos )
        {
            sscanf( aArgs[i], "--deviceaddress=%s", lDeviceAddress );
        }
        else if ( lString.find( "--unicast" ) != std::string::npos )
        {
            memset( lMulticastAddress, 0, 1024 );
        }
        else if ( lString.find( "--connectdevice" ) != std::string::npos )
        {
            lPassive = false;
        }
        else if ( lString.find( "--channel" ) != std::string::npos )
        {
            sscanf( aArgs[i], "--channel=%d", &lChannel );
        }
        else if ( lString.find( "--help" ) != std::string::npos )
        {
            PrintHelp();
            return 0;
        }
        else
        {
            printf( "Did not recognize argument %s\n", aArgs[i] );
            PrintHelp();
            return 1;
        }
    }

    if ( strlen( lDeviceAddress ) == 0 )
    {
        // No device address specified. Prompt with the device finder.
        PvDeviceFinderWnd lWnd;
        if ( !lWnd.ShowModal().IsOK() )
        {
            printf( "No GEV device selected.\n" );
            return 1;
        }
        PvDeviceInfo* lInfo = lWnd.GetSelected();
        sprintf( lDeviceAddress, "%s", lInfo->GetIPAddress().GetAscii() );
    }

    PvStream lStream;

    if ( strlen( lMulticastAddress ) == 0 )
    {
        lResult = lStream.Open( lDeviceAddress, lHostPort, lChannel, lLocalAddress );
        printf( "Receiving from device %s on interface %s:%d\n", 
            lDeviceAddress, lStream.GetLocalIPAddress().GetAscii(), lStream.GetLocalPort() );
    }
    else
    {
        lResult = lStream.Open( lDeviceAddress, lMulticastAddress, lHostPort, lChannel, lLocalAddress );
        printf( "Receiving from multicast address %s:%d (device %s) on interface %s:%d\n",
            lMulticastAddress, lHostPort, lDeviceAddress, lStream.GetLocalIPAddress().GetAscii(), lStream.GetLocalPort() );
    }

    if ( !lResult.IsOK() )
    {
        printf( "Failed opening the incoming stream: %s\n", lResult.GetDescription().GetAscii() );
        return 1;
    }

    PvPipeline lPipeline( &lStream );

    PvDevice lDevice;
    PvGenParameterArray *lDeviceParams = NULL;
    if ( !lPassive )
    {
        lResult = lDevice.Connect( lDeviceAddress );
        if ( !lResult.IsOK() )
        {
            printf( "Failed connecting to the device to set its destination and initiate an AcquisitionStart: %s\n", 
                lResult.GetDescription().GetAscii() );
            printf( "If the eBUS Transmitter to receive from doesn't have full device capabilities, add the --passive command line option and initiate streaming manually.\n" );
            return 1;
        }
        lDevice.SetStreamDestination( lStream.GetLocalIPAddress(), lStream.GetLocalPort(), lChannel );
            
        // Get device parameters need to control streaming
        lDeviceParams = lDevice.GetGenParameters();

        // Reading payload size from device. Otherwise, the pipeline may miss the first several images.
        PvInt64 lReceivePayloadSize = 0;
	    lDeviceParams->GetIntegerValue( "PayloadSize", lReceivePayloadSize );

        // Set the Buffer size and the Buffer count
        lPipeline.SetBufferSize( static_cast<PvUInt32>( lReceivePayloadSize ) );
    }

    lPipeline.SetBufferCount( 16 ); // Increase for high frame rate without missing block IDs
    lPipeline.Start();

    if ( !lPassive )
    {
        // TLParamsLocked is optional but when present, it MUST be set to 1
        // before sending the AcquisitionStart command
	    lDeviceParams->SetIntegerValue( "TLParamsLocked", 1 );

	    lDeviceParams->ExecuteCommand( "GevTimestampControlReset" );

        // The pipeline is already "armed", we just have to tell the device
        // to start sending us images
	    lDeviceParams->ExecuteCommand( "AcquisitionStart" );
    }
    
    // Get stream parameters/stats
    PvGenParameterArray *lStreamParams = lStream.GetParameters();

    printf( "Press any key to stop receiving.  \n" );

    char lDoodle[] = "|\\-|-/";
    int lDoodleIndex = 0;
    PvInt64 lImageCountVal = 0;
    double lFrameRateVal = 0.0;
    double lBandwidthVal = 0.0;

    while ( !PvKbHit() )
    {
        PvBuffer *lBuffer = NULL;
        PvResult  lOperationResult;
        PvResult lResult = lPipeline.RetrieveNextBuffer( &lBuffer, 1000, &lOperationResult );
        
        if ( lResult.IsOK() )
        {
            if ( lOperationResult.IsOK() )
            {
                //
                // We now have a valid buffer. This is where you would typically process the buffer.
                // -----------------------------------------------------------------------------------------
                // ...

                lStreamParams->GetIntegerValue( "ImagesCount", lImageCountVal );
                lStreamParams->GetFloatValue( "AcquisitionRateAverage", lFrameRateVal );
                lStreamParams->GetFloatValue( "BandwidthAverage", lBandwidthVal );

                printf( "%c BlockID: %016llX %.01f FPS %.01f Mb/s\r", 
                    lDoodle[ lDoodleIndex ],
                    lBuffer->GetBlockID(),
                    lFrameRateVal,
                    lBandwidthVal / 1000000.0 ); 
            }
            // We have an image - do some processing (...) and VERY IMPORTANT,
            // release the buffer back to the pipeline
            lPipeline.ReleaseBuffer( lBuffer );
        }
        else
        {
            printf( "%c Timeout\r", lDoodle[ lDoodleIndex ] );
        }

        ++lDoodleIndex %= 6;
    }
}
bool StartSlave()
{
	// Let the user select the device to receive from
	PvString lDeviceIP;
	if ( !SelectDevice( lDeviceIP ) )
	{
		return false;
	}
	
	// Create the PvStream object
	PvStream lStream;

	// Create the PvPipeline object
	PvPipeline lPipeline( &lStream );

    // Create a PvPipeline event sink (used to trap buffer too small events)
    PipelineEventSink lPipelineEventSink;
    lPipeline.RegisterEventSink( &lPipelineEventSink );

	// Open stream
	printf( "Opening stream\n" );
	lStream.Open( lDeviceIP, "239.192.1.1", 1042 );

	// IMPORTANT: the pipeline needs to be "armed", or started before 
	// we instruct the device to send us images
	printf( "Starting pipeline\n" );
    lPipeline.SetBufferCount( 16 );
	lPipeline.Start();

	// Get stream parameters/stats
	PvGenParameterArray *lStreamParams = lStream.GetParameters();
	PvGenInteger *lCount = dynamic_cast<PvGenInteger *>( lStreamParams->Get( "ImagesCount" ) );
	PvGenFloat *lFrameRate = dynamic_cast<PvGenFloat *>( lStreamParams->Get( "AcquisitionRateAverage" ) );
	PvGenFloat *lBandwidth = dynamic_cast<PvGenFloat *>( lStreamParams->Get( "BandwidthAverage" ) );
	PvGenBoolean *lIgnoreMissingPackets = dynamic_cast<PvGenBoolean *>( lStreamParams->Get( "IgnoreMissingPackets" ) );

	// Disabling resend packets
	lIgnoreMissingPackets->SetValue( true );

	char lDoodle[] = "|\\-|-/";
	int lDoodleIndex = 0;
	PvInt64 lImageCountVal = 0;
	double lFrameRateVal = 0.0;
	double lBandwidthVal = 0.0;

	// Acquire images until the user instructs us to stop
	printf( "\n<press a key to stop receiving>\n" );
	while ( !PvKbHit() )
	{
		// Retrieve next buffer		
		PvBuffer *lBuffer = NULL;
        PvResult  lOperationResult;
		PvResult lResult = lPipeline.RetrieveNextBuffer( &lBuffer, 1000, &lOperationResult );
		
        if ( lResult.IsOK() )
		{
            if (lOperationResult.IsOK())
            {
                //
                // We now have a valid buffer. This is where you would typically process the buffer.
                // -----------------------------------------------------------------------------------------
                // ...

			    lCount->GetValue( lImageCountVal );
			    lFrameRate->GetValue( lFrameRateVal );
			    lBandwidth->GetValue( lBandwidthVal );
			
				// If the buffer contains an image, display width and height
				PvUInt32 lWidth = 0, lHeight = 0;
				if ( lBuffer->GetPayloadType() == PvPayloadTypeImage )
				{
					// Get image specific buffer interface
					PvImage *lImage = lBuffer->GetImage();

					// Read width, height
					lWidth = lBuffer->GetImage()->GetWidth();
					lHeight = lBuffer->GetImage()->GetHeight();
				}
				
				printf( "%c BlockID: %016llX W: %i H: %i %.01f FPS %.01f Mb/s\r", 
                    lDoodle[ lDoodleIndex ],
                    lBuffer->GetBlockID(),
					lWidth,
					lHeight,
                    lFrameRateVal,
                    lBandwidthVal / 1000000.0 ); 
            }

            // We have an image - do some processing (...) and VERY IMPORTANT,
			// release the buffer back to the pipeline
			lPipeline.ReleaseBuffer( lBuffer );
		}
		else
		{
			// Timeout
			printf( "%c Timeout\r", lDoodle[ lDoodleIndex ] );
		}

		++lDoodleIndex %= 6;
	}

	PvGetChar(); // Flush key buffer for next stop
	printf( "\n\n" );

	// We stop the pipeline - letting the object lapse out of 
	// scope would have had the destructor do the same, but we do it anyway
	printf( "Stop pipeline\n" );
	lPipeline.Stop();

	// Now close the stream. Also optionnal but nice to have
	printf( "Closing stream\n" );
	lStream.Close();

    // Unregister pipeline event sink. Optional but nice to have.
    lPipeline.UnregisterEventSink( &lPipelineEventSink );

	return true;
}
Beispiel #4
0
bool AcquireImages()
{
	PvResult lResult;	
	PvDeviceInfo *lDeviceInfo = NULL;
	PvSystem lSystem;
	PvStream lStream;
	lSystem.SetDetectionTimeout( 20000 );
	lResult = lSystem.Find();
	if( !lResult.IsOK() )
	{
		cout << "PvSystem::Find Error: " << lResult.GetCodeString().GetAscii();
		return -1;
	}
	PvUInt32 lInterfaceCount = lSystem.GetInterfaceCount();
	for( PvUInt32 x = 0; x < lInterfaceCount; x++ )
	{
		PvInterface * lInterface = lSystem.GetInterface( x );
		cout << "Ethernet Interface " << endl;
		cout << "IP Address: " << lInterface->GetIPAddress().GetAscii() << endl;
		cout << "Subnet Mask: " << lInterface->GetSubnetMask().GetAscii() << endl << endl;
		PvUInt32 lDeviceCount = lInterface->GetDeviceCount();
		for( PvUInt32 y = 0; y < lDeviceCount ; y++ )
		{
			lDeviceInfo = lInterface->GetDeviceInfo( y );
			cout << "ThermoCam " << endl;
			cout << "IP Address: " << lDeviceInfo->GetIPAddress().GetAscii() << endl;
		}
	}
	if( lDeviceInfo != NULL )
	{
		cout << "Connecting to " << lDeviceInfo->GetIPAddress().GetAscii() << endl;
		PvDevice lDevice;
		lResult = lDevice.Connect( lDeviceInfo );
		if ( !lResult.IsOK() )
		{
			cout << "Unable to connect to " << lDeviceInfo->GetIPAddress().GetAscii() << endl;
		}
		else
		{
			cout << "Successfully connected to " << lDeviceInfo->GetIPAddress().GetAscii() << endl;
    			lResult = lDevice.NegotiatePacketSize( );
    			if ( !lResult.IsOK() )
    			{
				cout << endl;
        			cout << " Failed to negotiate a packet size setting GevSCPSPacketSize to original value";
        			PvSleepMs( 2500 );
    			}
			cout << endl;
    			cout << "3. Open stream......";
			lResult = lStream.Open( lDeviceInfo->GetIPAddress() );
			if ( !lResult.IsOK() )
			{	
				cout << endl;
				cout << "  Failed to open stream";
				return 0;
			}
			lDevice.SetStreamDestination( lStream.GetLocalIPAddress(), lStream.GetLocalPort() );
			PvInt64 lPayloadSize;
			lDevice.GetGenParameters()->GetIntegerValue( "PayloadSize", lPayloadSize );
			PvBuffer * lBuffer = new PvBuffer();
			lBuffer->Alloc( static_cast<PvUInt32>( lPayloadSize ) );
			PvBuffer *lPtr = NULL; 
			PvImage *lImage = NULL;
			cout << endl;
			cout << "5. Grab one image" << endl;
			lStream.QueueBuffer( lBuffer );
			lDevice.GetGenParameters()->SetIntegerValue( "TLParamsLocked", 1 );
			lDevice.GetGenParameters()->ExecuteCommand( "AcquisitionStart" );
			PvResult lStreamResult;
			lResult = lStream.RetrieveBuffer( &lPtr, &lStreamResult, 10000 );
			lDevice.GetGenParameters()->ExecuteCommand( "AcquisitionStop" );
			lDevice.GetGenParameters()->SetIntegerValue( "TLParamsLocked", 0 );
			PvInt64 lWidth = 0, lHeight = 0;
			PvGenParameterArray *lDeviceParams = lDevice.GetGenParameters();	
			lDeviceParams->GetIntegerValue( "Width", lWidth);
			lDeviceParams->GetIntegerValue( "Height", lHeight);			
			cvNamedWindow("OpenCV: ThermoCam",CV_WINDOW_NORMAL);
			cv::Mat raw_lImage(cv::Size(lWidth,lHeight),CV_8U);
			if ( lResult.IsOK() )
			{
				if ( lStreamResult.IsOK() )
				{
					cout << endl;
					cout << "6. Using RGB Filter";		
					lImage=lPtr->GetImage();
					lPtr->GetImage()->Alloc(lImage->GetWidth(),lImage->GetHeight(),PvPixelMono8);
					cout << "  a. Save the original image into ImageOriginal.bmp";			
					PvBufferWriter lBufferWriter;			
					lBufferWriter.Store(lPtr,"ThermoCam.bmp",PvBufferFormatBMP);
				}
				lImage->Attach(raw_lImage.data,lImage->GetWidth(),lImage->GetHeight(),PvPixelMono8);
				//cv::imshow("OpenCV: ThermoCam",raw_lImage);
				cv::FileStorage fs("ThermoCam.xml",cv::FileStorage::WRITE);		
				fs << "raw_lImage" << raw_lImage;
				fs.release();	
				//if(cv::waitKey(1000) >= 0) break;
				lPtr->Free();
			}
		  	lBuffer->Free();
			lDevice.ResetStreamDestination();
			lStream.Close();
			lDevice.Disconnect();
			return true;
		}
	}
	else
	{
		cout << "No device found" << endl;
	}
	return 0;
}