Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #ifndef _FACE_DETECTION_
00056 #define _FACE_DETECTION_
00057
00058
00059
00060
00061
00062
00063
00064
00065 #include <ros/ros.h>
00066 #include <nodelet/nodelet.h>
00067 #include <ros/package.h>
00068
00069 #include <actionlib/server/simple_action_server.h>
00070
00071
00072 #include <sensor_msgs/Image.h>
00073 #include <sensor_msgs/PointCloud2.h>
00074
00075 #include <cob_perception_msgs/DetectionArray.h>
00076
00077
00078 #include <message_filters/subscriber.h>
00079 #include <message_filters/synchronizer.h>
00080 #include <message_filters/sync_policies/approximate_time.h>
00081 #include <image_transport/image_transport.h>
00082 #include <image_transport/subscriber_filter.h>
00083
00084
00085 #include <cob_people_detection/Recognition.h>
00086
00087
00088 #include <actionlib/server/simple_action_server.h>
00089 #include <cob_people_detection/TrainContinuousAction.h>
00090 #include <cob_people_detection/TrainCaptureSampleAction.h>
00091 #include <cob_people_detection/RecognizeAction.h>
00092 #include <cob_people_detection/LoadAction.h>
00093 #include <cob_people_detection/SaveAction.h>
00094 #include <cob_people_detection/ShowAction.h>
00095
00096
00097 #include <opencv2/opencv.hpp>
00098 #include <cv_bridge/cv_bridge.h>
00099 #include <sensor_msgs/image_encodings.h>
00100
00101
00102 #include <pcl/point_types.h>
00103 #include <pcl_ros/point_cloud.h>
00104
00105
00106 #include "tinyxml.h"
00107
00108
00109 #include <boost/bind.hpp>
00110 #include <boost/thread/mutex.hpp>
00111 #include "boost/filesystem/operations.hpp"
00112 #include "boost/filesystem/convenience.hpp"
00113 #include "boost/filesystem/path.hpp"
00114 namespace fs = boost::filesystem;
00115
00116
00117 #ifdef __LINUX__
00118 #else
00119 #include "cob_vision_ipa_utils/MathUtils.h"
00120 #include "cob_sensor_fusion/ColoredPointCloudSequence.h"
00121 #endif
00122
00123 #include "cob_people_detection/people_detector.h"
00124
00125 #include <sstream>
00126 #include <string>
00127 #include <vector>
00128 #include <set>
00129
00130 namespace ipa_PeopleDetector
00131 {
00132
00133 typedef actionlib::SimpleActionServer<cob_people_detection::TrainContinuousAction> TrainContinuousServer;
00134 typedef actionlib::SimpleActionServer<cob_people_detection::TrainCaptureSampleAction> TrainCaptureSampleServer;
00135 typedef actionlib::SimpleActionServer<cob_people_detection::RecognizeAction> RecognizeServer;
00136 typedef actionlib::SimpleActionServer<cob_people_detection::LoadAction> LoadServer;
00137 typedef actionlib::SimpleActionServer<cob_people_detection::SaveAction> SaveServer;
00138 typedef actionlib::SimpleActionServer<cob_people_detection::ShowAction> ShowServer;
00139
00140
00141
00142 class CobFaceDetectionNodelet: public nodelet::Nodelet
00143 {
00144 protected:
00145 message_filters::Subscriber<sensor_msgs::PointCloud2> shared_image_sub_;
00146 image_transport::ImageTransport* it_;
00147 image_transport::SubscriberFilter color_camera_image_sub_;
00148 image_transport::Publisher face_detection_image_pub_;
00149 message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::Image> >* sync_pointcloud_;
00150 message_filters::Connection sync_pointcloud_callback_connection_;
00151 ros::Publisher face_position_publisher_;
00152
00153 ros::NodeHandle node_handle_;
00154
00155 #ifdef __LINUX__
00156 cv::Mat color_image_;
00157 cv::Mat range_image_;
00158 #else
00159 ipa_SensorFusion::ColoredPointCloudPtr colored_pc_;
00160 #endif
00161
00162 std::string directory_;
00163 bool run_pca_;
00164 int filename_;
00165
00166 std::vector<cv::Mat> face_images_;
00167 std::vector<std::string> id_;
00168 std::vector<std::string> id_unique_;
00169
00170 int n_eigens_;
00171 std::vector<cv::Mat> eigen_vectors_;
00172 cv::Mat eigen_val_mat_;
00173 cv::Mat avg_image_;
00174 cv::Mat projected_train_face_mat_;
00175 cv::Mat face_class_avg_projections_;
00176 #if CV_MAJOR_VERSION == 2
00177 cv::SVM person_classifier_;
00178 #else
00179
00180 cv::ml::SVM person_classifier_;
00181 #endif
00182
00183 PeopleDetector* people_detector_;
00184 int threshold_;
00185 int threshold_fs_;
00186 std::vector<cv::Rect> color_faces_;
00187 std::vector<cv::Rect> range_faces_;
00188 std::set<size_t> range_face_indices_with_color_face_detection_;
00189
00190
00191 ros::ServiceServer recognize_service_server_;
00192
00193
00194 TrainContinuousServer* train_continuous_server_;
00195 TrainCaptureSampleServer* train_capture_sample_server_;
00196 RecognizeServer* recognize_server_;
00197 LoadServer* load_server_;
00198 SaveServer* save_server_;
00199 ShowServer* show_server_;
00200 bool occupied_by_action_;
00201 bool recognize_server_running_;
00202 bool train_continuous_server_running_;
00203 bool capture_training_face_;
00204 int number_training_images_captured_;
00205
00206 bool do_recognition_;
00207 bool display_;
00208
00209 std::string current_training_id_;
00210 boost::timed_mutex action_mutex_;
00211
00212
00213 static const double FACE_SIZE_MIN_M = 0.12;
00214 static const double FACE_SIZE_MAX_M = 0.35;
00215 static const double MAX_FACE_Z_M = 8.0;
00216
00217
00218 double face_size_min_m_;
00219 double face_size_max_m_;
00220 double max_face_z_m_;
00221 bool fill_unassigned_depth_values_;
00222 bool reason_about_3dface_size_;
00223
00224 public:
00225
00226 CobFaceDetectionNodelet();
00227
00228 ~CobFaceDetectionNodelet();
00229
00231 void onInit();
00232
00235 unsigned long init();
00236
00241 unsigned long detectFaces(cv::Mat& xyz_image, cv::Mat& color_image);
00242
00248 unsigned long recognizeFace(cv::Mat& color_image, std::vector<int>& index);
00249
00254 unsigned long addFace(cv::Mat& image, std::string id);
00255
00258 unsigned long PCA();
00259
00260 unsigned long saveAllData();
00261
00264 unsigned long saveTrainingData();
00265
00266 unsigned long saveRecognizerData();
00267
00270 unsigned long loadAllData();
00271
00275 unsigned long loadTrainingData(bool runPCA);
00276
00279 unsigned long loadRecognizerData();
00280
00285 unsigned long getEigenface(cv::Mat& eigenface, int index);
00286
00290 unsigned long showAVGImage(cv::Mat& avgImage);
00291
00295 unsigned long saveRangeTrainImages(cv::Mat& xyz_image);
00296
00297
00298
00299 unsigned long convertColorImageMessageToMat(const sensor_msgs::Image::ConstPtr& color_image_msg, cv_bridge::CvImageConstPtr& color_image_ptr, cv::Mat& color_image);
00300
00301 unsigned long convertPclMessageToMat(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, cv::Mat& depth_image);
00302
00304 std::string getLabel(int index);
00305
00307 void recognizeCallback(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, const sensor_msgs::Image::ConstPtr& color_image_msg);
00308
00309 bool recognizeServiceServerCallback(cob_people_detection::Recognition::Request &req, cob_people_detection::Recognition::Response &res);
00310
00312 void recognizeServerCallback(const cob_people_detection::RecognizeGoalConstPtr& goal);
00313
00314 void trainContinuousCallback(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, const sensor_msgs::Image::ConstPtr& color_image_msg);
00315
00316 void trainContinuousServerCallback(const cob_people_detection::TrainContinuousGoalConstPtr& goal);
00317
00318 void trainCaptureSampleServerCallback(const cob_people_detection::TrainCaptureSampleGoalConstPtr& goal);
00319
00320 void loadServerCallback(const cob_people_detection::LoadGoalConstPtr& goal);
00321
00322 void saveServerCallback(const cob_people_detection::SaveGoalConstPtr& goal);
00323
00324 void showServerCallback(const cob_people_detection::ShowGoalConstPtr& goal);
00325
00326 unsigned long loadParameters(const char* iniFileName);
00327 };
00328
00329 }
00330 ;
00331
00332 #endif // _FACE_DETECTION_