00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef TESTSALIENCYMT_H_DEFINED
00041 #define TESTSALIENCYMT_H_DEFINED
00042
00043 #include "CMapDemo/SaliencyCMapMT.H"
00044 #include "Component/ModelManager.H"
00045 #include "Devices/DeviceOpts.H"
00046 #include "Devices/FrameGrabberConfigurator.H"
00047 #include "GUI/XWinManaged.H"
00048 #include "Image/Convolver.H"
00049 #include "Image/CutPaste.H"
00050 #include "Image/Image.H"
00051 #include "Image/Pixels.H"
00052 #include "Neuro/NeuroOpts.H"
00053 #include "Neuro/SaccadeControllers.H"
00054 #include "Neuro/WTAwinner.H"
00055 #include "Raster/Raster.H"
00056 #include "Transport/FrameIstream.H"
00057 #include "Util/Timer.H"
00058
00059 #include <arpa/inet.h>
00060 #include <fcntl.h>
00061 #include <netdb.h>
00062 #include <signal.h>
00063 #include <stdlib.h>
00064 #include <unistd.h>
00065
00066 #define MAXFLOAT 3.40282347e+38F
00067 #define sml 0
00068
00069
00070 #define NAVG 20
00071
00072
00073 #define SMFAC 1.00F
00074
00075 #define WINSIZE 51
00076 static bool goforever = true;
00077
00078
00079 void terminate(int s)
00080 { LERROR("*** INTERRUPT ***"); goforever = false; exit(1); }
00081
00082 ImageSet<float> bias(14);
00083 ImageSet<float> newBias(14);
00084
00085
00086
00087
00088 int main(int argc, char **argv)
00089 {
00090 MYLOGVERB = LOG_INFO;
00091
00092 CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB4");
00093
00094
00095 ModelManager manager("SaliencyMT Tester");
00096
00097
00098 nub::ref<FrameGrabberConfigurator>
00099 gbc(new FrameGrabberConfigurator(manager));
00100 manager.addSubComponent(gbc);
00101
00102 nub::ref<SaliencyMT> smt(new SaliencyMT(manager, orb, sml));
00103 manager.addSubComponent(smt);
00104
00105
00106
00107
00108 manager.exportOptions(MC_RECURSE);
00109 manager.setOptionValString(&OPT_FrameGrabberType, "V4L");
00110 manager.setOptionValString(&OPT_FrameGrabberMode, "YUV420P");
00111 manager.setOptionValString(&OPT_FrameGrabberDevice, "/dev/video1");
00112 manager.setOptionValString(&OPT_FrameGrabberChannel, "1");
00113 manager.setOptionValString(&OPT_FrameGrabberHue, "0");
00114 manager.setOptionValString(&OPT_FrameGrabberContrast, "16384");
00115
00116 manager.setOptionValString(&OPT_FrameGrabberDims, "160x120");
00117 manager.setOptionValString(&OPT_SaccadeControllerEyeType, "Threshfric");
00118 manager.setOptionValString(&OPT_SCeyeMaxIdleSecs, "1000.0");
00119 manager.setOptionValString(&OPT_SCeyeThreshMinOvert, "4.0");
00120 manager.setOptionValString(&OPT_SCeyeThreshMaxCovert, "3.0");
00121 manager.setOptionValString(&OPT_SCeyeThreshMinNum, "2");
00122
00123
00124
00125
00126
00127 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false) return(1);
00128
00129
00130 nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00131 if (gb.isInvalid())
00132 LFATAL("You need to select a frame grabber type via the "
00133 "--fg-type=XX command-line option for this program "
00134 "to be useful");
00135 const int w = gb->getWidth(), h = gb->getHeight();
00136
00137 const int foa_size = std::min(w, h) / 12;
00138 manager.setModelParamVal("InputFrameDims", Dims(w, h),
00139 MC_RECURSE | MC_IGNORE_MISSING);
00140 manager.setModelParamVal("SCeyeStartAtIP", true,
00141 MC_RECURSE | MC_IGNORE_MISSING);
00142 manager.setModelParamVal("SCeyeInitialPosition",Point2D<int>(w/2,h/2),
00143 MC_RECURSE | MC_IGNORE_MISSING);
00144 manager.setModelParamVal("FOAradius", foa_size,
00145 MC_RECURSE | MC_IGNORE_MISSING);
00146 manager.setModelParamVal("FoveaRadius", foa_size,
00147 MC_RECURSE | MC_IGNORE_MISSING);
00148
00149
00150 signal(SIGHUP, terminate); signal(SIGINT, terminate);
00151 signal(SIGQUIT, terminate); signal(SIGTERM, terminate);
00152 signal(SIGALRM, terminate);
00153
00154
00155 int sock = socket(AF_INET, SOCK_DGRAM, 0);
00156 if (sock == -1) PLFATAL("Cannot create server socket");
00157
00158
00159 uint frame = 0U;
00160 uint lastframesent = 0U;
00161
00162 uint64 avgtime = 0; int avgn = 0;
00163 float fps = 0.0F;
00164 uint64 p_avgtime = 0; int p_avgn = 0;
00165 float p_fps = 0.0F;
00166 Timer tim;
00167 Timer p_tim;
00168 Timer masterclock;
00169
00170 Image<float> sm(w >> sml, h >> sml, ZEROS);
00171 Point2D<int> fixation(-1, -1);
00172
00173
00174 Image<PixRGB<byte> > disp(w * 2, h + 20, ZEROS);
00175 disp += PixRGB<byte>(128);
00176 XWinManaged xwin(disp.getDims(), -1, -1, "RCBot");
00177 XWinManaged xwin2(Dims(w,h), -1, -1, "RCBot");
00178 XWinManaged xwin3(Dims(256,256), -1, -1, "RCBot");
00179
00180
00181
00182
00183 char info[1000];
00184
00185 Point2D<int> lastpointsent(w/2, h/2);
00186
00187
00188
00189 try {
00190
00191 manager.start();
00192
00193
00194 gb->startStream();
00195
00196
00197 tim.reset(); masterclock.reset();
00198 p_tim.reset();
00199
00200 Point2D<int> loc(84, 33);
00201
00202 while(goforever)
00203 {
00204
00205
00206 char filename[255];
00207
00208 sprintf(filename, "/usr/home/elazary/images/backyard/2/dframe%06d.ppm", frame+1);
00209 Image< PixRGB<byte> > ima = rescale(Raster::ReadRGB(filename), w, h);
00210 usleep(10000);
00211
00212
00213 inplacePaste(disp, ima, Point2D<int>(0, 0));
00214 Image<float> dispsm = rescale(sm, w,h) * SMFAC;
00215 inplaceNormalize(dispsm, 0.0F, 255.0F);
00216
00217
00218
00219
00220
00221
00222 inplacePaste(disp, Image<PixRGB<byte> >(toRGB(dispsm)), Point2D<int>(w, 0));
00223
00224
00225
00226
00227
00228
00229 Point2D<int> fix2(fixation); fix2.i += w;
00230 if (fixation.i >= 0)
00231 {
00232 drawDisk(disp, fixation, foa_size/6+2, PixRGB<byte>(20, 50, 255));
00233 drawDisk(disp, fixation, foa_size/6, PixRGB<byte>(255, 255, 20));
00234 drawDisk(disp, fix2, foa_size/6+2, PixRGB<byte>(20, 50, 255));
00235 drawDisk(disp, fix2, foa_size/6, PixRGB<byte>(255, 255, 20));
00236 }
00237
00238
00239
00240 xwin.drawImage(disp);
00241
00242
00243
00244
00245 if (loc.isValid()){
00246 LINFO("Loc: %i %i\n", loc.i, loc.j);
00247
00248
00249
00250 while(!smt->outputReady()){
00251 usleep(100);
00252 }
00253
00254 smt->setBiasSM(false);
00255 smt->setSaliencyMapLevel(0);
00256 smt->newInput(ima);
00257
00258 while(!smt->outputReady()){
00259 usleep(100);
00260 }
00261
00262 Image<float> tmpSM = smt->getOutput();
00263
00264
00265 Image<float> smp;
00266
00267 for(int i=0; i<14; i++){
00268 if (smt->cmaps[i].initialized()){
00269
00270
00271 Point2D<int> center_loc(loc.i-((WINSIZE-1)/2), loc.j-((WINSIZE-1)/2));
00272
00273 Image<float> target = crop(smt->cmaps[i],center_loc, Dims(WINSIZE,WINSIZE));
00274 bias[i] = target;
00275 xwin3.drawImage(target);
00276
00277 LINFO("Convol");
00278 for(int y=0; y<target.getHeight()/2; y++){
00279 for(int x=0; x<target.getWidth()/2; x++){
00280 float tmp = target.getVal(x,y);
00281 target.setVal(x,y,target.getVal(target.getWidth()-1-x,target.getHeight()-1-y));
00282 target.setVal(target.getWidth()-1-x,target.getHeight()-1-y,tmp);
00283
00284 }
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 xwin2.drawImage(target);
00296 getchar();
00297
00298 Convolver fc(target, smt->cmaps[i].getDims());
00299 Image<float> conv1 = fc.fftConvolve(smt->cmaps[i]);
00300 float maxval; Point2D<int> currwin; findMin(conv1, currwin, maxval);
00301 LINFO("Min at %ix%i", currwin.i, currwin.j);
00302 xwin3.drawImage(conv1);
00303 getchar();
00304
00305 }
00306 }
00307
00308 smt->setSMBias(bias);
00309
00310 smt->setBiasSM(true);
00311
00312 smt->newInput(ima);
00313 loc.i = -1; loc.j = -1;
00314
00315 }
00316
00317
00318
00319 if (smt->outputReady())
00320 {
00321
00322 Image<float> out = smt->getOutput();
00323 if (out.initialized())
00324 sm = out;
00325
00326
00327 float maxval; Point2D<int> currwin; findMin(sm, currwin, maxval);
00328
00329 currwin.i = currwin.i + (WINSIZE-1)/2;
00330 currwin.j = currwin.j + (WINSIZE-1)/2;
00331
00332 if (currwin.isValid()) fixation = currwin;
00333
00334 LINFO("Winner:%ix%i", fixation.i, fixation.j);
00335
00336
00337 double dist = 0;
00338 for(int i=0; i<14; i++){
00339 if (smt->cmaps[i].initialized()){
00340 if (bias[i].initialized() && newBias[i].initialized()){
00341 Point2D<int> center_fixation(fixation.i-((WINSIZE-1)/2),
00342 fixation.j-((WINSIZE-1)/2));
00343 Image<float> target = crop(smt->cmaps[i],
00344 center_fixation,
00345 Dims(WINSIZE,WINSIZE));
00346 newBias[i] = target;
00347 dist += distance(bias[i], newBias[i]);
00348 bias[i] = newBias[i];
00349 }
00350 }
00351 }
00352 LINFO("Distance: %f", dist);
00353 if (dist < 2000)
00354 smt->setSMBias(newBias);
00355
00356
00357
00358
00359 smt->newInput(ima);
00360 lastframesent = frame;
00361
00362
00363
00364
00365 p_avgtime += p_tim.getReset(); p_avgn ++;
00366 if (p_avgn == NAVG)
00367 {
00368 p_fps = 1000.0F / float(p_avgtime) * float(p_avgn);
00369 p_avgtime = 0; p_avgn = 0;
00370 }
00371
00372 }
00373
00374
00375 avgtime += tim.getReset(); avgn ++;
00376 if (avgn == NAVG)
00377 {
00378 fps = 1000.0F / float(avgtime) * float(avgn);
00379 avgtime = 0; avgn = 0;
00380 }
00381
00382
00383 sprintf(info, "%06u/%06u %.1ffps/%.5ffps ",
00384 frame, lastframesent, fps, p_fps);
00385
00386 writeText(disp, Point2D<int>(0, h), info,
00387 PixRGB<byte>(255), PixRGB<byte>(127));
00388
00389
00390 ++ frame;
00391 }
00392
00393
00394 manager.stop();
00395 close(sock);
00396
00397 } catch ( ... ) { };
00398
00399 return 0;
00400 }
00401
00402 #endif
00403
00404
00405
00406
00407