00001 /*!@file AppDevices/test-multigrab.C Test multiple frame grabbing and X display */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // 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: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppDevices/test-multigrab.C $ 00035 // $Id: test-multigrab.C 14321 2010-12-20 23:05:26Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Devices/DeviceOpts.H" 00040 #include "Devices/FrameGrabberConfigurator.H" 00041 #include "Devices/FrameGrabberFactory.H" 00042 #include "GUI/XWindow.H" 00043 #include "Image/DrawOps.H" 00044 #include "Image/Image.H" 00045 #include "Image/Pixels.H" 00046 #include "Util/Timer.H" 00047 #include "Util/log.H" 00048 #include "Util/sformat.H" 00049 00050 #include <vector> 00051 #include <cstdio> 00052 00053 #define NAVG 20 00054 00055 /*! This simple executable tests multiple video frame grabbing through 00056 either the video4linux2 driver (see V4L2grabber.H; when you specify 00057 --fg-type=V4L2 and then give the pathnames of V4L2 devices as 00058 arguments), or IEEE1394 (see IEEE1394grabber.H; when you specify 00059 --fg-type=1394 and pass the camera (subchannel) numbers as 00060 arguments). */ 00061 int main(const int argc, const char **argv) 00062 { 00063 // instantiate a model manager: 00064 ModelManager manager("Multi Frame Grabber Tester"); 00065 00066 // Instantiate our various ModelComponents: We instantiate a 00067 // FrameGrabberConfigurator so that people can select the type and 00068 // set some options. Then, however, we are going to instantiate a 00069 // bunch of grabbers, as per the command-line argument. We will then 00070 // remove our FrameGrabberConfigurator and its baby, as it was just 00071 // a placeholder to allow command-line configuration to take place: 00072 nub::soft_ref<FrameGrabberConfigurator> 00073 gbc(new FrameGrabberConfigurator(manager)); 00074 manager.addSubComponent(gbc); 00075 00076 // decide on which command-line options our model should export: 00077 manager.exportOptions(MC_RECURSE); 00078 00079 // Parse command-line: 00080 if (manager.parseCommandLine(argc, argv, "<dev1> ... <devN>", 1, -1) == false) return(1); 00081 00082 // do post-command-line configs: 00083 int ngb = manager.numExtraArgs(); 00084 00085 // what grabber type did we end up with? 00086 std::string typ = manager.getOptionValString(&OPT_FrameGrabberType); 00087 bool fire = false; if (typ.compare("1394") == 0) fire = true; 00088 00089 // instantiate a bunch of grabbers: 00090 std::vector< nub::soft_ref<FrameIstream> > gb; 00091 for (int i = 0; i < ngb; i ++) 00092 { 00093 const std::string name = sformat("grabber%03d", i); 00094 00095 if (fire) 00096 { 00097 // instantiate a grabber. Note: We cancel the default USE_MY_VALS so that our grabber will inherit whichever 00098 // settings were specified at the command line: 00099 gb.push_back(nub::soft_ref<FrameIstream>(makeIEEE1394grabber(manager, name, name, 0))); 00100 manager.addSubComponent(gb[i]); 00101 gb[i]->exportOptions(MC_RECURSE); 00102 gb[i]->setModelParamString("FrameGrabberSubChan", manager.getExtraArg(i)); 00103 gb[i]->setModelParamVal("FrameGrabberNbuf", 10); 00104 } 00105 else 00106 { 00107 // instantiate a grabber. Note: We cancel the default USE_MY_VALS so that our grabber will inherit whichever 00108 // settings were specified at the command line: 00109 gb.push_back(nub::soft_ref<FrameIstream>(makeV4L2grabber(manager, name, name, 0))); 00110 manager.addSubComponent(gb[i]); 00111 gb[i]->exportOptions(MC_RECURSE); 00112 gb[i]->setModelParamVal("FrameGrabberDevice", manager.getExtraArg(i)); 00113 } 00114 } 00115 00116 // we don't need the grabber that was in our configurator anymore: 00117 manager.removeSubComponent(*gbc); 00118 gbc.reset(NULL); // fully de-allocate the object and its children 00119 00120 // get a window ready: 00121 XWindow win(Dims(ngb * gb[0]->getWidth(), gb[0]->getHeight()), 00122 -1, -1, "test-multigrab window"); 00123 00124 // let's get all our ModelComponent instances started: 00125 manager.start(); 00126 00127 // get the frame grabbers to start streaming: 00128 for (int i = 0; i < ngb; i ++) gb[i]->startStream(); 00129 00130 // get ready for main loop: 00131 Timer tim; uint64 t[NAVG]; int frame = 0; 00132 while(1) { 00133 tim.reset(); 00134 00135 int offx = 0; 00136 for (int i = 0; i < ngb; i ++) 00137 { 00138 Image< PixRGB<byte> > ima = gb[i]->readRGB(); 00139 win.drawImage(ima, offx, 0); 00140 offx += gb[i]->getWidth(); 00141 } 00142 00143 // total time for multiple grabs and display: 00144 t[frame % NAVG] = tim.get(); 00145 00146 // compute and show framerate over the last NAVG frames: 00147 if (frame % NAVG == 0 && frame > 0) 00148 { 00149 uint64 avg = 0ULL; for (int i = 0; i < NAVG; i ++) avg += t[i]; 00150 float avg2 = 1000.0F / float(avg) * float(NAVG); 00151 printf("Framerate for %d grab+display: %.1f fps\n", ngb, avg2); 00152 } 00153 frame ++; 00154 } 00155 00156 // stop all our ModelComponents 00157 manager.stop(); 00158 00159 // all done! 00160 puts("All done!"); 00161 return 0; 00162 } 00163 00164 // ###################################################################### 00165 /* So things look consistent in everyone's emacs... */ 00166 /* Local Variables: */ 00167 /* indent-tabs-mode: nil */ 00168 /* End: */