00001 /*!@file GameBoard/eyemarkup-overlay.C overlays the markup on top of the image */ 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/GameBoard/eyemarkup-overlay.C $ 00035 // $Id: eyemarkup-overlay.C 14047 2010-09-25 06:13:44Z itti $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Image/Image.H" 00040 #include "Psycho/PsychoDisplay.H" 00041 #include "Psycho/EyeTrackerConfigurator.H" 00042 #include "Psycho/EyeTracker.H" 00043 #include "Psycho/PsychoOpts.H" 00044 #include "Component/EventLog.H" 00045 #include "Component/ComponentOpts.H" 00046 #include "Raster/Raster.H" 00047 #include "Util/MathFunctions.H" 00048 #include "Util/Types.H" 00049 #include "GameBoard/basic-graphics.H" 00050 #include "GameBoard/resize.h" 00051 #include <sys/types.h> 00052 #include <dirent.h> 00053 #include <errno.h> 00054 #include <vector> 00055 #include <string> 00056 #include <iostream> 00057 #include <SDL/SDL.h> 00058 #include <SDL/SDL_image.h> 00059 #include <stdio.h> 00060 #include <stdlib.h> 00061 #include <sstream> 00062 #include <time.h> 00063 #include "Image/DrawOps.H" 00064 #include "GameBoard/resize.h" 00065 #include <iostream> 00066 #include <fstream> 00067 #include <set> 00068 #include <algorithm> 00069 using namespace std; 00070 map<string,string> argMap ; 00071 00072 00073 //pushes back the name of files in the directory into the given vector 00074 int getdir (string dir, vector<string> &files ,string ext) 00075 { 00076 DIR *dp; 00077 struct dirent *dirp; 00078 if((dp = opendir(dir.c_str())) == NULL) { 00079 cout << "Error(" << errno << ") opening " << dir << endl; 00080 return errno; 00081 } 00082 string fn = "" ; 00083 size_t found; 00084 string extension = "" ; 00085 while ((dirp = readdir(dp)) != NULL) { 00086 fn = string(dirp->d_name) ; 00087 found = fn.find_last_of("."); 00088 if(found > 0 && found <1000){ 00089 extension = fn.substr(found) ; 00090 if(extension.compare(ext)== 0 ) 00091 files.push_back(dir+"/"+fn); 00092 } 00093 } 00094 closedir(dp); 00095 return 0; 00096 } 00097 00098 string getCorrespondingDataFile(string fn, vector<string> files){ 00099 uint pos = fn.find_last_of("/"); 00100 string lp = fn.substr(pos+1) ; 00101 lp = lp.substr(0,lp.find(".")); 00102 for( uint i = 0 ; i < files.size() ; i++ ){ 00103 uint posd = files[i].find_last_of("/"); 00104 string lpd = files[i].substr(posd+1) ; 00105 lpd = lpd.substr(0,lpd.find(".")); 00106 if(lpd.compare(lp) == 0) return files[i] ; 00107 } 00108 return "" ; 00109 } 00110 void overlay_rect(SDL_Surface* surf,Point2D<int> location , Point2D<int> size , Uint32 bc){ 00111 Point2D<int> corner = Point2D<int>(location.i- size.i/2 , location.j - size.j/2) ; 00112 drawRectangle(surf,bc,corner.i,corner.j,size.i -1,size.j -1 ,1); 00113 00114 } 00115 00116 00117 int addArgument(const string st,const string delim="="){ 00118 int i = st.find(delim) ; 00119 argMap[st.substr(0,i)] = st.substr(i+1); 00120 00121 return 0 ; 00122 } 00123 00124 std::string getArgumentValue(string arg){ 00125 return argMap[arg] ; 00126 } 00127 00128 std::string getUsageComment(){ 00129 00130 string com = string("\nlist of arguments : \n"); 00131 00132 com += "\ndelay=[>=0] (the delaly for overlaying marks) {default=0}\n"; 00133 com += "\nlogfile=[logfilename.psy] {default = eyemarkup.psy}\n" ; 00134 com += "\nmode=[1..2] (1 for static , 2 for dynamic) {default=2}\n" ; 00135 com += "\nimage-dir=[path to directory of images]\n" ; 00136 com += "\ndata-dir=[path to directory of .ceyeS files]\n" ; 00137 return com ; 00138 } 00139 00140 extern "C" int main(const int argc, char** argv) 00141 { 00142 ModelManager manager("Psycho Skin"); 00143 nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager)); 00144 00145 MYLOGVERB = LOG_INFO; // suppress debug messages 00146 argMap["mode"] = "2" ; 00147 argMap["logfile"]="eyemarkup.psy" ; 00148 argMap["image-dir"]="."; 00149 argMap["data-dir"] = "." ; 00150 argMap["delay"]="0" ; 00151 00152 manager.addSubComponent(d); 00153 nub::soft_ref<EventLog> el(new EventLog(manager)); 00154 manager.addSubComponent(el); 00155 d->setEventLog(el); 00156 if (manager.parseCommandLine(argc, argv, 00157 "at least one argument needed", 1, -1)==false){ 00158 cout<<getUsageComment()<<endl; 00159 return(1); 00160 } 00161 00162 for(uint i = 0 ; i < manager.numExtraArgs() ; i++){ 00163 addArgument(manager.getExtraArg(i),std::string("=")) ; 00164 } 00165 // Parse command-line: 00166 manager.setOptionValString(&OPT_EventLogFileName, argMap["logfile"]); 00167 manager.start(); 00168 00169 string dir = argMap["image-dir"]; 00170 vector<string> imfiles = vector<string>(); 00171 getdir(dir,imfiles,".png"); 00172 00173 dir = argMap["data-dir"]; 00174 vector<string> datafiles = vector<string>(); 00175 getdir(dir,datafiles,".ceyeS"); 00176 00177 while(imfiles.size() > 0){ 00178 string imfilename = imfiles[0] ; 00179 imfiles.erase(imfiles.begin()); 00180 LINFO("%s", imfilename.c_str()); 00181 00182 //let's define a bunch of colors for different events and put them in a map 00183 // fixation: 0 blue 00184 // saccade: 1 green 00185 // blink/Artifact: 2 red 00186 // Saccade during Blink: 3 green .- 00187 // smooth pursuit: 4 magenta 00188 // drift/misclassification: 5 black 00189 Uint32 blue = d->getUint32color(PixRGB<byte>(0, 0, 255)); 00190 Uint32 green = d->getUint32color(PixRGB<byte>(0,255,0) ); 00191 Uint32 red = d->getUint32color(PixRGB<byte>(255,0,0) ); 00192 Uint32 saccade_green = d->getUint32color(PixRGB<byte>(0,150,0) ); 00193 Uint32 magenta = d->getUint32color(PixRGB<byte>(0,255,255) ); 00194 Uint32 black = d->getUint32color(PixRGB<byte>(0,0,0) ); 00195 map<string,Uint32> colormap = map<string,Uint32>() ; 00196 colormap["0"] = blue ; colormap["1"] = green ;colormap["2"] = red ; 00197 colormap["3"] = saccade_green ; colormap["4"]=magenta ; colormap["5"]= black ; 00198 00199 //now let's take in the corresponding .ceyeS file 00200 string datafilename = getCorrespondingDataFile(imfilename,datafiles); 00201 if(datafilename.size()!=0){ 00202 00203 SDL_Surface* im = load_image(imfilename) ; 00204 float rsf = min((float)d->getWidth()/(float)im->w , (float)d->getHeight()/(float)im->h ) ; 00205 im = SDL_Resize(im , rsf, 5) ; 00206 SDL_Rect offset ; 00207 offset.x= (d->getWidth() - im->w) /2; 00208 offset.y=(d-> getHeight() - im->h) /2; 00209 00210 ifstream inFile( datafilename.c_str() , ios::in); 00211 if (! inFile) 00212 { 00213 LINFO("profile '%s' not found!" , datafilename.c_str()); 00214 return -1; 00215 } 00216 Point2D<int> rectsize = Point2D<int>(6,6) ; 00217 Point2D<int> loc ; 00218 char ch[1000]; 00219 00220 while (inFile.getline(ch , 1000) ){ 00221 string line = ch; 00222 //LINFO("reads : %s", line.c_str()); 00223 uint pos = line.find(" "); 00224 loc.i = (int) (rsf*atof(line.substr(0,pos).c_str())) ; 00225 line = line.substr(pos+1); 00226 pos = line.find(" "); 00227 loc.j = (int) (rsf*atof(line.substr(0,pos).c_str())) ; 00228 string colorcode = line.substr(line.size()-1) ; 00229 if (loc.i > 4 && loc.j > 4 ) 00230 overlay_rect(im,loc,rectsize,colormap[colorcode]); 00231 if(argMap["mode"].compare("2")==0) 00232 d->waitFrames(atoi(argMap["delay"].c_str())); 00233 d->displaySDLSurfacePatch(im , &offset,NULL , -2,false, true) ; 00234 } 00235 d->waitForMouseClick() ; 00236 dumpSurface(im) ; 00237 00238 } 00239 00240 00241 00242 00243 } 00244 manager.stop(); 00245 return 0 ; 00246 } 00247