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
00066 #include <ros/ros.h>
00067 #include <nodelet/nodelet.h>
00068 #include <ros/package.h>
00069
00070 #include <actionlib/server/simple_action_server.h>
00071
00072
00073 #include <sensor_msgs/Image.h>
00074 #include <sensor_msgs/PointCloud2.h>
00075
00076 #include <cob_people_detection_msgs/PeopleDetectionArray.h>
00077
00078
00079 #include <message_filters/subscriber.h>
00080 #include <message_filters/synchronizer.h>
00081 #include <message_filters/sync_policies/approximate_time.h>
00082 #include <image_transport/image_transport.h>
00083 #include <image_transport/subscriber_filter.h>
00084
00085
00086 #include <cob_people_detection/Recognition.h>
00087
00088
00089 #include <actionlib/server/simple_action_server.h>
00090 #include <cob_people_detection/TrainContinuousAction.h>
00091 #include <cob_people_detection/TrainCaptureSampleAction.h>
00092 #include <cob_people_detection/RecognizeAction.h>
00093 #include <cob_people_detection/LoadAction.h>
00094 #include <cob_people_detection/SaveAction.h>
00095 #include <cob_people_detection/ShowAction.h>
00096
00097
00098 #include <opencv/cv.h>
00099 #include <opencv/ml.h>
00100 #include <cv_bridge/cv_bridge.h>
00101 #include <sensor_msgs/image_encodings.h>
00102
00103
00104 #include <pcl/point_types.h>
00105 #include <pcl_ros/point_cloud.h>
00106
00107
00108 #include "tinyxml.h"
00109
00110
00111 #include <boost/bind.hpp>
00112 #include <boost/thread/mutex.hpp>
00113 #include "boost/filesystem/operations.hpp"
00114 #include "boost/filesystem/convenience.hpp"
00115 #include "boost/filesystem/path.hpp"
00116 namespace fs = boost::filesystem;
00117
00118
00119 #ifdef __LINUX__
00120 #else
00121 #include "cob_vision_ipa_utils/MathUtils.h"
00122 #include "cob_sensor_fusion/ColoredPointCloudSequence.h"
00123 #endif
00124
00125 #include "cob_people_detection/PeopleDetector.h"
00126
00127 #include <sstream>
00128 #include <string>
00129 #include <vector>
00130 #include <set>
00131
00132 namespace ipa_PeopleDetector {
00133
00134 typedef actionlib::SimpleActionServer<cob_people_detection::TrainContinuousAction> TrainContinuousServer;
00135 typedef actionlib::SimpleActionServer<cob_people_detection::TrainCaptureSampleAction> TrainCaptureSampleServer;
00136 typedef actionlib::SimpleActionServer<cob_people_detection::RecognizeAction> RecognizeServer;
00137 typedef actionlib::SimpleActionServer<cob_people_detection::LoadAction> LoadServer;
00138 typedef actionlib::SimpleActionServer<cob_people_detection::SaveAction> SaveServer;
00139 typedef actionlib::SimpleActionServer<cob_people_detection::ShowAction> ShowServer;
00140
00141
00142
00143
00144 class CobFaceDetectionNodelet : public nodelet::Nodelet
00145 {
00146 protected:
00147 message_filters::Subscriber<sensor_msgs::PointCloud2> shared_image_sub_;
00148 image_transport::ImageTransport* it_;
00149 image_transport::SubscriberFilter color_camera_image_sub_;
00150 image_transport::Publisher face_detection_image_pub_;
00151 message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::Image> >* sync_pointcloud_;
00152 message_filters::Connection sync_pointcloud_callback_connection_;
00153 ros::Publisher face_position_publisher_;
00154
00155 ros::NodeHandle node_handle_;
00156
00157 #ifdef __LINUX__
00158 cv::Mat color_image_;
00159 cv::Mat range_image_;
00160 #else
00161 ipa_SensorFusion::ColoredPointCloudPtr colored_pc_;
00162 #endif
00163
00164 std::string directory_;
00165 bool run_pca_;
00166 int filename_;
00167
00168 std::vector<cv::Mat> face_images_;
00169 std::vector<std::string> id_;
00170 std::vector<std::string> id_unique_;
00171
00172 int n_eigens_;
00173 std::vector<cv::Mat> eigen_vectors_;
00174 cv::Mat eigen_val_mat_;
00175 cv::Mat avg_image_;
00176 cv::Mat projected_train_face_mat_;
00177 cv::Mat face_class_avg_projections_;
00178 cv::SVM person_classifier_;
00179
00180 PeopleDetector* people_detector_;
00181 int threshold_;
00182 int threshold_fs_;
00183 std::vector<cv::Rect> color_faces_;
00184 std::vector<cv::Rect> range_faces_;
00185 std::set<size_t> range_face_indices_with_color_face_detection_;
00186
00187
00188 ros::ServiceServer recognize_service_server_;
00189
00190
00191 TrainContinuousServer* train_continuous_server_;
00192 TrainCaptureSampleServer* train_capture_sample_server_;
00193 RecognizeServer* recognize_server_;
00194 LoadServer* load_server_;
00195 SaveServer* save_server_;
00196 ShowServer* show_server_;
00197 bool occupied_by_action_;
00198 bool recognize_server_running_;
00199 bool train_continuous_server_running_;
00200 bool capture_training_face_;
00201 int number_training_images_captured_;
00202
00203 bool do_recognition_;
00204 bool display_;
00205
00206 std::string current_training_id_;
00207 boost::timed_mutex action_mutex_;
00208
00209
00210 static const double FACE_SIZE_MIN_M = 0.12;
00211 static const double FACE_SIZE_MAX_M = 0.35;
00212 static const double MAX_FACE_Z_M = 8.0;
00213
00214
00215 double face_size_min_m_;
00216 double face_size_max_m_;
00217 double max_face_z_m_;
00218 bool fill_unassigned_depth_values_;
00219 bool reason_about_3dface_size_;
00220
00221 public:
00222
00223 CobFaceDetectionNodelet();
00224
00225 ~CobFaceDetectionNodelet();
00226
00228 void onInit();
00229
00232 unsigned long init();
00233
00238 unsigned long detectFaces(cv::Mat& xyz_image, cv::Mat& color_image);
00239
00245 unsigned long recognizeFace(cv::Mat& color_image, std::vector<int>& index);
00246
00251 unsigned long addFace(cv::Mat& image, std::string id);
00252
00255 unsigned long PCA();
00256
00257 unsigned long saveAllData();
00258
00261 unsigned long saveTrainingData();
00262
00263 unsigned long saveRecognizerData();
00264
00267 unsigned long loadAllData();
00268
00272 unsigned long loadTrainingData(bool runPCA);
00273
00276 unsigned long loadRecognizerData();
00277
00282 unsigned long getEigenface(cv::Mat& eigenface, int index);
00283
00287 unsigned long showAVGImage(cv::Mat& avgImage);
00288
00292 unsigned long saveRangeTrainImages(cv::Mat& xyz_image);
00293
00294 unsigned long getMeasurement(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, const sensor_msgs::Image::ConstPtr& color_image_msg);
00295
00296 unsigned long convertColorImageMessageToMat(const sensor_msgs::Image::ConstPtr& color_image_msg, cv_bridge::CvImageConstPtr& color_image_ptr, cv::Mat& color_image);
00297
00298 unsigned long convertPclMessageToMat(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, cv::Mat& depth_image);
00299
00301 std::string getLabel(int index);
00302
00304 void recognizeCallback(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, const sensor_msgs::Image::ConstPtr& color_image_msg);
00305
00306
00307 bool recognizeServiceServerCallback(cob_people_detection::Recognition::Request &req, cob_people_detection::Recognition::Response &res);
00308
00310 void recognizeServerCallback(const cob_people_detection::RecognizeGoalConstPtr& goal);
00311
00312
00313 void trainContinuousCallback(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, const sensor_msgs::Image::ConstPtr& color_image_msg);
00314
00315 void trainContinuousServerCallback(const cob_people_detection::TrainContinuousGoalConstPtr& goal);
00316
00317 void trainCaptureSampleServerCallback(const cob_people_detection::TrainCaptureSampleGoalConstPtr& goal);
00318
00319 void loadServerCallback(const cob_people_detection::LoadGoalConstPtr& goal);
00320
00321 void saveServerCallback(const cob_people_detection::SaveGoalConstPtr& goal);
00322
00323 void showServerCallback(const cob_people_detection::ShowGoalConstPtr& goal);
00324
00325 unsigned long loadParameters(const char* iniFileName);
00326 };
00327
00328 };
00329
00330 #endif // _FACE_DETECTION_
00331