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 #include "Component/ModelManager.H"
00040 #include "Devices/DeviceOpts.H"
00041 #include "GUI/XWindow.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/CutPaste.H"
00044 #include "Image/Image.H"
00045 #include "Image/Pixels.H"
00046 #include "Image/MathOps.H"
00047 #include "Neuro/EnvVisualCortex.H"
00048 #include "Media/FrameSeries.H"
00049 #include "Media/MediaOpts.H"
00050 #include "Transport/FrameInfo.H"
00051 #include "Raster/GenericFrame.H"
00052 #include "Raster/Raster.H"
00053 #include "Util/Timer.H"
00054 #include "Util/log.H"
00055 #include "Util/MathFunctions.H"
00056 #include "Learn/Bayes.H"
00057 #include "Learn/BackpropNetwork.H"
00058 #include "Envision/env_image_ops.h"
00059 #include "Neuro/BeoHeadBrain.H"
00060
00061 #include "GUI/DebugWin.H"
00062 #include <ctype.h>
00063 #include <deque>
00064 #include <iterator>
00065 #include <stdlib.h>
00066 #include <string>
00067 #include <vector>
00068 #include <map>
00069
00070 void display(Image<PixRGB<byte> > &leftImg,
00071 const Image<byte> &leftSmap,
00072 const Point2D<int> &leftWinner,
00073 const byte maxVal,
00074 const Point2D<int> &targetLoc);
00075 void display(const Image<PixRGB<byte> > &img,
00076 const Image<PixRGB<byte> > &smap, Point2D<int> &winner, Rectangle &rect);
00077
00078
00079 XWinManaged *xwin;
00080 Timer timer;
00081 Image<PixRGB<byte> > disp;
00082 byte SmaxVal = 0;
00083 int smap_level = -1;
00084
00085 bool debug = 0;
00086 bool init_points = true;
00087
00088
00089 int main(int argc, const char **argv)
00090 {
00091
00092 ModelManager *mgr = new ModelManager("USC Robot Head");
00093
00094 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*mgr));
00095 mgr->addSubComponent(ifs);
00096
00097 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00098 mgr->addSubComponent(ofs);
00099
00100 nub::ref<EnvVisualCortex> leftEvc(new EnvVisualCortex(*mgr));
00101 mgr->addSubComponent(leftEvc);
00102
00103 nub::soft_ref<BeoHeadBrain> beoHeadBrain(new BeoHeadBrain(*mgr));
00104 mgr->addSubComponent(beoHeadBrain);
00105
00106
00107 mgr->exportOptions(MC_RECURSE);
00108 mgr->setOptionValString(&OPT_EvcMaxnormType, "None");
00109 mgr->setOptionValString(&OPT_EvcLevelSpec, "3,4,3,4,3");
00110
00111 mgr->setOptionValString(&OPT_InputFrameSource, "V4L");
00112 mgr->setOptionValString(&OPT_FrameGrabberMode, "RGB24");
00113 mgr->setOptionValString(&OPT_FrameGrabberDims, "320x240");
00114 mgr->setOptionValString(&OPT_FrameGrabberFPS, "30");
00115
00116
00117
00118
00119
00120
00121
00122
00123 if (mgr->parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00124
00125
00126 Dims imageDims = ifs->peekDims();
00127
00128 xwin = new XWinManaged(Dims(imageDims.w()*2,imageDims.h()*2+20),
00129 -1, -1, "ILab Robot Head Demo");
00130 disp = Image<PixRGB<byte> >(imageDims.w(),imageDims.h()+20, ZEROS);
00131
00132
00133 mgr->start();
00134
00135 smap_level = leftEvc->getMapLevel();
00136
00137
00138 ifs->startStream();
00139
00140 timer.reset();
00141
00142 byte leftMaxVal;
00143 Point2D<int> leftMaxPos;
00144
00145 int frame = 0;
00146
00147 beoHeadBrain->init(imageDims);
00148
00149 while(1) {
00150
00151 Image< PixRGB<byte> > leftImg;
00152 const FrameState is = ifs->updateNext();
00153 if (is == FRAME_COMPLETE)
00154 break;
00155
00156
00157 GenericFrame input = ifs->readFrame();
00158 if (!input.initialized())
00159 break;
00160 leftImg = input.asRgb();
00161
00162 leftEvc->input(leftImg);
00163
00164 ofs->writeRGB(leftImg, "input", FrameInfo("Copy of input", SRC_POS));
00165
00166 Image<float> leftVcxmap = leftEvc->getVCXmap();
00167 inplaceNormalize(leftVcxmap, 0.0F, 255.0F);
00168 Image<byte> leftSmap = leftVcxmap;
00169 findMax(leftSmap, leftMaxPos, leftMaxVal);
00170
00171 Image<byte> grey = luminance(leftImg);
00172
00173 Point2D<int> clickPos = xwin->getLastMouseClick();
00174
00175
00176 Point2D<int> targetLoc;
00177 if (clickPos.isValid())
00178 {
00179 beoHeadBrain->setTarget(clickPos, grey);
00180 } else {
00181 targetLoc = beoHeadBrain->trackObject(grey);
00182 }
00183 display(leftImg, leftSmap, leftMaxPos,
00184 SmaxVal, targetLoc);
00185
00186 frame++;
00187 }
00188
00189
00190
00191 mgr->stop();
00192
00193
00194 return 0;
00195 }
00196
00197 void display(Image<PixRGB<byte> > &leftImg,
00198 const Image<byte> &leftSmap,
00199 const Point2D<int> &leftWinner,
00200 const byte maxVal,
00201 const Point2D<int> &targetLoc)
00202 {
00203 static int avgn = 0;
00204 static uint64 avgtime = 0;
00205 static double fps = 0;
00206 char msg[255];
00207
00208
00209 drawCircle(leftImg,
00210 Point2D<int>(leftWinner.i *(1<<smap_level), leftWinner.j*(1<<smap_level)),
00211 30, PixRGB<byte>(255,0,0));
00212 drawCross(leftImg, Point2D<int>(leftImg.getWidth()/2, leftImg.getHeight()/2),
00213 PixRGB<byte>(0,255,0));
00214 sprintf(msg, "%i", maxVal);
00215 writeText(leftImg,
00216 Point2D<int>(leftWinner.i *(1<<smap_level), leftWinner.j*(1<<smap_level)),
00217 msg, PixRGB<byte>(255), PixRGB<byte>(127));
00218
00219 if (targetLoc.isValid())
00220 drawCircle(leftImg, targetLoc, 3, PixRGB<byte>(0,255,0));
00221
00222 xwin->drawImage(leftImg, 0, 0);
00223 Image<PixRGB<byte> > leftSmapDisp = toRGB(quickInterpolate(leftSmap, 1 << smap_level));
00224
00225
00226
00227
00228 avgn++;
00229 avgtime += timer.getReset();
00230 if (avgn == 20)
00231 {
00232 fps = 1000.0F / double(avgtime) * double(avgn);
00233 avgtime = 0;
00234 avgn = 0;
00235 }
00236
00237
00238 Image<PixRGB<byte> > infoImg(leftImg.getWidth()*2, 20, NO_INIT);
00239 writeText(infoImg, Point2D<int>(0,0), msg,
00240 PixRGB<byte>(255), PixRGB<byte>(127));
00241
00242
00243 }
00244
00245 void display(const Image<PixRGB<byte> > &img, const Image<PixRGB<byte> > &out,
00246 Point2D<int> &winner, Rectangle &rect)
00247 {
00248 static int avgn = 0;
00249 static uint64 avgtime = 0;
00250 static double fps = 0;
00251 char msg[255];
00252
00253 inplacePaste(disp, img, Point2D<int>(0,0));
00254
00255 drawRect(disp, rect, PixRGB<byte>(255,0,0));
00256 inplacePaste(disp, out, Point2D<int>(img.getWidth(), 0));
00257
00258
00259 avgn++;
00260 avgtime += timer.getReset();
00261 if (avgn == 20)
00262 {
00263 fps = 1000.0F / double(avgtime) * double(avgn);
00264 avgtime = 0;
00265 avgn = 0;
00266 }
00267
00268 sprintf(msg, "%.1ffps ", fps);
00269
00270 writeText(disp, Point2D<int>(0,img.getHeight()), msg,
00271 PixRGB<byte>(255), PixRGB<byte>(127));
00272
00273 xwin->drawImage(disp);
00274
00275 }
00276