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