face_detection.h
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2010
00004  *
00005  * Fraunhofer Institute for Manufacturing Engineering
00006  * and Automation (IPA)
00007  *
00008  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009  *
00010  * Project name: care-o-bot
00011  * ROS stack name: cob_vision
00012  * ROS package name: cob_people_detection
00013  * Description:
00014  *
00015  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00016  *
00017  * Author: Jan Fischer, email:jan.fischer@ipa.fhg.de
00018  * Author: Richard Bormann, email: richard.bormann@ipa.fhg.de
00019  * Supervised by:
00020  *
00021  * Date of creation: 03/2011
00022  * ToDo:
00023  *
00024  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00025  *
00026  * Redistribution and use in source and binary forms, with or without
00027  * modification, are permitted provided that the following conditions are met:
00028  *
00029  *     * Redistributions of source code must retain the above copyright
00030  *       notice, this list of conditions and the following disclaimer.
00031  *     * Redistributions in binary form must reproduce the above copyright
00032  *       notice, this list of conditions and the following disclaimer in the
00033  *       documentation and/or other materials provided with the distribution.
00034  *     * Neither the name of the Fraunhofer Institute for Manufacturing
00035  *       Engineering and Automation (IPA) nor the names of its
00036  *       contributors may be used to endorse or promote products derived from
00037  *       this software without specific prior written permission.
00038  *
00039  * This program is free software: you can redistribute it and/or modify
00040  * it under the terms of the GNU Lesser General Public License LGPL as
00041  * published by the Free Software Foundation, either version 3 of the
00042  * License, or (at your option) any later version.
00043  *
00044  * This program is distributed in the hope that it will be useful,
00045  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00046  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00047  * GNU Lesser General Public License LGPL for more details.
00048  *
00049  * You should have received a copy of the GNU Lesser General Public
00050  * License LGPL along with this program.
00051  * If not, see <http://www.gnu.org/licenses/>.
00052  *
00053  ****************************************************************/
00054 
00055 #ifndef _FACE_DETECTION_
00056 #define _FACE_DETECTION_
00057 
00058 //##################
00059 //#### includes ####
00060 
00061 // standard includes
00062 //--
00063 
00064 // ROS includes
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 // ROS message includes
00072 #include <sensor_msgs/Image.h>
00073 #include <sensor_msgs/PointCloud2.h>
00074 //#include <std_msgs/Float32MultiArray.h>
00075 #include <cob_perception_msgs/DetectionArray.h>
00076 
00077 // topics
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 // services
00085 #include <cob_people_detection/Recognition.h>
00086 
00087 // actions
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 // opencv
00097 #include <opencv2/opencv.hpp>
00098 #include <cv_bridge/cv_bridge.h>
00099 #include <sensor_msgs/image_encodings.h>
00100 
00101 // point cloud
00102 #include <pcl/point_types.h>
00103 #include <pcl_ros/point_cloud.h>
00104 
00105 // tiny xml
00106 #include "tinyxml.h"
00107 
00108 // boost
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 // external includes
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 //#### node class ####
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 // OpenCV 3
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         // Services
00191         ros::ServiceServer recognize_service_server_; 
00192 
00193         // Actions
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         //      bool turn_off_recognition_;                                     ///< is set true on quit request during recognition mode
00206         bool do_recognition_; 
00207         bool display_; 
00208 
00209         std::string current_training_id_; 
00210         boost::timed_mutex action_mutex_; 
00211 
00212         // constants
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         // parameters
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         //unsigned long getMeasurement(const sensor_msgs::PointCloud2::ConstPtr& shared_image_msg, const sensor_msgs::Image::ConstPtr& color_image_msg);
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_


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Mon May 6 2019 02:32:06