RecognizerThread.cpp
Go to the documentation of this file.
00001 
00002 #include <blort/ThreadObject/RecognizerThread.h>
00003 #include <stdexcept>
00004 #include <opencv2/legacy/compat.hpp>
00005 
00006 using namespace std;
00007 
00008 CRecognizerThread::CRecognizerThread(const blortRecognizer::CameraParameter& params, std::string config_root)
00009 {
00010         m_mutex.Lock();
00011                 m_quit = false;
00012                 m_params = params;
00013                 this->config_root = config_root;
00014                 cmd = IDLE;
00015                 m_image = cvCreateImage( cvSize(params.w, params.h), 8, 3 );
00016         m_mutex.Unlock();
00017 
00018 }
00019 
00020 CRecognizerThread::~CRecognizerThread()
00021 {
00022         m_mutex.Lock();
00023                 m_quit = true;  // stop running loop
00024         m_mutex.Unlock();
00025         
00026         m_evCmd.Set();
00027         m_evData.Set();
00028         
00029         m_running.Wait();       // wait for running loop to stop
00030         
00031 }
00032 
00033 void CRecognizerThread::Recognize(IplImage* image, std::map<std::string, boost::shared_ptr<TomGine::tgPose> > & poses, std::map<std::string, double> & confs)
00034 {
00035         m_mutex.Lock();
00036                 cvCopyImage(image, m_image);
00037                 m_poses = poses;
00038                 cmd = RECOGNIZE;
00039         m_mutex.Unlock();
00040         
00041         m_evCmd.Set();
00042         m_evData.Wait(); m_evData.Reset();
00043         
00044         m_mutex.Lock();
00045                 poses = m_poses;
00046                 confs = m_confs;
00047         m_mutex.Unlock();
00048 }
00049 
00050 void CRecognizerThread::LearnSifts(IplImage* image,  TomGine::tgModel &model, TomGine::tgPose& pose)
00051 {
00052         m_mutex.Lock();
00053                 cvCopyImage(image, m_image);
00054         m_poses.clear();
00055         m_models.clear();
00056                 m_poses["object"] = boost::shared_ptr<TomGine::tgPose>(new TomGine::tgPose(pose)); //HACK: name hardcoded
00057                 m_models.push_back(boost::shared_ptr<TomGine::tgModel>(new TomGine::tgModel(model)));
00058                 cmd = LEARN;
00059         m_mutex.Unlock();
00060         
00061         m_evCmd.Set();
00062         m_evData.Wait(); m_evData.Reset();
00063 }
00064 
00065 void CRecognizerThread::LoadSiftModel(const std::string& sift_file)
00066 {
00067         m_mutex.Lock();
00068                 m_sift_file = string(sift_file);
00069                 cmd = LOAD;
00070         m_mutex.Unlock();
00071         
00072         m_evCmd.Set();
00073         m_evData.Wait(); m_evData.Reset();
00074 }
00075 
00076 void CRecognizerThread::SaveSiftModel(const std::string& sift_file)
00077 {
00078         m_mutex.Lock();
00079                 m_sift_file = string(sift_file);
00080                 cmd = SAVE;
00081         m_mutex.Unlock();
00082         
00083         m_evCmd.Set();
00084         m_evData.Wait(); m_evData.Reset();
00085 }
00086 
00087 void CRecognizerThread::GetSifts(std::vector<blortRecognizer::Siftex>& sl)
00088 {
00089         m_mutex.Lock();
00090                 cmd = GETSIFT;
00091         m_mutex.Unlock();
00092         
00093         m_evCmd.Set();
00094         m_evData.Wait(); m_evData.Reset();
00095         
00096         m_mutex.Lock();
00097                 sl = m_siftexlist;
00098         m_mutex.Unlock();
00099 }
00100 
00101 void CRecognizerThread::GetLastSifts(std::vector<blortRecognizer::Siftex>& sl)
00102 {
00103         m_mutex.Lock();
00104                 cmd = GETLASTSIFT;
00105         m_mutex.Unlock();
00106         
00107         m_evCmd.Set();
00108         m_evData.Wait(); m_evData.Reset();
00109         
00110         m_mutex.Lock();
00111                 sl = m_lastsiftexlist;
00112         m_mutex.Unlock();
00113 }
00114 
00115 BOOL CRecognizerThread::OnTask()
00116 {
00117         m_running.Lock();
00118         
00119         
00120         blortRecognizer::Recognizer3D m_recognizer(m_params, config_root, true, true);
00121         //m_recognizer.setDoUndistort(false);
00122         
00123         while(!m_quit)
00124         {
00125                 switch(cmd)
00126                 {
00127                         
00128                         case RECOGNIZE:
00129                                 m_mutex.Lock();
00130                                         m_recognizer.recognize(m_image, m_poses, m_confs);
00131                                         result = m_recognizer.getImage();
00132                                         m_evData.Set();
00133                                         cmd = IDLE;
00134                                 m_mutex.Unlock();
00135                                 break;
00136                         
00137                         case LEARN:
00138                                 m_mutex.Lock();
00139         m_recognizer.learnSifts(m_image, *m_models[0], *(m_poses.begin()->second));
00140                                         m_evData.Set();
00141                                         cmd = IDLE;
00142                                 m_mutex.Unlock();
00143                                 break;
00144                         
00145                         case LOAD:
00146                                 m_mutex.Lock();
00147                                         m_recognizer.loadModelFromFile(m_sift_file.c_str());
00148                                         m_evData.Set();
00149                                         cmd = IDLE;
00150                                 m_mutex.Unlock();
00151                                 break;
00152                         
00153                         case SAVE:
00154                                 m_mutex.Lock();
00155                                         m_recognizer.saveModelToFile(m_sift_file.c_str());
00156                                         m_evData.Set();
00157                                         cmd = IDLE;
00158                                 m_mutex.Unlock();
00159                                 break;
00160                                 
00161                         case GETSIFT:
00162                                 m_mutex.Lock();
00163                                         m_recognizer.getSifts(m_siftexlist);
00164                                         m_evData.Set();
00165                                         cmd = IDLE;
00166                                 m_mutex.Unlock();
00167                                 break;
00168                                 
00169                         case GETLASTSIFT:
00170                                 m_mutex.Lock();
00171                                         m_recognizer.getLastSifts(m_lastsiftexlist);
00172                                         m_evData.Set();
00173                                         cmd = IDLE;
00174                                 m_mutex.Unlock();
00175                                 break;
00176                         
00177                         case IDLE:
00178                         default:
00179                                 m_evCmd.Wait(); m_evCmd.Reset();
00180                                 break;          
00181                 }
00182         }
00183 
00184         m_running.Unlock();
00185         return TRUE;
00186 }
00187 
00188 
00189 
00190 


blort
Author(s): Thomas Mörwald , Michael Zillich , Andreas Richtsfeld , Johann Prankl , Markus Vincze , Bence Magyar
autogenerated on Wed Aug 26 2015 15:24:12