$search
00001 00002 00003 00004 00005 00006 00007 #ifdef __LINUX__ 00008 #include "cob_people_detection/CuiPeopleDetector.h" 00009 #else 00010 #include "cob_vision/cob_people_detection/common/include/cob_people_detection/CuiPeopleDetector.h" 00011 #endif 00012 00013 00014 using namespace ipa_PeopleDetector; 00015 00016 00017 CuiPeopleDetector::CuiPeopleDetector() 00018 { 00019 m_DetectorControlFlow = new PeopleDetectorControlFlow(); 00020 } 00021 00022 CuiPeopleDetector::~CuiPeopleDetector() 00023 { 00024 delete m_DetectorControlFlow; 00025 } 00026 00027 unsigned long CuiPeopleDetector::Init() 00028 { 00029 if (m_DetectorControlFlow->Init("ConfigurationFiles/") & ipa_Utils::RET_FAILED) 00030 { 00031 std::cerr << "ERROR - CuiPeopleDetector::Init:" << std::endl; 00032 std::cerr << "\t ... Error while initializing control flow\n"; 00033 return ipa_Utils::RET_FAILED; 00034 } 00035 return ipa_Utils::RET_OK; 00036 } 00037 00038 unsigned long CuiPeopleDetector::ConsoleGUI() 00039 { 00040 // print out menu options 00041 std::cout << "INFO - PeopleDetector::ConsoleGUI:" << std::endl; 00042 std::cout << "\t ... Select one of the following options:" << std::endl << std::endl; 00043 00044 std::cout << "-------------------- People Detector functions ---------------------" << std::endl; 00045 std::cout << std::endl; 00046 std::cout << "t: Train new Person (Faces)" << std::endl; 00047 std::cout << "r: Recognize Persons" << std::endl; 00048 std::cout << "e: Show Eigenfaces" << std::endl; 00049 std::cout << "a: Show AVG image" << std::endl; 00050 std::cout << "s: Save training data" << std::endl; 00051 std::cout << "l: Load training data" << std::endl; 00052 00053 std::cout << std::endl << std::endl; 00054 std::cout << "q: Quit" << std::endl; 00055 00056 std::string cmd; 00057 std::cin >> cmd; 00058 00059 // Train 00060 if(strcmp("t", cmd.c_str())==0) return Train(); 00061 00062 // Recognize 00063 if(strcmp("r", cmd.c_str())==0) return Recognize(); 00064 00065 // Save 00066 if(strcmp("s", cmd.c_str())==0) return m_DetectorControlFlow->SaveTrainingData(); 00067 00068 // Load 00069 if(strcmp("l", cmd.c_str())==0) return m_DetectorControlFlow->LoadTrainingData(); 00070 00071 // Eigenfaces 00072 if(strcmp("e", cmd.c_str())==0) return ShowEigenfaces(); 00073 00074 // AVGImage 00075 if(strcmp("a", cmd.c_str())==0) return ShowAvgImage(); 00076 00077 // Exit 00078 if(strcmp("q", cmd.c_str())==0) return ipa_Utils::RET_FAILED; 00079 00080 return ipa_Utils::RET_OK; 00081 }; 00082 00083 unsigned long CuiPeopleDetector::Train() 00084 { 00085 int cmd; 00086 00087 cv::Mat colorImage_8U3; 00088 std::string id; // Id of trained face 00089 00090 std::cout << "Please enter your name: "; 00091 std::cin >> id; 00092 00093 std::cout << "INFO - PeopleDetector::ConsoleGUI:" << std::endl; 00094 std::cout << "\t ... Please press 'n' to acquire the next image" << std::endl; 00095 std::cout << "\t ... or press 's' to save the image" << std::endl; 00096 std::cout << "\t ... or press 'q' to quit!" << std::endl; 00097 00098 cv::namedWindow("Face Detector"); 00099 00100 ipa_SensorFusion::ColoredPointCloudPtr pc; 00101 00102 int count=0; 00103 00104 while(true) 00105 { 00106 cmd = cvWaitKey(); 00107 00108 // acquire next image 00109 if(cmd == 'n') 00110 { 00111 if (m_DetectorControlFlow->GetColoredPointCloud(pc, m_DetectorControlFlow->GetPCMode(), 1) & ipa_Utils::RET_FAILED) 00112 { 00113 std::cerr << "ERROR - CuiPeopleDetector::Train:" << std::endl; 00114 std::cerr << "\t ... Could not get Colored Point Cloud" << std::endl; 00115 return ipa_Utils::RET_FAILED; 00116 } 00117 00118 //DWORD start = timeGetTime(); 00119 m_DetectorControlFlow->DetectFaces(pc); 00120 std::cout << "INFO - CuiPeopleDetector::Train:" << std::endl; 00121 //std::cout << "\t ... Detection Time: " << (timeGetTime() - start) << std::endl; 00122 count++; 00123 00124 (pc->GetColorImage()).copyTo(colorImage_8U3); 00125 00126 for(int i=0; i<(int)m_DetectorControlFlow->m_colorFaces.size(); i++) 00127 { 00128 cv::Rect face = m_DetectorControlFlow->m_colorFaces[i]; 00129 cv::rectangle(colorImage_8U3, cv::Point(face.x, face.y), cv::Point(face.x + face.width, face.y + face.height), CV_RGB( 0, 255, 0 ), 2, 8, 0); 00130 } 00131 00132 cv::imshow("Face Detector", colorImage_8U3); 00133 } 00134 00135 // save image 00136 if(cmd == 's') 00137 { 00138 // Check if there is more than one face detected 00139 if(m_DetectorControlFlow->m_colorFaces.size() > 1) 00140 { 00141 std::cout << "WARNING - CuiPeopleDetector::ConsoleGUI:" << std::endl; 00142 std::cout << "\t ... More than one faces are detected in image. Please try again." << std::endl; 00143 continue; 00144 } 00145 00146 // Check if there is less than one face detected 00147 if(m_DetectorControlFlow->m_colorFaces.size() < 1) 00148 { 00149 std::cout << "WARNING - CuiPeopleDetector::ConsoleGUI:" << std::endl; 00150 std::cout << "\t ... Less than one faces are detected in image. Please try again." << std::endl; 00151 continue; 00152 } 00153 00154 m_DetectorControlFlow->AddFace(pc->GetColorImage(), id); 00155 std::cout << "INFO - CuiPeopleDetector::ConsoleGUI:" << std::endl; 00156 std::cout << "\t ... Face saved. (" << m_DetectorControlFlow->m_faceImages.size() << ")" << std::endl; 00157 } 00158 00159 // quit 00160 if(cmd == 'q') 00161 { 00162 // Exit 00163 break; 00164 } 00165 } 00166 00167 cvDestroyAllWindows(); 00168 00169 return ipa_Utils::RET_OK; 00170 } 00171 00172 unsigned long CuiPeopleDetector::Recognize() 00173 { 00174 int cmd; 00175 cv::Mat colorImage_8U3; 00176 00177 std::cout << "INFO - PeopleDetector::ConsoleGUI:" << std::endl; 00178 std::cout << "\t ... Please press 'n' to acquire the next image" << std::endl; 00179 std::cout << "\t ... or press 'q' to quit!" << std::endl; 00180 00181 m_DetectorControlFlow->PCA(); 00182 00183 if(m_DetectorControlFlow->m_faceImages.size() < 2) 00184 { 00185 std::cout << "WARNING - PeopleDetector::ConsoleGUI:" << std::endl; 00186 std::cout << "\t ... Less than two images are trained.\n"; 00187 return ipa_Utils::RET_OK; 00188 } 00189 00190 cv::namedWindow("Face Detector"); 00191 00192 while(true) 00193 { 00194 cmd = cv::waitKey(); 00195 if(cmd == 'n') 00196 { 00197 ipa_SensorFusion::ColoredPointCloudPtr pc; 00198 00199 if (m_DetectorControlFlow->GetColoredPointCloud(pc, m_DetectorControlFlow->GetPCMode(), 1) & ipa_Utils::RET_FAILED) 00200 { 00201 std::cerr << "ERROR - CuiPeopleDetector::Recognize:" << std::endl; 00202 std::cerr << "\t ... Could not get Colored Point Cloud" << std::endl; 00203 return ipa_Utils::RET_FAILED; 00204 } 00205 00206 //DWORD start = timeGetTime(); 00207 00208 m_DetectorControlFlow->DetectFaces(pc); 00209 00210 (pc->GetColorImage()).copyTo(colorImage_8U3); 00211 00212 std::vector<int> index; 00213 m_DetectorControlFlow->RecognizeFace(pc, index); 00214 std::cout << "INFO - CuiPeopleDetector::Recognize:" << std::endl; 00215 //std::cout << "WARNING: Recognition deactivated in code!\n"; 00216 //for (int i=0; i<(int)m_DetectorControlFlow->m_colorFaces.size(); i++) index.push_back(-1); 00217 //std::cout << "\t ... Recognize Time: " << (timeGetTime() - start) << std::endl; 00218 00219 for(int i=0; i<(int)m_DetectorControlFlow->m_rangeFaces.size(); i++) 00220 { 00221 cv::Rect face = m_DetectorControlFlow->m_rangeFaces[i]; 00222 00223 cv::rectangle(colorImage_8U3, cv::Point(face.x, face.y), cv::Point(face.x + face.width, face.y + face.height), CV_RGB(0, 0, 255), 2, 8, 0); 00224 } 00225 00226 for(int i=0; i<(int)m_DetectorControlFlow->m_colorFaces.size(); i++) 00227 { 00228 cv::Rect face = m_DetectorControlFlow->m_colorFaces[i]; 00229 00230 cv::rectangle(colorImage_8U3, cv::Point(face.x, face.y), cv::Point(face.x + face.width, face.y + face.height), CV_RGB(0, 255, 0), 2, 8, 0); 00231 00232 std::stringstream tmp; 00233 switch(index[i]) 00234 { 00235 case -1: 00236 // Distance to face class is too high 00237 tmp << "Unknown"; 00238 cv::putText(colorImage_8U3, tmp.str().c_str(), cv::Point(face.x,face.y+face.height+25), cv::FONT_HERSHEY_PLAIN, 2, CV_RGB( 255, 0, 0 ), 2); 00239 break; 00240 case -2: 00241 // Distance to face space is too high 00242 tmp << "No face"; 00243 cv::putText(colorImage_8U3, tmp.str().c_str(), cv::Point(face.x,face.y+face.height+25), cv::FONT_HERSHEY_PLAIN, 2, CV_RGB( 255, 0, 0 ), 2); 00244 break; 00245 default: 00246 // Face classified 00247 tmp << m_DetectorControlFlow->m_idUnique[index[i]].c_str(); 00248 cv::putText(colorImage_8U3, tmp.str().c_str(), cv::Point(face.x,face.y+face.height+25), cv::FONT_HERSHEY_PLAIN, 2, CV_RGB( 0, 255, 0 ), 2); 00249 } 00250 } 00251 00252 cv::imshow("Face Detector", colorImage_8U3); 00253 } 00254 00255 if(cmd == 'q') 00256 { 00257 // Exit 00258 break; 00259 } 00260 } 00261 00262 // Clear 00263 cvDestroyAllWindows(); 00264 00265 return ipa_Utils::RET_OK; 00266 } 00267 00268 unsigned long CuiPeopleDetector::ShowEigenfaces() 00269 { 00270 cv::Mat eigenface; 00271 00272 cv::namedWindow("Eigenfaces"); 00273 00274 m_DetectorControlFlow->PCA(); 00275 00276 for(int i=0; i<(int)m_DetectorControlFlow->m_faceImages.size()-1; i++) 00277 { 00278 m_DetectorControlFlow->GetEigenface(eigenface, i); 00279 cv::imshow("Eigenfaces", eigenface); 00280 00281 int cmd = cv::waitKey(); 00282 if(cmd == 'q') 00283 { 00284 // Exit 00285 cvDestroyAllWindows(); 00286 break; 00287 } 00288 } 00289 00290 cvDestroyAllWindows(); 00291 00292 return ipa_Utils::RET_OK; 00293 } 00294 unsigned long CuiPeopleDetector::ShowAvgImage() 00295 { 00296 cv::Mat avgImage(100, 100, CV_8UC1); 00297 00298 m_DetectorControlFlow->PCA(); 00299 00300 m_DetectorControlFlow->ShowAVGImage(avgImage); 00301 00302 cv::namedWindow("AVGImage"); 00303 imshow("AVGImage", avgImage); 00304 cv::waitKey(); 00305 00306 cvDestroyAllWindows(); 00307 00308 return ipa_Utils::RET_OK; 00309 }