psycho-overlay.C

Go to the documentation of this file.
00001 /*!@file AppPsycho/psycho-overlay.C Psychophysics display of movies */
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/AppPsycho/psycho-overlay.C $
00035 // $Id: psycho-overlay.C 13712 2010-07-28 21:00:40Z itti $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Image/Image.H"
00040 #include "Media/MPEGStream.H"
00041 #include "Media/MediaOpts.H"
00042 #include "Psycho/PsychoDisplay.H"
00043 #include "Psycho/EyeTrackerConfigurator.H"
00044 #include "Psycho/EyeTracker.H"
00045 #include "Psycho/PsychoOpts.H"
00046 #include "Raster/Raster.H"
00047 #include "Component/EventLog.H"
00048 #include "Component/ComponentOpts.H"
00049 #include "Util/MathFunctions.H"
00050 #include "Util/Types.H"
00051 #include "Video/VideoFrame.H"
00052 #include "rutz/time.h"
00053 
00054 #include <deque>
00055 
00056 #define CACHELEN 150
00057 
00058 // ######################################################################
00059 static bool cacheFrame(nub::soft_ref<InputMPEGStream>& mp,
00060                        std::deque<VideoFrame>& cache)
00061 {
00062   const VideoFrame frame = mp->readVideoFrame();
00063   if (!frame.initialized()) return false; // end of stream
00064 
00065   cache.push_front(frame);
00066   return true;
00067 }
00068 
00069 // ######################################################################
00070 static int submain(const int argc, char** argv)
00071 {
00072   MYLOGVERB = LOG_INFO;  // suppress debug messages
00073 
00074   // Instantiate a ModelManager:
00075   ModelManager manager("Psycho Movie");
00076 
00077   // Instantiate our various ModelComponents:
00078   nub::soft_ref<InputMPEGStream> mp
00079     (new InputMPEGStream(manager, "Input MPEG Stream", "InputMPEGStream"));
00080   manager.addSubComponent(mp);
00081 
00082   nub::soft_ref<EventLog> el(new EventLog(manager));
00083   manager.addSubComponent(el);
00084 
00085   nub::soft_ref<EyeTrackerConfigurator>
00086     etc(new EyeTrackerConfigurator(manager));
00087   manager.addSubComponent(etc);
00088 
00089   nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00090   manager.addSubComponent(d);
00091 
00092   manager.setOptionValString(&OPT_InputMPEGStreamPreload, "true");
00093   manager.setOptionValString(&OPT_EventLogFileName, "psychodata.psy");
00094   manager.setOptionValString(&OPT_EyeTrackerType, "ISCAN");
00095 
00096   // Parse command-line:
00097   if (manager.parseCommandLine(argc, argv,
00098                                "<movie1.mpg> <overlay_image1>..."
00099                                "<movieN.mpg> <overlay_imageN>", 1, -1)==false)
00100     return(1);
00101 
00102   // hook our various babies up and do post-command-line configs:
00103   nub::soft_ref<EyeTracker> et = etc->getET();
00104   d->setEyeTracker(et);
00105   d->setEventLog(el);
00106   et->setEventLog(el);
00107 
00108   // EyeLink opens the screen for us, so make sure SDLdisplay is slave:
00109   if (etc->getModelParamString("EyeTrackerType").compare("EL") == 0)
00110     d->setModelParamVal("SDLslaveMode", true);
00111 
00112   // let's get all our ModelComponent instances started:
00113   manager.start();
00114 
00115   // let's do an eye tracker calibration:
00116   et->calibrate(d);
00117   d->clearScreen();
00118   d->displayText("<SPACE> for random play; other key for ordered");
00119   int c = d->waitForKey(true);
00120 
00121   // setup array of movie indices:
00122   if (manager.numExtraArgs() % 2 != 0)
00123     LFATAL("You need to have an overlay image for every movie!");
00124 
00125   uint nbmovies = manager.numExtraArgs() / 2;
00126   int index[nbmovies];
00127   int imgindex[nbmovies];
00128   for (uint i = 0; i < nbmovies; i ++)
00129     {
00130       index[i] = i;
00131       imgindex[i] = i;
00132     }
00133 
00134   if (c == ' ')
00135     {
00136       LINFO("Randomizing movies and images...");
00137       randShuffle(index,nbmovies);
00138       randShuffle(imgindex,nbmovies);
00139     }
00140 
00141   // main loop:
00142   for (uint i = 0; i < nbmovies; i ++)
00143     {
00144       d->clearScreen();
00145 
00146       //get our image to be overlayed
00147       LINFO("Loading %s", manager.getExtraArg(imgindex[i]*2+1).c_str());
00148       Image<PixRGB<byte> > cimage =
00149         Raster::ReadRGB(manager.getExtraArg(imgindex[i]*2+1));
00150       //transparent pixel should be in upper left hand corner
00151       PixRGB<byte> currTransPixel = cimage[0];
00152 
00153       // cache initial movie frames:
00154       bool streaming = true;
00155       LINFO("Buffering '%s'...", manager.getExtraArg(index[i]*2).c_str());
00156       mp->setFileName(manager.getExtraArg(index[i]*2));
00157 
00158       std::deque<VideoFrame> cache;
00159       for (uint j = 0; j < CACHELEN; j ++)
00160         {
00161           streaming = cacheFrame(mp, cache);
00162           if (streaming == false) break;  // all movie frames got cached!
00163         }
00164       LINFO("'%s' ready.", manager.getExtraArg(index[i]*2).c_str());
00165 
00166       // give a chance to other processes (useful on single-CPU machines):
00167       sleep(1); if (system("/bin/sync")) LFATAL("sync failed");
00168 
00169       // display fixation to indicate that we are ready:
00170       d->displayFixation();
00171 
00172       // ready to go whenever the user is ready:
00173       d->waitForKey(true); int frame = 0;
00174       d->waitNextRequestedVsync(false, true);
00175       d->pushEvent(std::string("===== Playing movie: ") +
00176                    manager.getExtraArg(index[i]) + " =====");
00177 
00178       // start the eye tracker:
00179       et->track(true);
00180 
00181       // blink the fixation:
00182       d->displayFixationBlink();
00183 
00184       // create an overlay:
00185       d->createVideoOverlay(VIDFMT_YUV420P,mp->getWidth(),mp->getHeight()); // mpeg stream returns YUV420P
00186 
00187       // play the movie:
00188       rutz::time start = rutz::time::wall_clock_now();
00189       while(cache.size())
00190         {
00191           // let's first cache one more frame:
00192           if (streaming) streaming = cacheFrame(mp, cache);
00193 
00194           // get next frame to display and put it into our overlay:
00195           VideoFrame vidframe = cache.back();
00196           d->displayVideoOverlay_image(vidframe, frame,
00197                                        SDLdisplay::NEXT_VSYNC,
00198                                        cimage, currTransPixel,4);
00199           cache.pop_back();
00200 
00201           ++frame;
00202         }
00203       rutz::time stop = rutz::time::wall_clock_now();
00204       const double secs = (stop-start).sec();
00205       LINFO("%d frames in %.02f sec (~%.02f fps)", frame, secs, frame/secs);
00206 
00207       // destroy the overlay. Somehow, mixing overlay displays and
00208       // normal displays does not work. With a single overlay created
00209       // before this loop and never destroyed, the first movie plays
00210       // ok but the other ones don't show up:
00211       d->destroyYUVoverlay();
00212       d->clearScreen();  // sometimes 2 clearScreen() are necessary
00213 
00214       // stop the eye tracker:
00215       usleep(50000);
00216       et->track(false);
00217 
00218       // let's do a quickie eye tracker recalibration:
00219       et->recalibrate(d,13);
00220     }
00221 
00222   d->clearScreen();
00223   d->displayText("Experiment complete. Thank you!");
00224   d->waitForKey(true);
00225 
00226   // stop all our ModelComponents
00227   manager.stop();
00228 
00229   // all done!
00230   return 0;
00231 }
00232 
00233 // ######################################################################
00234 extern "C" int main(const int argc, char** argv)
00235 {
00236   // simple wrapper around submain() to catch exceptions (because we
00237   // want to allow PsychoDisplay to shut down cleanly; otherwise if we
00238   // abort while SDL is in fullscreen mode, the X server won't return
00239   // to its original resolution)
00240   try
00241     {
00242       return submain(argc, argv);
00243     }
00244   catch (...)
00245     {
00246       REPORT_CURRENT_EXCEPTION;
00247     }
00248 
00249   return 1;
00250 }
00251 
00252 // ######################################################################
00253 /* So things look consistent in everyone's emacs... */
00254 /* Local Variables: */
00255 /* indent-tabs-mode: nil */
00256 /* End: */
Generated on Sun May 8 08:40:09 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3