00001 /*!@file Parallel/beograb-master.C Grab video & save PNM frames on beowulf 00002 local disks */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Parallel/beograb-master.C $ 00036 // $Id: beograb-master.C 12074 2009-11-24 07:51:51Z itti $ 00037 // 00038 00039 #include "Beowulf/Beowulf.H" 00040 #include "Component/ModelManager.H" 00041 #include "Devices/FrameGrabberConfigurator.H" 00042 #include "GUI/XWindow.H" 00043 #include "Image/DrawOps.H" 00044 #include "Image/Image.H" 00045 #include "Image/Pixels.H" 00046 #include "Image/Pixels.H" 00047 #include "Transport/FrameIstream.H" 00048 #include "Util/Timer.H" 00049 #include "Util/log.H" 00050 00051 #include <iostream> 00052 #include <signal.h> 00053 #include <unistd.h> 00054 #include <cstdio> 00055 00056 static bool goforever = true; 00057 void terminate(int s) 00058 { LERROR("*** INTERRUPT ***"); goforever = false; exit(1); } 00059 00060 // ###################################################################### 00061 int main(const int argc, const char **argv) 00062 { 00063 // suppress LDEBUG messages: 00064 MYLOGVERB = LOG_INFO; 00065 00066 // instantiate a model manager: 00067 ModelManager manager("Beowulf-based FrameGrabber"); 00068 00069 // Instantiate our various ModelComponents: 00070 nub::soft_ref<FrameGrabberConfigurator> 00071 gbc(new FrameGrabberConfigurator(manager)); 00072 manager.addSubComponent(gbc); 00073 00074 nub::soft_ref<Beowulf> 00075 beo(new Beowulf(manager, "Beowulf Master", "BeowulfMaster", true)); 00076 manager.addSubComponent(beo); 00077 00078 // Parse command-line: 00079 if (manager.parseCommandLine(argc, argv, "<framepause>", 1, 1) == false) 00080 return(1); 00081 00082 // do post-command-line configs: 00083 nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber(); 00084 if (gb.isInvalid()) 00085 LFATAL("You need to select a frame grabber type via the " 00086 "--fg-type=XX command-line option for this program " 00087 "to be useful"); 00088 int framepause = manager.getExtraArgAs<int>(0); 00089 if (framepause < 5 || framepause > 2048) 00090 LFATAL("Invalid framepause %d [range 5..2048]", framepause); 00091 00092 // let's get all our ModelComponent instances started: 00093 manager.start(); 00094 00095 // catch signals and redirect them to terminate for clean exit: 00096 signal(SIGHUP, terminate); signal(SIGINT, terminate); 00097 signal(SIGQUIT, terminate); signal(SIGTERM, terminate); 00098 signal(SIGALRM, terminate); 00099 00100 // get prepared to grab, communicate, display, etc: 00101 XWindow xw(gb->peekDims(), -1, -1, "USC iLab Beograb"); 00102 int32 frame = 0; // count the frames 00103 Timer tim; // for computation of framerate & pause 00104 TCPmessage smsg; // buffer to send messages to nodes 00105 Image< PixRGB<byte> > image; // image grabbed and displayed 00106 const int nbnode = beo->getNbSlaves(); // number of slave nodes 00107 00108 std::cerr << "***** READY. Press [RETURN] to start grabbing *****\n"; 00109 getchar(); 00110 00111 // ########## MAIN LOOP: grab, process, display: 00112 while(goforever) 00113 { 00114 // initialize timer: 00115 tim.reset(); 00116 00117 // grab image: 00118 image = gb->readRGB(); 00119 00120 // display total time spent on this frame: 00121 if (tim.get() > 34) 00122 LINFO("*** Warning: grabbing %d took %llums", frame, tim.get()); 00123 00124 // display image: 00125 xw.drawImage(image); 00126 00127 // send image to slave node, depending on frame number: 00128 smsg.reset(frame, 1); 00129 smsg.addImage(image); 00130 beo->send(frame % nbnode, smsg); 00131 00132 // display total time spent on this frame: 00133 if (tim.get() > 34) 00134 LINFO("*** Warning: frame %d took %llums", frame, tim.get()); 00135 00136 // if necessary, sleep for a while until fixed amount of time elapsed: 00137 while(int(tim.get()) < framepause) 00138 { 00139 struct timespec ts, ts2; 00140 ts.tv_sec = 0; ts.tv_nsec = 1000000; // sleep 1 ms 00141 nanosleep(&ts, &ts2); 00142 } 00143 00144 // ready for next frame: 00145 frame++; 00146 } 00147 00148 // stop all our ModelComponents 00149 manager.stop(); 00150 00151 exit(0); 00152 } 00153 00154 // ###################################################################### 00155 /* So things look consistent in everyone's emacs... */ 00156 /* Local Variables: */ 00157 /* indent-tabs-mode: nil */ 00158 /* End: */