sequence_res.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <math.h>
00003 #include <string>
00004 #include <vector>
00005 #include <cv.h>
00006 #include <highgui.h>
00007 
00008 using namespace std;
00009 
00010 struct Sbox_det
00011 {
00012         cv::Rect box;
00013         float score;
00014         Sbox_det() :  box(0,0,0,0) , score(0) { }
00015         Sbox_det( cv::Rect box_ , float score_ )  :  box(box_) , score(score_) { }
00016 };
00017 
00018 void read_boxes_gt( string fileString , vector<vector<cv::Rect> >& boxes , int image_scale);
00019 void read_boxes_det( string fileString , vector<vector<Sbox_det> >& boxes );
00020 void viz_boxes( string imageString , vector<cv::Rect> detections , vector<cv::Rect> gt , vector<cv::Rect> accepted );
00021 string convertInt(int number);
00022 
00023 int main(int argc, char *argv[])
00024 {
00025         //reading input arguments -> detection file to be opened ---------------------------------------
00026         string res_filename;
00027         float threshold_max, threshold_min, threshold_step;
00028         if (argc == 5)
00029         {
00030                 res_filename = argv[1]; //example: resFILTr_mydb_tD_0.2_mT_-0.5.txt
00031                 threshold_min = atof(argv[2]);
00032                 threshold_max = atof(argv[3]); 
00033                 threshold_step = atof(argv[4]);
00034                 printf("************* Sequence results analysis ***************\n\n");
00035                 printf( "overall results are stored on global_results_%s file\n" ,  argv[1] );
00036                 printf( "frame per frame results on frame_results_<thrhold>_%s file\n\n" ,  argv[1] );
00037                 
00038         }
00039         else
00040         {
00041                 printf("Incorred usage: seq_res <file name> <threshold_min> <threshold_max> <threshold_step>\n");
00042                 return -1;
00043         }
00044         
00045         //reading the ground truth boxes ----------------------------------------------------------------
00046         string homepath = getenv("HOME");
00047         string fileString = homepath + "/Desktop/laser_vision_pr/annotations/annotations_test.txt";
00048         vector<vector<cv::Rect> > boxes_gt;
00049         read_boxes_gt( fileString , boxes_gt , 2);
00050         printf("size ground truth=  %d\n" , boxes_gt.size() );
00051         
00052         //reading result detections boxes with associated scores ----------------------------------------------
00053         fileString = homepath + "/Desktop/laser_vision_pr/test/" + res_filename ;
00054         vector<vector<cv::Rect> > boxes_det;
00055         vector<cv::Rect> frame_boxes;
00056         vector<vector<Sbox_det> > boxes_det_score;
00057         read_boxes_det(fileString , boxes_det_score );
00058         cv::Rect image_box(0,0,512,384);
00059         
00060         //creates an empty file and its header for the global results
00061         fileString = homepath + "/Desktop/laser_vision_pr/test/global_results_"  + res_filename;
00062         FILE * file_out;
00063         file_out = fopen (  fileString.c_str() , "w");
00064         fprintf(  file_out , "threshold TPR   FPR  sum_TP sum_TN sum_FP sum_FN");
00065         fclose(file_out);
00066         
00067         float threshold = threshold_min;
00068         while( threshold < threshold_max )
00069         {
00070                 //copying detection boxes that satisfy a certain threshold
00071                 boxes_det.clear();
00072                 for (unsigned int i = 0; i < boxes_det_score.size() ; i++)
00073                 {
00074                         frame_boxes.clear();
00075                         for( unsigned int j = 0; j < boxes_det_score[i].size() ; j++)
00076                         {
00077                                 if ( boxes_det_score[i][j].score > threshold )
00078                                         frame_boxes.push_back( image_box & boxes_det_score[i][j].box ); //adds new box, limited to image box to the detections 
00079                         }
00080                         boxes_det.push_back( frame_boxes );
00081                 }
00082                 printf("size detections at threshold %f = %d\n" , threshold,  boxes_det_score.size() );
00083                 
00084                 //setting positives and negatives detections ----------------------------------------------------------
00085                 vector<vector<cv::Rect> > tp_boxes;
00086                 vector<cv::Rect> tp_boxes_frame;
00087                 stringstream threshold_string;
00088                 threshold_string << threshold;
00089                 fileString = homepath + "/Desktop/laser_vision_pr/test/frame_results_" + threshold_string.str() + "_"  + res_filename;
00090                 file_out = fopen (  fileString.c_str() , "w");
00091                 fprintf ( file_out , "TP   TN   FP   FN"  );
00092                 vector<int> TP, TN, FP, FN;
00093                 //      printf("boxes size = %d" , boxes_det.size() );
00094                 TP.reserve( boxes_det.size() );
00095                 TN.reserve( boxes_det.size() );
00096                 FP.reserve( boxes_det.size() );
00097                 FN.reserve( boxes_det.size() );
00098                 cv::Rect box_inters;
00099                 int N;
00100                 boxes_det.size() > boxes_gt.size() ? N =  boxes_gt.size() : N =  boxes_det.size() ;
00101                 printf("%d\n", N);
00102                 for( unsigned int i = 0; i < N ; i++)
00103                 {
00104                         TP.push_back( 0);
00105                         TN.push_back( 0);
00106                         FP.push_back( 0);
00107                         FN.push_back( 0);
00108                         tp_boxes_frame.clear();
00109                         //printf("-------------     frame %d       ----------------------------------\n", i);
00110                         for (unsigned int j = 0; j < boxes_det[i].size() ; j++)
00111                         {
00112                                 for ( unsigned int k = 0; k < boxes_gt[i].size(); k++)
00113                                 {
00114                                         //True Positives
00115                                         box_inters = boxes_det[i][j] & boxes_gt[i][k];
00116                                         int box_inters_area = box_inters.width * box_inters.height;
00117                                         int box_union_area = boxes_gt[i][k].width * boxes_gt[i][k].height +
00118                                                                                                 boxes_det[i][j].width * boxes_det[i][j].height -
00119                                                                                                 box_inters_area;
00120         //                              printf( "detection %d -> %d  and gt %d -> %d is %d and union %d -> \n" , j, boxes_det[i][j].width * boxes_det[i][j].height,  
00121         //                                                                      k, boxes_gt[i][k].width * boxes_gt[i][k].height, box_inters_area , box_union_area );
00122                                         if ( box_inters_area * 2  >  box_union_area )
00123                                         {
00124                                                 TP[i] += 1;
00125                                                 tp_boxes_frame.push_back( boxes_det[i][j] );
00126                                                 //printf("At frame %d : detection at box_det = %d and box_gt = %d\n" , i , j , k );
00127                                                 break;
00128                                         }
00129                                 }
00130                         }
00131                         tp_boxes.push_back(tp_boxes_frame);
00132                         //False negatives
00133                         if ( TP[i] < boxes_gt[i].size() )
00134                                 FN[i] =  boxes_gt[i].size() - TP[i] ;
00135                         //False Positives
00136                         if ( TP[i] < boxes_det[i].size() )
00137                                 FP[i] =  boxes_det[i].size() - TP[i] ;
00138                         //True Negatives
00139                         if ( boxes_gt[i].size() == 0 & TP[i] == 0 )
00140                                 TN[i] = 1;
00141                         fprintf( file_out , "\n %d %d %d %d", TP[i], TN[i] , FP[i] , FN[i]);
00142                         fileString =  homepath + "/Desktop/laser_vision_pr/test/camera/frame" + convertInt(i+1) + ".jpg";
00143             
00144             // To see the frame uncomment next two lines                        
00145            //printf("         *******  frame %d  ******\n" ,i);
00146                    //viz_boxes( fileString , boxes_det[i] , boxes_gt[i] , tp_boxes[i] );
00147                         
00148                 }
00149                 fclose(file_out);
00150                 
00151                 //true and false rates calculation ----------------------------------------------------------------------------------------
00152                 int sum_TP(0), sum_TN(0), sum_FP(0), sum_FN(0);
00153                 for ( unsigned int i = 0; i < TP.size(); i++ )
00154                 {
00155                         sum_TP += TP[i];
00156                         sum_TN += TN[i];
00157                         sum_FP += FP[i];
00158                         sum_FN += FN[i];
00159                 }
00160                 //True positive rate
00161                 float TPR = (float) sum_TP / (float) ( sum_TP + sum_FN );
00162                 //false positive rate
00163                 float FPR = (float) sum_FP / (float) ( sum_FP + sum_TN );
00164 
00165                 //Final results calculations TPR and FPR -------------------------------------------------------------------------------------
00166         //      printf(  "sum_TP = %d, sum_TN = %d, sum_FP = %d, sum_FN = %d \n" , sum_TP, sum_TN, sum_FP, sum_FN );
00167         //      printf (  "TPR = %f, FPR = %f \n ",  TPR, FPR);
00168                 fileString = homepath + "/Desktop/laser_vision_pr/test/global_results_"  + res_filename;
00169                 file_out = fopen (  fileString.c_str() , "a");
00170                 fprintf(  file_out , "\n%f %f %f %d %d %d %d" , threshold, TPR, FPR, sum_TP, sum_TN, sum_FP, sum_FN );
00171                 fclose(file_out);
00172         
00173                 //updating threshold value
00174                 threshold += threshold_step;
00175         }
00176         
00177         return 0;
00178 }
00179 
00180 void read_boxes_gt( string fileString , vector<vector<cv::Rect> >& boxes , int image_scale) 
00181 {
00182         int num_det, x, y, w, h;
00183         vector<cv::Rect> instant_detections;
00184         char c[256];
00185         FILE * file_out;
00186         file_out = fopen (  fileString.c_str() , "r");
00187         fscanf(file_out, "%s " , c);
00188         bool eof_flag = 1;
00189         //while(  !feof(file_out) ){
00190         while( eof_flag )
00191         {
00192                 fscanf (file_out, "%d", &num_det);
00193                 instant_detections.clear();
00194                 for( int i =0 ; i < num_det; i++)
00195                 {
00196                         fscanf(file_out, " %d %d %d %d", &x , &y, &w, &h);
00197                         instant_detections.push_back( cv::Rect(x/image_scale,y/image_scale,w/image_scale,h/image_scale) );
00198                 }
00199                 boxes.push_back( instant_detections );
00200                 fscanf(file_out, "\n%s " , c);
00201                 if ( feof(file_out) ) eof_flag = 0;
00202         }
00203         fclose(file_out);       
00204 }
00205 
00206 void read_boxes_det( string fileString , vector<vector<Sbox_det> >& boxes ) 
00207 {
00208         int num_det, x, y, w, h;
00209         float score;
00210         vector<Sbox_det> instant_detections;
00211         char c[256];
00212         FILE * file_out;
00213         file_out = fopen (  fileString.c_str() , "r");
00214         fscanf(file_out, "%s " , c);
00215         bool eof_flag = 1;
00216         //while(  !feof(file_out) ){
00217         while( eof_flag )
00218         {
00219                 fscanf (file_out, "%d", &num_det);
00220                 instant_detections.clear();
00221                 for( int i =0 ; i < num_det; i++)
00222                 {
00223                         fscanf(file_out, " %d %d %d %d %f", &x , &y, &w, &h , &score);
00224                         instant_detections.push_back( Sbox_det ( cv::Rect(x,y,w,h) , score  ) );
00225                 }
00226                 boxes.push_back( instant_detections );
00227                 fscanf(file_out, "\n%s " , c);
00228                 if ( feof(file_out) ) 
00229                         eof_flag = 0;
00230         }
00231         fclose(file_out);       
00232 }
00233 
00234 
00235 void viz_boxes( string imageString , vector<cv::Rect> detections , vector<cv::Rect> gt , vector<cv::Rect> accepted )
00236 {
00237         cv::Mat inIm = cv::imread( imageString.c_str(), 1 );
00238         
00239         for (unsigned int i = 0; i< detections.size(); i++)
00240         {
00241                 cv::rectangle( inIm ,  detections[i].tl(), detections[i].br(), cv::Scalar(0,0,255), 2  );
00242                 printf( "Detection Box %d : x = %d, y = %d, w = %d, h = %d\n" , i, detections[i].x,detections[i].y,detections[i].width,detections[i].height );
00243         }
00244         for (unsigned int i = 0; i< gt.size(); i++)
00245         {
00246                         cv::rectangle( inIm ,  gt[i].tl(), gt[i].br(), cv::Scalar(255,0,0), 2  );
00247         }
00248         for (unsigned int i = 0; i< accepted.size(); i++)
00249         {
00250                         cv::rectangle( inIm ,  accepted[i].tl(), accepted[i].br(), cv::Scalar(0,255,0), 2  );
00251                         printf( "Accepted Box %d : x = %d, y = %d, w = %d, h = %d\n" , i, accepted[i].x,accepted[i].y,accepted[i].width,accepted[i].height );
00252         }
00253         cv::namedWindow("My Image" , CV_WINDOW_AUTOSIZE);
00254         cv::imshow("My Image",inIm);
00255         cv::waitKey(0);
00256         
00257 }
00258 
00259 string convertInt(int number)
00260 {
00261         stringstream ss;//create a stringstream
00262         ss << number;//add number to the stream
00263         switch((int) log10( (float) number))
00264         {
00265                 case 0 :
00266                         return "000" + ss.str();
00267                 case 1 :
00268                         return "00" + ss.str();
00269                 case 2 :
00270                         return "0" + ss.str();
00271                 case 3 :
00272                         return ss.str();
00273         }
00274 
00275         return ss.str();//return a string with the contents of the stream
00276 }
00277 


iri_my_database_publ
Author(s): Gonzalo Ferrer
autogenerated on Fri Dec 6 2013 23:31:04