00001 /*! @file Qt/SceneUnderstandingForm.ui.h Main form for scene undersanding */ 00002 00003 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Qt/SceneUnderstandingForm.ui.h $ 00004 // $Id: SceneUnderstandingForm.ui.h 9412 2008-03-10 23:10:15Z farhan $ 00005 00006 /**************************************************************************** 00007 ** ui.h extension file, included from the uic-generated form implementation. 00008 ** 00009 ** If you want to add, delete, or rename functions or slots, use 00010 ** Qt Designer to update this file, preserving your code. 00011 ** 00012 ** You should not define a constructor or destructor in this file. 00013 ** Instead, write your code in functions called init() and destroy(). 00014 ** These will automatically be called by the form's constructor and 00015 ** destructor. 00016 *****************************************************************************/ 00017 00018 00019 #include "Channels/SubmapAlgorithmBiased.H" 00020 00021 void SceneUnderstandingForm::init( ModelManager & manager ) 00022 { 00023 00024 itsMgr = &manager; 00025 00026 //Init the descriptor vector 00027 nub::ref<StdBrain> brain = dynCastWeak<StdBrain>(itsMgr->subComponent("Brain")); 00028 00029 00030 //The SceneUnderstanding 00031 itsSceneUnderstanding = new SceneUnderstanding(itsMgr, brain); 00032 00033 itsDescriptorVec = itsSceneUnderstanding->getDescriptorVecPtr(); 00034 itsBayesNetwork = itsSceneUnderstanding->getBayesPtr(); 00035 itsProlog = itsSceneUnderstanding->getPrologPtr(); 00036 00037 itsDescriptorVecDialog.init(*itsDescriptorVec); 00038 00039 00040 itsTrainScene = new SceneGenerator(SceneGenerator::ALOI_OBJECTS, 500, 500); 00041 itsTestScene = new SceneGenerator(SceneGenerator::ALOI_OBJECTS, 500, 500); 00042 00043 } 00044 00045 void SceneUnderstandingForm::fileOpen() 00046 { 00047 00048 static QString prevFilename("/home/lior/scenes/satellite/images/set1"); 00049 00050 QString file = QFileDialog::getOpenFileName( prevFilename, 00051 "Images (*.pgm *.ppm *.pnm *.jpeg *.png *.jpg)", 00052 this, "OpenImageDialog", 00053 "Choose Image" ); 00054 if( !file.isEmpty() ) { 00055 prevFilename = file; 00056 itsImg = Raster::ReadRGB( file ); 00057 printf("File: %s \n", file.ascii()); 00058 // itsImg = rescale(itsImg, 680, 512); 00059 itsSceneUnderstanding->setImage(itsImg); 00060 evolveBrain(); 00061 00062 /* //draw ref circles 00063 for(int y=0; y<itsImg.getHeight()+70; y+=70) 00064 for(int x=0; x<itsImg.getWidth()+80; x+=80) 00065 { 00066 if ((y/70)%2) 00067 drawCircle(itsImg, Point2D<int>(x-40,y), 40, PixRGB<byte>(255,0,0)); 00068 else 00069 drawCircle(itsImg, Point2D<int>(x,y), 40, PixRGB<byte>(255,0,0)); 00070 }*/ 00071 imgDisp->setImage(itsImg); 00072 updateDisplay(); 00073 } 00074 } 00075 00076 void SceneUnderstandingForm::fileOpenWorkspace() 00077 { 00078 00079 QString KBFile("/home/lior/scenes/satellite/scenes/scene"); 00080 QString bayesFile("/home/lior/scenes/satellite/scenes/scene.net"); 00081 00082 LINFO("Consulting with %s", KBFile.ascii()); 00083 if( itsProlog->consult((char*)KBFile.ascii()) ) 00084 { 00085 msgLabel->setText(QString("KB Loaded: ") + KBFile); 00086 } else { 00087 msgLabel->setText(QString("Can not load KB: ") + KBFile); 00088 } 00089 00090 itsBayesNetwork->load(bayesFile.ascii()); 00091 00092 } 00093 00094 void SceneUnderstandingForm::updateDisplay() 00095 { 00096 nub::ref<StdBrain> brain = itsSceneUnderstanding->getBrainPtr(); 00097 if (true) showTraj(brain); 00098 if (true) showSMap(brain); 00099 if (true) showChannels(brain); 00100 00101 } 00102 00103 void SceneUnderstandingForm::fileSave() 00104 { 00105 00106 } 00107 00108 00109 void SceneUnderstandingForm::fileExit() 00110 { 00111 } 00112 00113 00114 void SceneUnderstandingForm::showBiasSettings() 00115 { 00116 itsBiasSettingsDialog.init(*itsMgr); 00117 itsBiasSettingsDialog.show(); 00118 00119 } 00120 00121 00122 void SceneUnderstandingForm::evolveBrain() 00123 { 00124 00125 float interestLevel = itsSceneUnderstanding->evolveBrain(); 00126 updateDisplay(); 00127 LINFO("Interest Level %f", interestLevel); 00128 if (interestLevel > 1.0F) 00129 { 00130 msgLabel->setText(QString("Found something interesting %L1").arg(interestLevel)); 00131 Point2D<int> foveaLoc = itsSceneUnderstanding->getFoveaLoc(); 00132 classifyFovea(foveaLoc.i, foveaLoc.j); 00133 } else { 00134 msgLabel->setText(QString("Boring")); 00135 } 00136 00137 } 00138 00139 void SceneUnderstandingForm::showTraj(nub::ref<StdBrain>& brain){ 00140 static bool setupTab = true; 00141 static ImageCanvas* disp = NULL; 00142 00143 //get the Seq com 00144 nub::ref<SimEventQueueConfigurator> seqc = 00145 dynCastWeak<SimEventQueueConfigurator>(itsMgr->subComponent("SimEventQueueConfigurator")); 00146 nub::ref<SimEventQueue> itsSEQ = seqc->getQ(); 00147 00148 itsOutputImg = brain->getSV()->getTraj(itsSEQ->now()); 00149 00150 if (setupTab){ 00151 00152 QWidget* tab = new QWidget(tabDisp); 00153 QHBoxLayout* tabLayout = new QHBoxLayout(tab, 11, 6); 00154 disp = new ImageCanvas(tab); 00155 tabLayout->addWidget(disp); 00156 tabDisp->insertTab(tab, "Trajectory" ); 00157 setupTab = false; 00158 00159 //set bias connection 00160 connect(disp, SIGNAL(mousePressed(int,int) ), 00161 this, SLOT( getDescriptor(int,int) ) ); 00162 00163 } 00164 00165 if (disp) 00166 disp->setImage(itsOutputImg); 00167 00168 } 00169 00170 00171 void SceneUnderstandingForm::showSMap( nub::ref<StdBrain> & brain ) 00172 { 00173 static bool setupTab = true; 00174 static ImageCanvas* disp = NULL; 00175 00176 if (setupTab){ 00177 QWidget* tab = new QWidget(tabDisp); 00178 QHBoxLayout* tabLayout = new QHBoxLayout(tab, 11, 6); 00179 disp = new ImageCanvas(tab); 00180 tabLayout->addWidget(disp); 00181 tabDisp->insertTab(tab, "SMap" ); 00182 setupTab = false; 00183 } 00184 00185 if (disp) { 00186 Image<float> img = brain->getSM()->getV(false); 00187 disp->setImage(img); 00188 } 00189 00190 } 00191 00192 00193 void SceneUnderstandingForm::showChannels( nub::ref<StdBrain> & brain ) 00194 { 00195 static bool setupTab = true; 00196 static ImageCanvas* disp[10]; 00197 int numChans = brain->getVC()->numChans(); 00198 00199 if (setupTab){ 00200 for (int i=0; i<numChans; i++) 00201 { 00202 QWidget* tab = new QWidget(tabDisp); 00203 QHBoxLayout* tabLayout = new QHBoxLayout(tab, 11, 6); 00204 disp[i] = new ImageCanvas(tab); 00205 tabLayout->addWidget(disp[i]); 00206 tabDisp->insertTab(tab, 00207 brain->getVC()->subChan(i)->descriptiveName()); 00208 setupTab = false; 00209 } 00210 } 00211 00212 if (disp) 00213 { 00214 for( int i=0; i<numChans; i++){ 00215 Image<float> img = brain->getVC()->subChan(i)->getOutput(); 00216 disp[i]->setImage(img); 00217 } 00218 } 00219 00220 } 00221 00222 00223 void SceneUnderstandingForm::configureView( QAction * action ) 00224 { 00225 LINFO("Configure view %i %i %i", itsViewTraj, itsViewSMap, itsViewChannels); 00226 if (viewTrajAction->isOn()) itsViewTraj = true; else itsViewTraj = false; 00227 if (viewSMapAction->isOn()) itsViewSMap = true; else itsViewSMap = false; 00228 if (viewChannelsAction->isOn()) itsViewChannels = true; else itsViewChannels = false; 00229 } 00230 00231 00232 void SceneUnderstandingForm::setBias( int x, int y ) 00233 { 00234 00235 } 00236 00237 00238 void SceneUnderstandingForm::getDescriptor( int x, int y ) 00239 { 00240 printf("%i,%i\n", x, y); 00241 nub::ref<StdBrain> brain = dynCastWeak<StdBrain>(itsMgr->subComponent("Brain")); 00242 ComplexChannel* cc = &*dynCastWeak<ComplexChannel>(brain->getVC()); 00243 itsCurrentAttention = Point2D<int>(x,y); 00244 //check if the brain has evolved 00245 if (!cc->hasInput()) 00246 evolveBrain(); 00247 00248 //Classify the object under the fovea 00249 if (cc->hasInput()) //check if we got an input (could be a blank image) 00250 { 00251 classifyFovea(x, y); 00252 } 00253 } 00254 00255 00256 00257 00258 void SceneUnderstandingForm::showDescriptorVec() 00259 { 00260 itsDescriptorVecDialog.show(); 00261 } 00262 00263 00264 void SceneUnderstandingForm::genScene() 00265 { 00266 00267 if (!editConfigureTestAction->isOn()) //Train Mode 00268 itsImg = itsTrainScene->getScene(9); 00269 else 00270 itsImg = itsTestScene->getScene(9); 00271 00272 imgDisp->setImage(itsImg); 00273 // evolveBrain(); 00274 00275 } 00276 00277 void SceneUnderstandingForm::classifyFovea(int x, int y) 00278 { 00279 00280 double prob; 00281 int cls = itsSceneUnderstanding->classifyFovea(Point2D<int>(x,y), &prob); 00282 itsDescriptorVecDialog.update(); 00283 00284 if (cls == -1) 00285 { 00286 msgLabel->setText(QString("Can you tell me what this is?")); 00287 itsCurrentObject = -1; 00288 } else { 00289 LINFO("Class name is %s (%f)", itsBayesNetwork->getClassName(cls), prob); 00290 msgLabel->setText(QString("This is %L1 (%L2)") 00291 .arg(itsBayesNetwork->getClassName(cls)) 00292 .arg(prob)); 00293 itsCurrentObject = cls; 00294 } 00295 00296 00297 } 00298 00299 00300 00301 00302 void SceneUnderstandingForm::run() 00303 { 00304 for (int i=0; i<timesSpinBox->value(); i++){ 00305 genScene(); //generate a scene 00306 evolveBrain(); //evolve the brain 00307 } 00308 00309 } 00310 00311 00312 void SceneUnderstandingForm::loadBayesNetwork() 00313 { 00314 QString file = QFileDialog::getOpenFileName( QString::null, 00315 "Bayes network (*.net)", 00316 this, "OpenImageDialog", 00317 "Choose Bayes Network" ); 00318 if( !file.isEmpty() ) 00319 itsBayesNetwork->load(file.ascii()); 00320 00321 } 00322 00323 00324 void SceneUnderstandingForm::saveBayesNetwork() 00325 { 00326 QString file = QFileDialog::getSaveFileName( QString::null, 00327 "Bayes network (*.net)", 00328 this, "SaveImageDialog", 00329 "Choose Bayes Network" ); 00330 if( !file.isEmpty() ) 00331 itsBayesNetwork->save(file.ascii()); 00332 00333 00334 } 00335 00336 00337 void SceneUnderstandingForm::viewBayesNetwork() 00338 { 00339 itsBayesNetworkDialog.show(); 00340 itsBayesNetworkDialog.init(*itsBayesNetwork); 00341 00342 } 00343 00344 00345 void SceneUnderstandingForm::setBiasImage( bool biasVal ) 00346 { 00347 nub::ref<StdBrain> brain = dynCastWeak<StdBrain>(itsMgr->subComponent("Brain")); 00348 ComplexChannel* cc = &*dynCastWeak<ComplexChannel>(brain->getVC()); 00349 00350 //set the bias 00351 setSubmapAlgorithmBiased(*cc); 00352 } 00353 00354 00355 void SceneUnderstandingForm::showSceneSettings() 00356 { 00357 itsSceneSettingsDialog.init(itsTrainScene, itsTestScene); 00358 itsSceneSettingsDialog.show(); 00359 00360 } 00361 00362 00363 void SceneUnderstandingForm::submitDialog() 00364 { 00365 00366 QString str = dialogText->text().lower(); 00367 LINFO("You said: %s", str.ascii()); 00368 00369 if (str.startsWith("this is ")) //bias for the object 00370 { 00371 QString obj = str.section(' ', 2, 2); 00372 msgLabel->setText(QString("Learning ") + obj); 00373 00374 //Learn the object 00375 LINFO("Learning object from %ix%i", itsCurrentAttention.i, itsCurrentAttention.j); 00376 00377 itsSceneUnderstanding->learn(itsCurrentAttention, obj.ascii()); 00378 itsDescriptorVecDialog.update(); 00379 updateDisplay(); 00380 00381 return; 00382 } 00383 00384 if (str.startsWith("where is ")) //bias for the object 00385 { 00386 00387 QString obj = str.section(' ', 2, 2); 00388 bool objKnown = itsSceneUnderstanding->biasFor(obj.ascii()); 00389 00390 if (objKnown ) //we know about this object 00391 { 00392 msgLabel->setText(QString("Finding ") + obj); 00393 LINFO("Biasing for %s", obj.ascii()); 00394 evolveBrain(); 00395 } else { 00396 msgLabel->setText(QString("I am sorry, but I dont know anything about ") + obj); 00397 } 00398 updateDisplay(); 00399 return ; 00400 00401 } 00402 00403 //prolog interface 00404 if (str.startsWith("consult ")) //load a prolog file 00405 { 00406 QString KBFile = str.section(' ', 1, 1); 00407 msgLabel->setText(QString("Loading KB: ") + KBFile); 00408 00409 LINFO("Consulting with %s", KBFile.ascii()); 00410 if( itsProlog->consult((char*)KBFile.ascii()) ) 00411 { 00412 msgLabel->setText(QString("KB Loaded: ") + KBFile); 00413 } else { 00414 msgLabel->setText(QString("Can not load KB: ") + KBFile); 00415 } 00416 00417 return; 00418 } 00419 00420 00421 if (str.startsWith("go")) //load a prolog file 00422 { 00423 00424 std::string sceneType = itsSceneUnderstanding->highOrderRec(); 00425 updateDisplay(); 00426 00427 QString msg; 00428 msg = QString("Scene is: ") + sceneType; 00429 msgLabel->setText(msg); 00430 00431 return; 00432 00433 } 00434 00435 msgLabel->setText(QString("I did not understand!")); 00436 00437 00438 } 00439 00440 00441 00442