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 bool vvid_command_line_player::print_xml_params(vul_arg_info_list& arg_list,
00183                                                 vcl_string param_block_name)
00184 {
00185   vcl_string filename = (*parameter_output_file_)();
00186   if (filename == "")
00187     return false;
00188 
00189   vcl_ofstream outstream(filename.c_str());
00190   if (!outstream)
00191   {
00192     vcl_cerr << "error: could not create param output file ["<<filename<<"]\n";
00193     return false;
00194   }
00195   int n_args = arg_list.args_.size();
00196 
00197   outstream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
00198             << "<vxl>\n"
00199             << "  <params app=\""<<param_block_name<<"\" nparams=\""<<n_args<<"\">\n";
00200 
00201   for (vcl_vector<vul_arg_base*>::iterator arg_it = arg_list.args_.begin();
00202        arg_it != arg_list.args_.end(); arg_it++)
00203   {
00204     vul_arg_base *arg = *arg_it;
00205     vcl_string command = arg->option();
00206     vcl_string description = arg->help();
00207     vcl_string type = arg->type_;
00208     if (type == "bool")
00209       type = "flag";
00210 
00211     outstream << "    <param command=\"" << command << "\" description=\""
00212               << description << "\" type=\"" << type << '\"';
00213     if (command == "-V")
00214       outstream << " System_info=\"INPUT_VIDEO\"";
00215     if (command == "-O")
00216       outstream << " System_info=\"OUTPUT_DIRECTORY\"";
00217     if (command == "-S")
00218       outstream << " System_info=\"STATUS_BLOCK\"";
00219     if (command == "-P")
00220       outstream << " System_info=\"PERFORMANCE_OUTPUT\"";
00221     // see if param is an output file
00222     for (vcl_vector<vul_arg<vcl_string> >::iterator pit = output_files_.begin();
00223          pit != output_files_.end(); pit++)
00224     {
00225       vcl_string of_cmd = ( (*pit).option() );
00226         if (command == of_cmd)
00227         {
00228           outstream << " System_info=\"OUTPUT\"";
00229           break;
00230         }
00231     }
00232     outstream << " value=\"";
00233     // if arg is a string, we have to get rid of ' ' around value
00234     vcl_ostringstream value_stream;
00235     arg->print_value(value_stream);
00236     if (type == "string")
00237     {
00238       vcl_string value_string = value_stream.str();
00239       value_string.replace(value_string.find("'"),1,"");
00240       value_string.replace(value_string.find("'"),1,"");
00241       outstream << value_string;
00242     }
00243     else if (type == "flag")
00244     {
00245       vcl_string value_string = value_stream.str();
00246       if ((value_string == "not set") || (command == "-print_params_only"))
00247         value_string = "off";
00248       else if (value_string == "set")
00249         value_string = "on";
00250       else
00251         value_string = "unknown";
00252       outstream << value_string;
00253     }
00254     else
00255       outstream << value_stream.str();
00256     outstream << "\" />\n";
00257   }
00258   outstream << "  </params>\n"
00259             << "</vxl>\n";
00260 
00261   outstream.close();
00262   return true;
00263 }