00001 /*!@file AppPsycho/psycho-search.C Psychophysics display for a search for a 00002 target that is presented to the observer prior to the search */ 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/AppPsycho/psycho-searchGabor.C $ 00036 // $Id: psycho-searchGabor.C 10794 2009-02-08 06:21:09Z itti $ 00037 // 00038 00039 #include "Component/ModelManager.H" 00040 #include "Image/ColorOps.H" // for makeRGB() 00041 #include "Image/CutPaste.H" // for inplacePaste() 00042 #include "Image/Image.H" 00043 #include "Image/MathOps.H" // for inplaceSpeckleNoise() 00044 #include "Psycho/PsychoDisplay.H" 00045 #include "Psycho/EyeTrackerConfigurator.H" 00046 #include "Psycho/EyeTracker.H" 00047 #include "Psycho/PsychoOpts.H" 00048 #include "Component/EventLog.H" 00049 #include "Component/ComponentOpts.H" 00050 #include "Raster/Raster.H" 00051 #include "Util/MathFunctions.H" 00052 #include "Util/StringUtil.H" 00053 00054 #include <ctype.h> 00055 #include <vector> 00056 #include <string> 00057 #include <fstream> 00058 00059 using namespace std; 00060 00061 //! number of frames in the mask 00062 #define NMASK 10 00063 00064 // ###################################################################### 00065 static int submain(const int argc, char** argv) 00066 { 00067 MYLOGVERB = LOG_INFO; // suppress debug messages 00068 00069 // Instantiate a ModelManager: 00070 ModelManager manager("Psycho Search"); 00071 00072 // Instantiate our various ModelComponents: 00073 nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager)); 00074 manager.addSubComponent(d); 00075 00076 nub::soft_ref<EyeTrackerConfigurator> 00077 etc(new EyeTrackerConfigurator(manager)); 00078 manager.addSubComponent(etc); 00079 00080 nub::soft_ref<EventLog> el(new EventLog(manager)); 00081 manager.addSubComponent(el); 00082 00083 manager.setOptionValString(&OPT_EventLogFileName, "psychodata.psy"); 00084 manager.setOptionValString(&OPT_EyeTrackerType, "ISCAN"); 00085 00086 // Parse command-line: 00087 if (manager.parseCommandLine(argc, argv, "<imagelist.txt>", 1, 1) == false) 00088 return(1); 00089 00090 // hook our various babies up and do post-command-line configs: 00091 nub::soft_ref<EyeTracker> et = etc->getET(); 00092 d->setEyeTracker(et); 00093 d->setEventLog(el); 00094 et->setEventLog(el); 00095 00096 // let's get all our ModelComponent instances started: 00097 manager.start(); 00098 00099 00100 // let's pre-load all the image names so that we can randomize them later: 00101 FILE *f = fopen(manager.getExtraArg(0).c_str(), "r"); 00102 if (f == NULL) LFATAL("Cannot read stimulus file"); 00103 char line[1024]; 00104 std::vector<std::string> ilist, tlist, rlist, slist; 00105 00106 int correct = 0, accuracy = 100, total = 0; 00107 00108 while(fgets(line, 1024, f)) 00109 { 00110 std::vector<std::string> tokens; 00111 // each line has four filenames: first the imagelet that contains 00112 // only the target, second the image that contains the target in 00113 // its environment, third the image to report position of target 00114 //fourth the name of the spec file for the search array 00115 LINFO("line reads %s",line); 00116 split(line," ", std::back_inserter(tokens)); 00117 00118 // now line is at the imagelet, line2 at the image 00119 tlist.push_back(std::string(tokens[0])); 00120 ilist.push_back(std::string(tokens[1])); 00121 rlist.push_back(std::string(tokens[2])); 00122 slist.push_back(std::string(tokens[3])); 00123 LINFO("\nNew pair \nline1 reads: %s, \nline2 reads:%s, \nline 3 reads %s,\nline 4 reads %s,",tokens[0].c_str(), tokens[1].c_str(),tokens[2].c_str(), tokens[3].c_str()); 00124 00125 } 00126 fclose(f); 00127 00128 // randomize stimulus presentation order: 00129 int nimg = ilist.size(); int imindex[nimg]; 00130 for (int i = 0; i < nimg; i ++) imindex[i] = i; 00131 randShuffle(imindex, nimg); 00132 00133 // let's display an ISCAN calibration grid: 00134 d->clearScreen(); 00135 d->displayISCANcalib(); 00136 d->waitForKey(); 00137 00138 // let's do an eye tracker calibration: 00139 d->displayText("<SPACE> to calibrate; other key to skip"); 00140 int c = d->waitForKey(); 00141 if (c == ' ') d->displayEyeTrackerCalibration(3, 3); 00142 00143 // we are ready to start: 00144 d->clearScreen(); 00145 00146 d->displayText("<SPACE> to start experiment"); 00147 d->waitForKey(); 00148 00149 //******************* main loop:***********************// 00150 for (int im = 0; im < nimg; im++) { 00151 int imnum = imindex[im]; 00152 00153 // load up the images and show a fixation cross on a blank screen: 00154 d->clearScreen(); 00155 LINFO("Loading '%s' / '%s'...", ilist[imnum].c_str(),tlist[imnum].c_str()); 00156 00157 // get the imagelet and place it at a random position: 00158 00159 if(!Raster::fileExists(tlist[imnum])) 00160 { 00161 // stop all our ModelComponents 00162 manager.stop(); 00163 LFATAL("i couldnt find image file %s", tlist[imnum].c_str()); 00164 } 00165 00166 Image< PixRGB<byte> > img = Raster::ReadRGB(tlist[imnum]); 00167 Image< PixRGB<byte> > rndimg(d->getDims(), NO_INIT); 00168 rndimg.clear(d->getGrey()); 00169 int rndx = 0,rndy = 0; 00170 00171 SDL_Surface *surf1 = d->makeBlittableSurface(img, true); 00172 char buf[256]; 00173 00174 if(!Raster::fileExists(ilist[imnum])) 00175 { 00176 manager.stop(); 00177 LFATAL("i couldnt find image file %s", ilist[imnum].c_str()); 00178 } 00179 img = Raster::ReadRGB(ilist[imnum]); 00180 SDL_Surface *surf2 = d->makeBlittableSurface(img, true); 00181 00182 //randShuffle(mindex, NMASK); 00183 00184 // load up the reporting number image: 00185 if(!Raster::fileExists(rlist[imnum])) 00186 { 00187 // stop all our ModelComponents 00188 manager.stop(); 00189 LFATAL(" i couldnt find image file %s", tlist[imnum].c_str()); 00190 } 00191 00192 img = Raster::ReadRGB(rlist[imnum]); 00193 SDL_Surface *surf3 = d->makeBlittableSurface(img, true); 00194 00195 // give a chance to other processes if single-CPU: 00196 usleep(200000); 00197 00198 // ready to go whenever the user is ready: 00199 d->displayFixationBlink(); 00200 //d->waitForKey(); 00201 d->waitNextRequestedVsync(false, true); 00202 00203 //************************ Display target************************// 00204 sprintf(buf, "===== Showing imagelet: %s at (%d, %d) =====", 00205 tlist[imnum].c_str(), rndx, rndy); 00206 00207 d->pushEvent(buf); 00208 d->displaySurface(surf1, 0, true); 00209 usleep(2000000); 00210 00211 d->clearScreen(); 00212 00213 //************************ Display array************************// 00214 00215 // start the eye tracker: 00216 et->track(true); 00217 00218 // show the image: 00219 d->pushEvent(std::string("===== Showing search image: ") + ilist[imnum] + 00220 std::string(" =====")); 00221 00222 d->displaySurface(surf2, 0, true); 00223 00224 // wait for key; it will record reaction time in the logs: 00225 d->waitForKey(); 00226 // stop the eye tracker: 00227 et->track(false); //we just want to record eye movements while subjects view the array 00228 00229 00230 //*********************** Display reporting image**************// 00231 // show the reporting image: 00232 d->pushEvent(std::string("===== Showing reporting image: ") + rlist[imnum] + 00233 std::string(" =====")); 00234 d->displaySurface(surf3, 0, true); 00235 00236 usleep(200000); 00237 d->displayText("Input the target number:"); 00238 00239 string inputString = d->getString('\n'); 00240 00241 //check user response 00242 char tmp[40]; 00243 00244 //lets open the spec file and extract the target number 00245 ifstream specFile(slist[imnum].c_str(), ifstream::in); 00246 00247 bool found =false; 00248 string testLine; 00249 std::vector<std::string> specTokens; 00250 00251 if(specFile.is_open()) 00252 { 00253 while(!specFile.eof() && !found) 00254 { 00255 getline(specFile, testLine); 00256 string::size_type loc = testLine.find("target", 0); 00257 00258 if(loc != string::npos) 00259 found = true; 00260 } 00261 if(!found) 00262 { 00263 manager.stop(); 00264 LFATAL("couldnt find the target number from spec file"); 00265 } 00266 00267 split(testLine," ", std::back_inserter(specTokens)); 00268 00269 std::string responseString; 00270 responseString = inputString; 00271 int intResponse = atoi(responseString.c_str()), intActual = atoi(specTokens[1].c_str()); 00272 00273 total++; 00274 00275 if (intResponse == intActual) 00276 { 00277 correct ++; 00278 accuracy = correct * 100 / total; 00279 sprintf(tmp, "Correct! Accuracy is %d%%", accuracy); 00280 d->displayText(tmp); 00281 d->pushEvent(std::string("===== Correct =====")); 00282 usleep(500000); 00283 } 00284 else 00285 { 00286 accuracy = correct * 100 / total; 00287 sprintf(tmp, "Wrong! Accuracy is %d%%", accuracy); 00288 d->displayText(tmp); 00289 d->pushEvent(std::string("===== Wrong =====")); 00290 usleep(500000); 00291 } 00292 00293 specFile.close(); 00294 } 00295 else 00296 { 00297 d->displayText("no target file found!"); 00298 LFATAL("couldnt open the file -%s-",slist[imnum].c_str()); 00299 } 00300 00301 00302 // free the imagelet and image: 00303 SDL_FreeSurface(surf1); SDL_FreeSurface(surf2); SDL_FreeSurface(surf3); 00304 00305 00306 // let's do a quiinckie eye tracker calibration once in a while: 00307 /*if (im > 0 && im % 20 == 0) { 00308 d->displayText("Ready for quick recalibration"); 00309 d->waitForKey(); 00310 d->displayEyeTrackerCalibration(3, 3); 00311 d->clearScreen(); 00312 d->displayText("Ready to continue with the images"); 00313 d->waitForKey(); 00314 }*/ 00315 00316 //allow for a break after 50 trials then recalibrate 00317 00318 if (im==50) 00319 { 00320 d->displayText("You may take a break press space to continue when ready"); 00321 d->waitForKey(); 00322 // let's display an ISCAN calibration grid: 00323 d->clearScreen(); 00324 d->displayISCANcalib(); 00325 d->waitForKey(); 00326 // let's do an eye tracker calibration: 00327 d ->displayText("<SPACE> to calibrate; other key to skip"); 00328 int c = d->waitForKey(); 00329 if (c == ' ') d->displayEyeTrackerCalibration(3, 3); 00330 } 00331 00332 00333 00334 et->recalibrate(d,15); 00335 00336 } 00337 00338 d->clearScreen(); 00339 d->displayText("Experiment complete. Thank you!"); 00340 d->waitForKey(); 00341 00342 // stop all our ModelComponents 00343 manager.stop(); 00344 00345 // all done! 00346 return 0; 00347 } 00348 00349 // ###################################################################### 00350 00351 extern "C" int main(const int argc, char** argv) 00352 { 00353 // simple wrapper around submain() to catch exceptions (because we 00354 // want to allow PsychoDisplay to shut down cleanly; otherwise if we 00355 // abort while SDL is in fullscreen mode, the X server won't return 00356 // to its original resolution) 00357 try 00358 { 00359 return submain(argc, argv); 00360 } 00361 catch (...) 00362 { 00363 REPORT_CURRENT_EXCEPTION; 00364 } 00365 00366 return 1; 00367 } 00368 // ###################################################################### 00369 /* So things look consistent in everyone's emacs... */ 00370 /* Local Variables: */ 00371 /* indent-tabs-mode: nil */ 00372 /* End: */