contrib/brl/vvid/vvid_command_line_player.cxx
Go to the documentation of this file.
00001 // This is brl/vvid/vvid_command_line_player.cxx
00002 #include "vvid_command_line_player.h"
00003 //:
00004 // \file
00005 // \author D.E.Crispell
00006 
00007 #include <vcl_iostream.h>
00008 #include <vcl_string.h>
00009 #include <vcl_sstream.h>
00010 #include <vcl_fstream.h>
00011 #include <vcl_vector.h>
00012 #include <vil1/vil1_image.h>
00013 #include <vil1/vil1_memory_image_of.h>
00014 #include <vpro/vpro_video_process.h>
00015 #include <vidl_vil1/vidl_vil1_movie.h>
00016 #include <vidl_vil1/vidl_vil1_io.h>
00017 #include <vidl_vil1/vidl_vil1_frame.h>
00018 
00019 //: Default Constructor
00020 vvid_command_line_player::vvid_command_line_player()
00021 {
00022    movie_=(vidl_vil1_movie*)0;
00023    video_process_ = 0;
00024    status_output_filename_ = "";
00025 
00026    parameter_output_file_ = new vul_arg<vcl_string>(arg_list_,"-X","parameter output file","");
00027    input_video_file_ = new vul_arg<vcl_string>(arg_list_,"-V","video input file","");
00028    status_block_file_ = new vul_arg<vcl_string>(arg_list_,"-S","status block file","");
00029    performance_output_file_ = new vul_arg<vcl_string>(arg_list_,"-P","performance output file","");
00030    output_directory_ = new vul_arg<vcl_string>(arg_list_,"-O","output directory","");
00031    print_params_flag_ = new vul_arg<bool>(arg_list_,"-print_params_only",
00032                                           "print xml parameter file and exit",false);
00033 }
00034 
00035 //: Destructor
00036 vvid_command_line_player::~vvid_command_line_player()
00037 {
00038   delete parameter_output_file_;
00039   delete input_video_file_;
00040   delete status_block_file_;
00041   delete output_directory_;
00042   delete print_params_flag_;
00043 }
00044 
00045 //: load a video
00046 bool vvid_command_line_player::load_video_file()//vcl_string filename)
00047 {
00048   vcl_string filename = (*input_video_file_)();
00049   movie_ = vidl_vil1_io::load_movie(filename.c_str());
00050   if (!movie_)
00051   {
00052     vcl_cerr << "Failed to load movie file\n";
00053     return false;
00054   }
00055   vidl_vil1_movie::frame_iterator pframe = movie_->last();
00056   nframes_ = pframe->get_real_frame_index();
00057   return true;
00058 }
00059 
00060 //: set video process
00061 bool vvid_command_line_player::set_video_process(vpro_video_process_sptr video_proc)
00062 {
00063   video_process_ = video_proc;
00064   return true;
00065 }
00066 
00067 
00068 //: add the required SYSTEM_INFO args to the list of args.
00069 // Algorithm-specific args can then be added to the list and parsed.
00070 bool vvid_command_line_player::add_system_info_args(vul_arg_info_list& arg_list)
00071 {
00072   arg_list.add(parameter_output_file_);
00073   arg_list.add(input_video_file_);
00074   arg_list.add(status_block_file_);
00075   arg_list.add(output_directory_);
00076   arg_list.add(performance_output_file_);
00077   arg_list.add(print_params_flag_);
00078 
00079   return true;
00080 }
00081 
00082 //: play the video
00083 bool vvid_command_line_player::play_video()
00084 {
00085   if (!(video_process_ && movie_))
00086   {
00087     vcl_cerr << "Video Process or Movie not loaded\n";
00088     return false;
00089   }
00090   for (vidl_vil1_movie::frame_iterator pframe=movie_->begin();
00091        pframe!=movie_->end();
00092        ++pframe)
00093   {
00094     vcl_cout << "frame["<< pframe->get_real_frame_index()<<"]\n";
00095     vil1_image img = pframe->get_image();
00096 
00097     vil1_memory_image_of<unsigned char> image(img);
00098 
00099     // note - it is up to the video process to call clear_input() after each frame
00100     video_process_->add_input_image(image);
00101 
00102     if (video_process_->execute())
00103     {
00104       //if (video_process_->get_output_type()==vpro_video_process::SPATIAL_OBJECT)
00105 
00106       //else if (video_process_->get_output_type()==vpro_video_process::IMAGE)
00107 
00108       //else if (video_process_->get_output_type()==vpro_video_process::TOPOLOGY)
00109     }
00110     // write status block
00111     if ((*status_block_file_)() != "")
00112         write_status((*status_block_file_)(), pframe->get_real_frame_index());
00113   }
00114   return true;
00115 }
00116 
00117 //: write status block to file
00118 // \todo each video process should be able to have its own status_block type
00119 void vvid_command_line_player::write_status(vcl_string output_file, int iframe)
00120 {
00121   float percent_done = 0;
00122   if (nframes_)
00123     percent_done = float(iframe)/float(nframes_);
00124 
00125   vcl_ofstream outstream(output_file.c_str());
00126   outstream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
00127             << "<status>\n"
00128             << "  <basic>\n"
00129             << "    <info percent_completed=\""<<percent_done<<"\"/>\n"
00130             << "  </basic>\n"
00131             << "  <optional>\n"
00132             << "    <info number_of_frames_processed=\""<<iframe<<"\"/>\n"
00133             << "    <info total_number_of_frames=\""<<nframes_<<"\"/>\n"
00134             << "  </optional>\n"
00135             << "</status>\n";
00136 
00137   outstream.close();
00138 }
00139 
00140 //: so the wrapper program can exit if the user wants param file only
00141 bool vvid_command_line_player::print_params_only()
00142 {
00143   return (*print_params_flag_)();
00144 }
00145 
00146 bool vvid_command_line_player::add_output_file(vul_arg<vcl_string> output_file)
00147 {
00148   output_files_.push_back(output_file);
00149   return true;
00150 }
00151 
00152 bool vvid_command_line_player::print_performance_output(vcl_string video_name,
00153                                                         vcl_vector<float> frame_scores)
00154 {
00155   vcl_cout << "printing performance\n";
00156   vcl_string filename = (*performance_output_file_)();
00157   if (filename == "")
00158     return false;
00159   vcl_ofstream outstream(filename.c_str());
00160   if (!outstream)
00161   {
00162     vcl_cerr << "error: could not create performance output file ["<<filename<<"]\n";
00163     return false;
00164   }
00165   outstream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
00166             << "<performance>\n"
00167             << "  <description>Video Frame Scores</description>\n"
00168             << "  <frames>\n"
00169             << "    <video name=\""<<video_name<<"\" totalframes=\""<<frame_scores.size()<<"\">\n";
00170   int frame_idx = 0;
00171   for (vcl_vector<float>::iterator fit=frame_scores.begin(); fit != frame_scores.end(); fit++)
00172   {
00173     outstream << "      <frame index=\""<<frame_idx++<<"\" score=\""<<*fit<<"\"/>\n";
00174   }
00175   outstream << "    </video>\n"
00176             << "  </frames>\n"
00177             << "</performance>\n";
00178 
00179   outstream.close();
00180   return true;
00181 }
00182 
00183 bool vvid_command_line_player::print_xml_params(vul_arg_info_list& arg_list,
00184                                                 vcl_string param_block_name)
00185 {
00186   vcl_string filename = (*parameter_output_file_)();
00187   if (filename == "")
00188     return false;
00189 
00190   vcl_ofstream outstream(filename.c_str());
00191   if (!outstream)
00192   {
00193     vcl_cerr << "error: could not create param output file ["<<filename<<"]\n";
00194     return false;
00195   }
00196   int n_args = arg_list.args_.size();
00197 
00198   outstream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
00199             << "<vxl>\n"
00200             << "  <params app=\""<<param_block_name<<"\" nparams=\""<<n_args<<"\">\n";
00201 
00202   for (vcl_vector<vul_arg_base*>::iterator arg_it = arg_list.args_.begin();
00203        arg_it != arg_list.args_.end(); arg_it++)
00204   {
00205     vul_arg_base *arg = *arg_it;
00206     vcl_string command = arg->option();
00207     vcl_string description = arg->help();
00208     vcl_string type = arg->type_;
00209     if (type == "bool")
00210       type = "flag";
00211 
00212     outstream << "    <param command=\"" << command << "\" description=\""
00213               << description << "\" type=\"" << type << '\"';
00214     if (command == "-V")
00215       outstream << " System_info=\"INPUT_VIDEO\"";
00216     if (command == "-O")
00217       outstream << " System_info=\"OUTPUT_DIRECTORY\"";
00218     if (command == "-S")
00219       outstream << " System_info=\"STATUS_BLOCK\"";
00220     if (command == "-P")
00221       outstream << " System_info=\"PERFORMANCE_OUTPUT\"";
00222     // see if param is an output file
00223     for (vcl_vector<vul_arg<vcl_string> >::iterator pit = output_files_.begin();
00224          pit != output_files_.end(); pit++)
00225     {
00226       vcl_string of_cmd = ( (*pit).option() );
00227         if (command == of_cmd)
00228         {
00229           outstream << " System_info=\"OUTPUT\"";
00230           break;
00231         }
00232     }
00233     outstream << " value=\"";
00234     // if arg is a string, we have to get rid of ' ' around value
00235     vcl_ostringstream value_stream;
00236     arg->print_value(value_stream);
00237     if (type == "string")
00238     {
00239       vcl_string value_string = value_stream.str();
00240       value_string.replace(value_string.find("'"),1,"");
00241       value_string.replace(value_string.find("'"),1,"");
00242       outstream << value_string;
00243     }
00244     else if (type == "flag")
00245     {
00246       vcl_string value_string = value_stream.str();
00247       if ((value_string == "not set") || (command == "-print_params_only"))
00248         value_string = "off";
00249       else if (value_string == "set")
00250         value_string = "on";
00251       else
00252         value_string = "unknown";
00253       outstream << value_string;
00254     }
00255     else
00256       outstream << value_stream.str();
00257     outstream << "\" />\n";
00258   }
00259   outstream << "  </params>\n"
00260             << "</vxl>\n";
00261 
00262   outstream.close();
00263   return true;
00264 }