Example #1
0
void Main::doWork()
{
	Trajectory trajectory;
    IplImage *img = imAcqGetImg(imAcq);
    Mat grey(img->height, img->width, CV_8UC1);
    cvtColor(cvarrToMat(img), grey, CV_BGR2GRAY);

    tld->detectorCascade->imgWidth = grey.cols;
    tld->detectorCascade->imgHeight = grey.rows;
    tld->detectorCascade->imgWidthStep = grey.step;

	if(showTrajectory)
	{
		trajectory.init(trajectoryLength);
	}

    if(selectManually)
    {

        CvRect box;

        if(getBBFromUser(img, box, gui) == PROGRAM_EXIT)
        {
            return;
        }

        if(initialBB == NULL)
        {
            initialBB = new int[4];
        }

        initialBB[0] = box.x;
        initialBB[1] = box.y;
        initialBB[2] = box.width;
        initialBB[3] = box.height;
    }

    FILE *resultsFile = NULL;

    if(printResults != NULL)
    {
        resultsFile = fopen(printResults, "w");
        if(!resultsFile)
        {
            fprintf(stderr, "Error: Unable to create results-file \"%s\"\n", printResults);
            exit(-1);
        }
    }

    bool reuseFrameOnce = false;
    bool skipProcessingOnce = false;

    if(loadModel && modelPath != NULL)
    {
        tld->readFromFile(modelPath);
        reuseFrameOnce = true;
    }
    else if(initialBB != NULL)
    {
        Rect bb = tldArrayToRect(initialBB);

        printf("Starting at %d %d %d %d\n", bb.x, bb.y, bb.width, bb.height);

        tld->selectObject(grey, &bb);
        skipProcessingOnce = true;
        reuseFrameOnce = true;
    }
	
	

    while(imAcqHasMoreFrames(imAcq))
    {
        double tic = cvGetTickCount();

        if(!reuseFrameOnce)
        {
            cvReleaseImage(&img);
			img = imAcqGetImg(imAcq);
            if(img == NULL)
            {
                printf("current image is NULL, assuming end of input.\n");
                break;
            }

            cvtColor(cvarrToMat(img), grey, CV_BGR2GRAY);
        }

        if(!skipProcessingOnce)
        {
            tld->processImage(cvarrToMat(img));
			if (tld->currBB)
			{
				CvPoint center = cvPoint(tld->currBB->x + tld->currBB->width / 2, tld->currBB->y + tld->currBB->height / 2);
				Point statePt = tld->particlefilter->ParticleFileterProcess(center.x, center.y);

				cout << "center" << center.x << "\t" << center.y << endl;
				cout << "state" << statePt.x+20 << "\t" << statePt.y+40 << endl;
				getchar();
			}
        }
        else
        {
            skipProcessingOnce = false;
        }

        if(printResults != NULL)
        {
            if(tld->currBB != NULL)
            {
                fprintf(resultsFile, "%d %.2d %.2d %.2d %.2d %f\n", imAcq->currentFrame - 1, tld->currBB->x, tld->currBB->y, tld->currBB->width, tld->currBB->height, tld->currConf);
            }
            else
            {
                fprintf(resultsFile, "%d NaN NaN NaN NaN NaN\n", imAcq->currentFrame - 1);
            }
        }

        double toc = (cvGetTickCount() - tic) / cvGetTickFrequency();

        toc = toc / 1000000;

        float fps = 1 / toc;

        int confident = (tld->currConf >= threshold) ? 1 : 0;

        if(showOutput || saveDir != NULL)
        {
            char string[128];

            char learningString[10] = "";

            if(tld->learning)
            {
                strcpy(learningString, "Learning");
            }

            sprintf(string, "#%d,Posterior %.2f; fps: %.2f, #numwindows:%d, %s", imAcq->currentFrame - 1, tld->currConf, fps, tld->detectorCascade->numWindows, learningString);
            CvScalar yellow = CV_RGB(255, 255, 0);
            CvScalar blue = CV_RGB(0, 0, 255);
            CvScalar black = CV_RGB(0, 0, 0);
            CvScalar white = CV_RGB(255, 255, 255);
			CvScalar red = CV_RGB(255, 0, 0);
			CvScalar green = CV_RGB(0, 255, 0);
            if(tld->currBB != NULL)
            {
                CvScalar rectangleColor = (confident) ? blue : yellow;
                cvRectangle(img, tld->currBB->tl(), tld->currBB->br(), rectangleColor, 8, 8, 0);
				cvCircle(img, tld->statePt, 5,red,4); 
				CvPoint center = cvPoint(tld->currBB->x + tld->currBB->width / 2, tld->currBB->y + tld->currBB->height / 2);
				cvCircle(img, center, 5, green, 4);

				if(showTrajectory)       
				{
					CvPoint center = cvPoint(tld->currBB->x+tld->currBB->width/2, tld->currBB->y+tld->currBB->height/2);
					cvLine(img, cvPoint(center.x-2, center.y-2), cvPoint(center.x+2, center.y+2), rectangleColor, 2);
					cvLine(img, cvPoint(center.x-2, center.y+2), cvPoint(center.x+2, center.y-2), rectangleColor, 2);
					trajectory.addPoint(center, rectangleColor);
				}
            }
			else if(showTrajectory)
			{
				trajectory.addPoint(cvPoint(-1, -1), cvScalar(-1, -1, -1));
			}

			if(showTrajectory)
			{
				trajectory.drawTrajectory(img);
			}

            CvFont font;
            cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, .5, .5, 0, 1, 8);
            cvRectangle(img, cvPoint(0, 0), cvPoint(img->width, 50), black, CV_FILLED, 8, 0);
            cvPutText(img, string, cvPoint(25, 25), &font, white);

            if(showForeground)
            {

                for(size_t i = 0; i < tld->detectorCascade->detectionResult->fgList->size(); i++)
                {
                    Rect r = tld->detectorCascade->detectionResult->fgList->at(i);
                    cvRectangle(img, r.tl(), r.br(), white, 1);
                }

            }


            if(showOutput)
            {
                gui->showImage(img);
                char key = gui->getKey();

                if(key == 'q') break;

                if(key == 'b')
                {

                    ForegroundDetector *fg = tld->detectorCascade->foregroundDetector;

                    if(fg->bgImg.empty())
                    {
                        fg->bgImg = grey.clone();
                    }
                    else
                    {
                        fg->bgImg.release();
                    }
                }

                if(key == 'c')
                {
                    //clear everything
                    tld->release();
                }

                if(key == 'l')
                {
                    tld->learningEnabled = !tld->learningEnabled;
                    printf("LearningEnabled: %d\n", tld->learningEnabled);
                }

                if(key == 'a')
                {
                    tld->alternating = !tld->alternating;
                    printf("alternating: %d\n", tld->alternating);
                }

                if(key == 'e')
                {
                    tld->writeToFile(modelExportFile);
                }

                if(key == 'i')
                {
                    tld->readFromFile(modelPath);
                }

                if(key == 'r')
                {
                    CvRect box;

                    if(getBBFromUser(img, box, gui) == PROGRAM_EXIT)
                    {
                        break;
                    }

                    Rect r = Rect(box);

                    tld->selectObject(grey, &r);
                }
            }

            if(saveDir != NULL)
            {
                char fileName[256];
                sprintf(fileName, "%s/%.5d.png", saveDir, imAcq->currentFrame - 1);

                cvSaveImage(fileName, img);
            }
        }

        if(reuseFrameOnce)
        {
            reuseFrameOnce = false;
        }
    }

    cvReleaseImage(&img);
    img = NULL;

    if(exportModelAfterRun)
    {
        tld->writeToFile(modelExportFile);
    }

    if(resultsFile)
    {
        fclose(resultsFile);
    }
}
Example #2
0
void Main::doWork()
{
	Trajectory trajectory;
	//usleep( 1000 );
    IplImage *img = imAcqGetImg( imAcq );
    std::vector<cv::Point> flandmarkVector;
	cv::Point pupil;
	
    int loops = 50; // skip the first 50 frames of the video.
    for ( int counter = 0; counter <= loops; counter++ )
    {
    	img = imAcqGetImg( imAcq );
    }
    Mat grey( img->height, img->width, CV_8UC1 );
    cvtColor( cv::Mat( img ), grey, CV_BGR2GRAY );

    tld->detectorCascade->imgWidth = grey.cols;
    tld->detectorCascade->imgHeight = grey.rows;
    tld->detectorCascade->imgWidthStep = grey.step;
	
	if( showTrajectory )
	{
		trajectory.init( trajectoryLength );
	}

    if( selectManually )
    {

        CvRect box;

        if( getBBFromUser( img, box, gui ) == PROGRAM_EXIT )
        {
            return;
        }

        if( initialBB == NULL )
        {
            initialBB = new int[ 4 ];
        }

        initialBB[ 0 ] = box.x;
        initialBB[ 1 ] = box.y;
        initialBB[ 2 ] = box.width;
        initialBB[ 3 ] = box.height;
    }

	// initialBB is not being set when there is no parameter -b given
	doHaar( grey );
	
    
    
    FILE *resultsFile = NULL;

    if( printResults != NULL )
    {
        resultsFile = fopen( printResults, "w" );
    }

    bool reuseFrameOnce = false;
    bool skipProcessingOnce = false;


	
	
    if( loadModel && modelPath != NULL )
    {
        tld->readFromFile(modelPath);
        reuseFrameOnce = true;
    }
    else if( initialBB != NULL )
    {
        Rect bb = tldArrayToRect( initialBB );

        printf( "Starting at %d %d %d %d\n",
				bb.x,
				bb.y,
				bb.width,
				bb.height
		);

        tld->selectObject( grey, &bb );
        skipProcessingOnce = true;
        reuseFrameOnce = true;
    }
    
    // deleting initialBB to avoid memLeaks
    delete initialBB;
	cv::Rect faceBB;
	
	eyeTld->detectorCascade->imgWidth = tld->currBB->width;
    eyeTld->detectorCascade->imgHeight = tld->currBB->height;
    eyeTld->detectorCascade->imgWidthStep = grey.step;
    cv::Rect eye = doDetectEye( img );
    
	eye.x += tld->currBB->x;
	eye.y += tld->currBB->y;
	eyeTld->selectObject( grey, &eye );
	
	//cv::Point pupil = cv::Point( 0, 0 );
	
    while( imAcqHasMoreFrames( imAcq ) )
    {
        double tic = cvGetTickCount();

        if( !reuseFrameOnce )
        {
            img = imAcqGetImg( imAcq );

            if( img == NULL )
            {
                printf( "current image is NULL, assuming end of input.\n" );
                break;
            }

            cvtColor( cv::Mat( img ), grey, CV_BGR2GRAY );
        }

        if( !skipProcessingOnce )
        {
            tld->processImage( img );
            eyeTld->processImage( img );
        }
        else
        {
            skipProcessingOnce = false;
        }
        faceBB = cv::Rect( tld->currBB->x,
        		tld->currBB->y,
        		tld->currBB->width,
        		tld->currBB->height
        );
        std::cout << "bb [ " <<
	        	tld->currBB->x << ", " <<
	        	tld->currBB->y << ", " <<
	        	tld->currBB->width << ", " <<
	        	tld->currBB->height << "]" <<
	        	std::endl;
		pupil = findPupil( img, cv::Rect( eyeTld->currBB->x,
				eyeTld->currBB->y,
				eyeTld->currBB->width,
				eyeTld->currBB->height
		) );
		std::cout << "pupil [ " <<
				pupil.x <<
				", " <<
				pupil.y <<
				" ]" <<
				std::endl;
				
	        	
		/*
        flandmarkVector = doFlandmark( *img, faceBB );
        if ( flandmarkVector.size() == 2 )
        {
        		std::cout << " [ " << 
        				flandmarkVector[ 0 ].x << ", " << 
        				flandmarkVector[ 0 ].y << " ]";
        		std::cout << " -- [ " << 
        				flandmarkVector[ 1 ].x << ", " << 
        				flandmarkVector[ 1 ].y << " ]" <<
        				std::endl;

				if ( flandmarkVector[ 0 ].x > 0 && 
						flandmarkVector[ 0 ].y > 0 && 
						flandmarkVector[ 0 ].x < tld->detectorCascade->imgWidth &&
						flandmarkVector[ 0 ].y < tld->detectorCascade->imgHeight
				)
				{
					cvCircle ( img,
							flandmarkVector[ 0 ],
							4,
							CV_RGB( 0, 111, 255 ),
							4
					);
				}
				if ( flandmarkVector[ 1 ].x > 0 && 
						flandmarkVector[ 1 ].y > 0 &&
						flandmarkVector[ 1 ].x < tld->detectorCascade->imgWidth &&
						flandmarkVector[ 1 ].y < tld->detectorCascade->imgHeight
				)
				{
					cvCircle ( img,
							flandmarkVector[ 1 ],
							4,
							CV_RGB( 0, 111, 255 ),
							4
					);
				}
        }
        else
        {
        	std::cout << "Flandmark vector is of size " <<
        			flandmarkVector.size() << std::endl;
        }
		*/
		
		/*
		printf( "eye: %d %d %d %d\n",
				eye.x,
				eye.y,
				eye.width,
				eye.height
		);
		*/
		//eye.width += tld->currBB->width;
		//eye.height += tld->currBB->height;
		

        if( printResults != NULL )
        {
            if( tld->currBB != NULL )
            {
                fprintf( resultsFile,
                		"%d %.2d %.2d %.2d %.2d %f\n",
                		imAcq->currentFrame - 1,
                		tld->currBB->x,
                		tld->currBB->y,
                		tld->currBB->width,
                		tld->currBB->height,
                		tld->currConf
                );
            }
            else
            {
                fprintf( resultsFile,
                		"%d NaN NaN NaN NaN NaN\n",
                		imAcq->currentFrame - 1
                );
            }
        }

        double toc = ( cvGetTickCount() - tic ) / cvGetTickFrequency();

        toc = toc / 1000000;

        float fps = 1 / toc;

        int confident = (tld->currConf >= threshold) ? 1 : 0;

        if( showOutput || saveDir != NULL )
        {
            char string[128];
            char learningString[10] = "";

            if( tld->learning && !eyeTld->learning)
            {
                strcpy( learningString, "Learning+-" );
            }
            else if( tld->learning && eyeTld->learning )
            {
                strcpy( learningString, "Learning++" );
            }
            else if( !tld->learning && eyeTld->learning )
            {
                strcpy( learningString, "Learning-+" );
            }
            
            

            sprintf( string,
            		"#%d,Posterior %.2f; fps: %.2f, #numwindows:%d, %s",
            		imAcq->currentFrame - 1,
            		tld->currConf,
            		fps,
            		tld->detectorCascade->numWindows,
            		learningString
            );
            CvScalar yellow = CV_RGB( 255, 255, 0 );
            CvScalar blue = CV_RGB( 0, 0, 255 );
            CvScalar black = CV_RGB( 0, 0, 0 );
            CvScalar white = CV_RGB( 255, 255, 255 );

            if( tld->currBB != NULL && eyeTld->currBB != NULL )
            {
                CvScalar rectangleColor = ( confident ) ? blue : yellow;
                cvRectangle( img,
                		tld->currBB->tl(),
                		tld->currBB->br(),
                		rectangleColor,
                		8,
                		8,
                		0
                );
                
                CvScalar rectangleColor2 = ( confident ) ? blue : yellow;
                cvRectangle( img,
                		eyeTld->currBB->tl(),
                		eyeTld->currBB->br(),
                		rectangleColor2,
                		8,
                		8,
                		0
                );
                /*
				pupil = findPupil( img, cv::Rect( eyeTld->currBB->x, eyeTld->currBB->y, eyeTld->currBB->width, eyeTld->currBB->height ) );
				cvRectangle( img,
						cv::Point( pupil.x, pupil.y ),
						cv::Point( pupil.x + 2, pupil.y + 2 ),
						2,
						2,
						0
				);
				*/
						
                /*
                if ( eye.x > 0 && 
						eye.y > 0 &&
						eye.width > 0 &&
						eye.height > 0
				)
                {						
					cvRectangle( img,
							cv::Point( eye.x, eye.y ),
							cv::Point( eye.x + eye.width,
									eye.y + eye.height
							),
							rectangleColor,
							8,
							8,
							0
					);
				}*/

				if( showTrajectory )
				{
					CvPoint center = cvPoint( 
							tld->currBB->x + tld->currBB->width / 2,
							tld->currBB->y + tld->currBB->height / 2
					);
					cvLine( img,
							cvPoint( center.x - 2, center.y - 2 ),
							cvPoint( center.x + 2, center.y + 2 ),
							rectangleColor,
							2
					);
					cvLine( img,
							cvPoint( center.x - 2, center.y + 2),
							cvPoint( center.x + 2, center.y - 2),
							rectangleColor,
							2
					);
					trajectory.addPoint( center, rectangleColor );
				}
            }
			else if( showTrajectory )
			{
				trajectory.addPoint( cvPoint( -1, -1 ),
						cvScalar( -1, -1, -1 ) 
				);
			}

			if( showTrajectory )
			{
				trajectory.drawTrajectory( img );
			}

            CvFont font;
            cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX, .5, .5, 0, 1, 8 );
            // display a black bar at the top of the output window
            // for a better background to write on.
            cvRectangle( img,
            		cvPoint( 0, 0 ),
            		cvPoint( img->width, 50 ),
            		black,
            		CV_FILLED,
            		8,
            		0
            );
            cvPutText( img, string, cvPoint( 25, 25 ), &font, white );

            if( showForeground )
            {
                for( size_t i = 0;
                		i < tld->detectorCascade->detectionResult->fgList->size();
                		i++
                )
                {
                    Rect r = tld->detectorCascade->detectionResult->fgList->at( i );
                    cvRectangle( img, r.tl(), r.br(), white, 1 );
                }

            }


            if( showOutput )
            {
                gui->showImage( img );
                char key = gui->getKey();

                if( key == 'q' )
                {
                	break;
                }

                if( key == 'b' )
                {
                    ForegroundDetector *fg = 
                    		tld->detectorCascade->foregroundDetector;

                    if( fg->bgImg.empty() )
                    {
                        fg->bgImg = grey.clone();
                    }
                    else
                    {
                        fg->bgImg.release();
                    }
                }

                if( key == 'c' )
                {
                    //clear everything
                    tld->release();
                }
                
                if( key == 'l' )
                {
                    tld->learningEnabled = !tld->learningEnabled;
                    printf( "LearningEnabled: %d\n", tld->learningEnabled );
                }

                if( key == 'a' )
                {
                    tld->alternating = !tld->alternating;
                    printf( "alternating: %d\n", tld->alternating );
                }

                if( key == 'e' )
                {
                    tld->writeToFile( modelExportFile );
                }

                if( key == 'i' )
                {
                    tld->readFromFile( modelPath );
                }

                if( key == 'r' )
                {
                    CvRect box;

                    if( getBBFromUser( img, box, gui ) == PROGRAM_EXIT )
                    {
                        break;
                    }

                    Rect r = Rect( box );

                    tld->selectObject( grey, &r );
                }
            }

            if( saveDir != NULL )
            {
                char fileName[ 256 ];
                sprintf( fileName, 
                		"%s/%.5d.png",
                		saveDir,
                		imAcq->currentFrame - 1
                );

                cvSaveImage( fileName, img );
            }
        }

        if( !reuseFrameOnce )
        {
            cvReleaseImage( &img );
        }
        else
        {
            reuseFrameOnce = false;
        }
    }

    if( exportModelAfterRun )
    {
        tld->writeToFile( modelExportFile );
    }
    //FB: Avoiding leakage
	if( printResults != NULL )
    {
		fclose( resultsFile );
    }
}