VisLearner.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00019 /************************************************************************
00020                         VisLearner.cpp - Copyright klank
00021 
00022 **************************************************************************/
00023 
00024 #include "VisLearner.h"
00025 #include "boost/date_time/posix_time/posix_time.hpp"
00026 
00027 
00028 extern volatile bool g_stopall;
00029 
00030 #ifdef BOOST_THREAD
00031 #include "boost/bind.hpp"
00032 #define BOOST(A) A
00033 #else
00034 #define BOOST(A)
00035 #endif
00036 
00037 
00038 #define XML_NODE_REFINEMENTALGORITHMS "RefineAlgs"
00039 #define XML_NODE_PROVINGALGORITHMS "ProveAlgs"
00040 
00041 using namespace cop;
00042 
00043 
00044 bool VisLearner::s_Learning = false;
00045 bool VisLearner::s_Running = true;
00046 // Constructors/Destructors
00047 //
00048 
00049 VisLearner::VisLearner ( XMLTag* tag, SignatureDB& sigDB, ImageInputSystem& imgSys
00050 #ifdef LOGFILE
00051             ,LogFile& log
00052 #endif /*LOGFILE*/
00053             , bool bLearning) :
00054                 m_signatureDB(sigDB),
00055                 m_imageSys(imgSys),
00056                 m_stats(tag != NULL ? tag->GetChild(XML_NODE_STATISTICS) : NULL)
00057 #ifdef LOGFILE
00058                 ,m_refinements(tag != NULL ? tag->GetChild(XML_NODE_REFINEMENTALGORITHMS) : NULL, log)
00059                 ,m_checks(tag != NULL ? tag->GetChild(XML_NODE_PROVINGALGORITHMS): NULL, log)
00060 #else  /*LOGFILE*/
00061     ,m_refinements(tag != NULL ? tag->GetChild(XML_NODE_REFINEMENTALGORITHMS) : NULL)
00062                 ,m_checks(tag != NULL ? tag->GetChild(XML_NODE_PROVINGALGORITHMS): NULL)
00063 #endif /*LOGFILE*/
00064 {
00065 #ifdef BOOST_THREAD
00066   bLearning = false;
00067         if(bLearning)
00068                 m_learningThread = new boost::thread( boost::bind(&VisLearner::threadfunc, this) ) ;
00069         else
00070                 m_learningThread = NULL;
00071 #else
00072 #endif
00073        m_refinements.SetName(XML_NODE_VISLEARNER);
00074        m_checks.SetName(XML_NODE_VISLEARNER);
00075 }
00076 
00077 VisLearner::~VisLearner ( )
00078 {
00079         s_Running = false;
00080 #ifdef BOOST_THREAD
00081         if(m_learningThread != NULL)
00082                 m_learningThread->join();
00083         delete m_learningThread;
00084 #else
00085 #endif
00086 }
00087 
00088 //
00089 // Methods
00090 //
00091  void VisLearner::threadfunc()
00092 {
00093   std::vector<Sensor*> cam = m_imageSys.GetAllSensors();
00094   while(VisLearner::s_Running && !g_stopall)
00095   {
00096     size_t n = m_taskList.size();
00097     if(n > 0)
00098     {
00099       s_Learning = true; TaskID type = m_taskList[0].first;
00100       Signature* sig = m_taskList[0].second;
00101       int numOfObjects = 1;
00102       double qualityMeasure = 0.0;
00103 #ifdef _DEBUG
00104       printf("New Learning Task\n");
00105 #endif /*_DEBUG*/
00106       RefineAlgorithm* refalg = (RefineAlgorithm*)m_refinements.BestAlgorithm(type, *sig, cam);
00107       if(refalg != NULL)
00108       {
00109           printf("Alg selected: %s\n", refalg->GetName().c_str());
00110           try
00111           {
00112             Descriptor* d = refalg->Perform(cam, sig->GetObjectPose(), *sig, numOfObjects, qualityMeasure);
00113             if(d == NULL)
00114               break;
00115             sig->SetElem(d);
00116           }
00117           catch(char const* error_text)
00118           {
00119             printf("Refinement failed due to: %s\n", error_text);
00120           }
00121       }
00122       else
00123       {
00124          ProveAlgorithm* provalg = (ProveAlgorithm*)m_checks.BestAlgorithm(type, *sig, cam);
00125          if(provalg != NULL)
00126          {
00127            printf("Alg selected: %s\n", refalg->GetName().c_str());
00128            try
00129            {
00130              ImprovedPose res = provalg->Perform(cam, sig->GetObjectPose(), *sig, numOfObjects, qualityMeasure);
00131              if(res.first != NULL)
00132              {
00133                 res.first->m_qualityMeasure = res.second;
00134              }
00135              sig->SetPose(res.first);
00136 #ifdef _DEBUG
00137              printf("Evaluation results in: %f\n", res.second);
00138 #endif /*_DEBUG*/
00139              if(res.second > 0.5)
00140              {
00141 #ifdef _DEBUG
00142                printf("This is good!\n");
00143 #endif /*_DEBUG*/
00144              }
00145            }
00146            catch(char const* error_text)
00147            {
00148 #ifdef _DEBUG
00149              printf("Evaluation failed due to: %s\n", error_text);
00150 #endif /*_DEBUG*/
00151            }
00152         }
00153      }
00154      m_taskList.erase(m_taskList.begin());
00155      s_Learning = false;
00156    }
00157    else
00158    {
00159      //printf("Learning Thread Sleeps \n\n\n");
00160       BOOST(boost::system_time t);
00161       BOOST(t = get_system_time());
00162       t += boost::posix_time::seconds(1);
00163       boost::thread::sleep(t);
00164       return;
00165     }
00166   }
00167 }
00168 
00169 void VisLearner::AddAlgorithm(Algorithm<Descriptor*>* alg)
00170 {
00171         m_refinements.AddAlgorithm(alg, ELEM, 1.0, 0.0);
00172 }
00173 void VisLearner::AddAlgorithm(Algorithm<ImprovedPose>* alg)
00174 {
00175         m_checks.AddAlgorithm(alg, ELEM, 1.0, 0.0);
00176 }
00177 
00184 SignatureLocations_t VisLearner::RefineObject (PossibleLocations_t* lastKnownPoses, PerceptionPrimitive &visPrim, int &numOfObjects)
00185 {
00186   SignatureLocations_t ret_vec;
00187   Signature& sig = *visPrim.GetSignature();
00188   TaskID type = 0x100;
00189   PossibleLocations_t::const_iterator it = (*lastKnownPoses).begin();
00190   std::vector<Sensor*> sensors;
00191   for(;it!=(*lastKnownPoses).end(); it++)
00192   {
00193     RelPose* lastKnownPose = (*it).first;
00194     try
00195     {
00196       sensors = m_imageSys.GetBestSensor(*lastKnownPose);
00197     }
00198     catch(const char* text)
00199     {
00200       printf ("Error selecting a camera: %s\n", text);
00201       continue;
00202     }
00203 
00204     std::vector<Algorithm<Descriptor*>*> refalg_list = m_refinements.BestAlgorithmList(type, sig, sensors);
00205     printf("The list of algorithms for refinement contains %ld algs\n", refalg_list.size());
00206     if(refalg_list.size() > 0)
00207     {
00209       bool some_success = false;
00210       std::vector<Algorithm<Descriptor*>*>::const_iterator iter = refalg_list.begin();
00211       for(;iter != refalg_list.end(); iter++)
00212       {
00213         RefineAlgorithm* refalg = (RefineAlgorithm*)(*iter);
00214         try
00215         {
00216           double qualityMeasure;
00217           boost::system_time t0;
00218           boost::system_time t1;
00219           t0 = boost::get_system_time();
00220           Descriptor* d = refalg->Perform(sensors, lastKnownPose, sig, numOfObjects, qualityMeasure);
00221           t1 = boost::get_system_time();
00222           boost::posix_time::time_duration td = t1 - t0;
00223           printf("Calc time: %s\n", boost::posix_time::to_simple_string(td).c_str() );
00224           /*sensors[0]->Show(-1);*/
00225           if(d != NULL)
00226           {
00227             d->SetLastPerceptionPrimitive(visPrim.GetID());
00228             d->Evaluate(qualityMeasure, 1.0);
00229 
00230             sig.SetElem(d);
00231             RelPose* pose = d->GetLastMatchedPose();
00232             if(pose != NULL)
00233               lastKnownPose = pose;
00234 
00235             /*d->Show(lastKnownPose, sensors[0] );*/
00236             lastKnownPose->m_qualityMeasure = qualityMeasure;
00237             visPrim.AddResult(refalg, &m_refinements, d->m_ID, lastKnownPose->m_qualityMeasure,((double)((t1 - t0).total_milliseconds()) /  1000.0));
00238             /*m_refinements.EvalAlgorithm(refalg, lastKnownPose->m_qualityMeasure, ((double)((t1 - t0).total_milliseconds()) /  1000.0), &sig);*/
00239             some_success = true;
00240           }
00241           else
00242           {
00243             /*No result for this object, remeber*/
00244             /*m_refinements.EvalAlgorithm(refalg, 0.0, ((double)((t1 - t0).total_milliseconds()) /  1000.0), &sig);*/
00245           }
00246         }
00247         catch(char const* error_text)
00248         {
00249           /*Crash for this object, remeber*/
00250           m_refinements.EvalAlgorithm(refalg, 0.0, 1.0, &sig);
00251           printf("Refinement failed due to: %s\n", error_text);
00252         }
00253       }
00254       if(some_success)
00255       {
00256         m_signatureDB.AddSignature(&sig);
00257         ret_vec.push_back(std::pair<RelPose*, Signature*>(lastKnownPose, &sig));
00258       }
00259     }
00260   }
00261   return ret_vec;
00262 }
00263 
00264 
00265 SignatureLocations_t VisLearner::ProoveObject (PossibleLocations_t* lastKnownPoses, PerceptionPrimitive &visPrim, int &numOfObjects)
00266 {
00267   SignatureLocations_t ret_vec;
00268   Signature& sig = *visPrim.GetSignature();
00269   TaskID type = 0x100;
00270   PossibleLocations_t::const_iterator it = (*lastKnownPoses).begin();
00271   std::vector<Sensor*> sensors;
00272   for(;it!=(*lastKnownPoses).end(); it++)
00273   {
00274     RelPose* lastKnownPose = (*it).first;
00275     try
00276     {
00277       sensors = m_imageSys.GetBestSensor(*lastKnownPose);
00278     }
00279     catch(const char* text)
00280     {
00281       printf ("Error selecting a camera: %s\n", text);
00282       continue;
00283     }
00284     ProveAlgorithm* provalg = (ProveAlgorithm*)m_checks.BestAlgorithm(type, sig, sensors);
00285     if(provalg != NULL)
00286     {
00287       printf("Alg selected: %s\n", provalg->GetName().c_str());
00288       try
00289       {
00290         double qualityMeasure;
00291        ImprovedPose ipose = provalg->Perform(sensors, lastKnownPose, sig, numOfObjects, qualityMeasure);
00293         #ifdef _DEBUG
00294         printf("Evaluation results in: %f\n", ipose.second);
00295         #endif /*_DEBUG*/
00296         if(ipose.second > 0.5)
00297         {
00298         #ifdef _DEBUG
00299           printf("This is good!\n");
00300         #endif /*_DEBUG*/
00301         }
00302         ret_vec.push_back(std::pair<RelPose*, Signature*>(lastKnownPose, &sig));
00303       }
00304       catch(char const* error_text)
00305       {
00306 #ifdef _DEBUG
00307          printf("Evaluation failed due to: %s\n", error_text);
00308 #endif /*_DEBUG*/
00309       }
00310     }
00311   }
00312   return ret_vec;
00313 }
00314 
00319 double VisLearner::RefineObject (Signature& sig)
00320 {
00321         if(s_Learning == true)
00322                 return 0.0;
00323         TaskID id = ELEM;
00324 
00325         m_taskList.push_back(std::pair<TaskID, Signature*>
00326                         (id, &sig));
00327 
00328 #ifndef BOOST_THREAD
00329         threadfunc();
00330 #endif
00331         return m_taskList.size();
00332 }
00333 
00334 
00335 
00336 XMLTag* VisLearner::Save()
00337 {
00338         XMLTag* tag = new XMLTag(XML_NODE_VISLEARNER);
00339         tag->AddChild(m_stats.Save());
00340         tag->AddChild(m_refinements.Save(XML_NODE_REFINEMENTALGORITHMS));
00341         tag->AddChild(m_checks.Save(XML_NODE_PROVINGALGORITHMS));
00342         return tag;
00343 }
00344 
00349 Signature* VisLearner::GetObjectSignature (int index )
00350 {
00351         return NULL;
00352 }
00353 
00354 #ifndef WIN32
00355 #include "AlgorithmSelector.hpp"
00356 template class AlgorithmSelector<Descriptor* >;
00357 template class AlgorithmSelector<double >;
00358 #else
00359 #include "AlgorithmSelector.hpp"
00360 template AlgorithmSelector<Descriptor* >;
00361 template AlgorithmSelector<double >;
00362 #endif
00363 


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Mon Oct 6 2014 10:48:45