00001 /*!@file Transport/InfoOutputSeries.C Very simple output source that just prints basic info about the frames it receives */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Transport/InfoOutputSeries.C $ 00035 // $Id: InfoOutputSeries.C 8864 2007-10-19 19:11:17Z rjpeters $ 00036 // 00037 00038 #ifndef TRANSPORT_INFOOUTPUTSERIES_C_DEFINED 00039 #define TRANSPORT_INFOOUTPUTSERIES_C_DEFINED 00040 00041 #include "Transport/InfoOutputSeries.H" 00042 00043 #include "Component/GlobalOpts.H" 00044 #include "Image/Dims.H" 00045 #include "Raster/GenericFrame.H" 00046 #include "Util/FileUtil.H" 00047 #include "Util/log.H" 00048 #include "rutz/time.h" 00049 00050 #include <map> 00051 00052 // ###################################################################### 00053 struct InfoOutputSeries::Impl 00054 { 00055 struct ChanInfo 00056 { 00057 ChanInfo() : spec(), nframes(0) {} 00058 00059 GenericFrameSpec spec; 00060 int nframes; 00061 rutz::time first_time; 00062 rutz::time last_time; 00063 }; 00064 00065 typedef std::map<std::string, ChanInfo> map_type; 00066 00067 map_type infoMap; 00068 std::string fileName; 00069 }; 00070 00071 // ###################################################################### 00072 InfoOutputSeries::InfoOutputSeries(OptionManager& mgr) 00073 : 00074 FrameOstream(mgr, "InfoOutputSeries", "Info Output Series"), 00075 itsTestMode(&OPT_TestMode, this), 00076 rep(new Impl) 00077 {} 00078 00079 // ###################################################################### 00080 InfoOutputSeries::~InfoOutputSeries() 00081 { 00082 delete rep; 00083 } 00084 00085 // ###################################################################### 00086 void InfoOutputSeries::setConfigInfo(const std::string& filename) 00087 { 00088 // NOTE: if you modify any behavior here, then please update the 00089 // corresponding documentation for the global "--out" option inside 00090 // the OPT_OutputFrameSink definition in Media/MediaOpts.C 00091 00092 this->setFileName(filename); 00093 } 00094 00095 // ###################################################################### 00096 void InfoOutputSeries::writeFrame(const GenericFrame& frame, 00097 const std::string& shortname, 00098 const FrameInfo& auxinfo) 00099 { 00100 const rutz::time t = rutz::time::wall_clock_now(); 00101 00102 Impl::ChanInfo& info = rep->infoMap[shortname]; 00103 00104 info.spec = frame.frameSpec(); 00105 if (info.nframes == 0) 00106 info.first_time = t; 00107 info.last_time = t; 00108 info.nframes++; 00109 } 00110 00111 // ###################################################################### 00112 void InfoOutputSeries::stop2() 00113 { 00114 try 00115 { 00116 bool owned = false; 00117 FILE* f = stdOutputFileOpen(rep->fileName, &owned); // may throw 00118 00119 for (Impl::map_type::const_iterator 00120 itr = rep->infoMap.begin(), 00121 stop = rep->infoMap.end(); 00122 itr != stop; ++itr) 00123 { 00124 const double secs = 00125 ((*itr).second.last_time - (*itr).second.first_time).sec(); 00126 00127 const double fps = 00128 (*itr).second.nframes >= 2 00129 ? ((*itr).second.nframes - 1) / secs 00130 : 0.0; 00131 00132 if (itsTestMode.getVal()) 00133 fprintf(f, "InfoOutputSeries: summary[%s]: %d frames (%s)\n", 00134 (*itr).first.c_str(), 00135 (*itr).second.nframes, 00136 (*itr).second.spec.getDescription().c_str()); 00137 else 00138 fprintf(f, "InfoOutputSeries: summary[%s]: %d frames @ %.2ffps (%s)\n", 00139 (*itr).first.c_str(), 00140 (*itr).second.nframes, 00141 fps, 00142 (*itr).second.spec.getDescription().c_str()); 00143 } 00144 00145 fflush(f); 00146 00147 if (owned) 00148 fclose(f); 00149 } 00150 catch (...) 00151 { 00152 // don't propagate exceptions here since we are potentially 00153 // inside a destructor chain 00154 } 00155 } 00156 00157 // ###################################################################### 00158 void InfoOutputSeries::closeStream(const std::string& shortname) 00159 { 00160 Impl::map_type::iterator itr = rep->infoMap.find(shortname); 00161 if (itr != rep->infoMap.end()) 00162 rep->infoMap.erase(itr); 00163 } 00164 00165 // ###################################################################### 00166 void InfoOutputSeries::setFileName(const std::string& filename) 00167 { 00168 rep->fileName = filename; 00169 } 00170 00171 // ###################################################################### 00172 /* So things look consistent in everyone's emacs... */ 00173 /* Local Variables: */ 00174 /* mode: c++ */ 00175 /* indent-tabs-mode: nil */ 00176 /* End: */ 00177 00178 #endif // TRANSPORT_INFOOUTPUTSERIES_C_DEFINED