face_normalizer_node.cpp
Go to the documentation of this file.
00001 
00061 #ifdef __LINUX__
00062 #include "cob_people_detection/face_normalizer_node.h"
00063 #include "cob_vision_utils/GlobalDefines.h"
00064 #else
00065 #endif
00066 
00067 // OpenCV
00068 #include <opencv2/opencv.hpp>
00069 #include <cv_bridge/cv_bridge.h>
00070 #include <sensor_msgs/image_encodings.h>
00071 
00072 // Boost
00073 #include <boost/shared_ptr.hpp>
00074 #include "cob_people_detection/face_normalizer_node.h"
00075 using namespace ipa_PeopleDetector;
00076 
00077 // Prevent deleting memory twice, when using smart pointer
00078 void voidDeleter(const sensor_msgs::Image* const )
00079 {
00080 }
00081 
00082 FaceNormalizerNode::FaceNormalizerNode(ros::NodeHandle nh) :
00083         node_handle_(nh)
00084 {
00085         //node_handle_.param("debug", debug, false);
00086         //std::cout << "debug = " << debug << "\n";
00087 
00088 
00089         // advertise topics
00090         norm_face_publisher_ = node_handle_.advertise<cob_perception_msgs::ColorDepthImageCropArray>("norm_faces", 1);
00091 
00092         // subscribe to head detection topic
00093         face_position_subscriber_ = nh.subscribe("face_positions", 1, &FaceNormalizerNode::facePositionsCallback, this);
00094 }
00095 
00096 FaceNormalizerNode::~FaceNormalizerNode(void)
00097 {
00098 }
00099 
00100 void FaceNormalizerNode::facePositionsCallback(const cob_perception_msgs::ColorDepthImageArray::ConstPtr& face_positions)
00101 {
00102         // receive head and face positions and recognize faces in the face region, finally publish detected and recognized faces
00103 
00104         // --- convert color image patches of head regions and contained face bounding boxes ---
00105         cv_bridge::CvImageConstPtr cv_ptr;
00106         std::vector < cv::Mat > heads_color_images;
00107         heads_color_images.resize(face_positions->head_detections.size());
00108         std::vector < cv::Mat > heads_depth_images;
00109         heads_depth_images.resize(face_positions->head_detections.size());
00110         std::vector < std::vector<cv::Rect>> face_bounding_boxes;
00111         face_bounding_boxes.resize(face_positions->head_detections.size());
00112         std::vector < cv::Rect > head_bounding_boxes;
00113         head_bounding_boxes.resize(face_positions->head_detections.size());
00114         for (unsigned int i = 0; i < face_positions->head_detections.size(); i++)
00115         {
00116                 // color image
00117                 {
00118                         sensor_msgs::ImageConstPtr msgPtr = boost::shared_ptr<sensor_msgs::Image const>(&(face_positions->head_detections[i].color_image), voidDeleter);
00119                         try
00120                         {
00121                                 cv_ptr = cv_bridge::toCvShare(msgPtr, sensor_msgs::image_encodings::BGR8);
00122                         } catch (cv_bridge::Exception& e)
00123                         {
00124                                 ROS_ERROR("cv_bridge exception: %s", e.what());
00125                                 return;
00126                         }
00127                         heads_color_images[i] = cv_ptr->image;
00128                 }
00129 
00130                 // depth image
00131                 sensor_msgs::ImageConstPtr msgPtr = boost::shared_ptr<sensor_msgs::Image const>(&(face_positions->head_detections[i].depth_image), voidDeleter);
00132                 try
00133                 {
00134                         cv_ptr = cv_bridge::toCvShare(msgPtr, sensor_msgs::image_encodings::TYPE_32FC3);
00135                 } catch (cv_bridge::Exception& e)
00136                 {
00137                         ROS_ERROR("cv_bridge exception: %s", e.what());
00138                         return;
00139                 }
00140                 heads_depth_images[i] = cv_ptr->image;
00141 
00142                 // face bounding boxes
00143                 face_bounding_boxes[i].resize(face_positions->head_detections[i].face_detections.size());
00144                 for (uint j = 0; j < face_bounding_boxes[i].size(); j++)
00145                 {
00146                         const cob_perception_msgs::Rect& source_rect = face_positions->head_detections[i].face_detections[j];
00147                         cv::Rect rect(source_rect.x, source_rect.y, source_rect.width, source_rect.height);
00148                         face_bounding_boxes[i][j] = rect;
00149                 }
00150 
00151                 // head bounding box
00152                 const cob_perception_msgs::Rect& source_rect = face_positions->head_detections[i].head_detection;
00153                 cv::Rect rect(source_rect.x, source_rect.y, source_rect.width, source_rect.height);
00154                 head_bounding_boxes[i] = rect;
00155         }
00156 
00157         std::vector < cv::Mat > crops;
00158         for (size_t i = 0; i < heads_depth_images.size(); i++)
00159         {
00160 
00161                 for (size_t j = 0; j < face_bounding_boxes[i].size(); j++)
00162                 {
00163                         int dim = 160;
00164                         //  cv::Mat bgr_crop=heads_color_images[i](face_bounding_boxes[i][j]);
00165                         //  cv::Mat xyz_crop=heads_depth_images[i](face_bounding_boxes[i][j]);
00166                         cv::Mat bgr_crop, xyz_crop;
00167                         heads_color_images[i](face_bounding_boxes[i][j]).copyTo(bgr_crop);
00168                         heads_depth_images[i](face_bounding_boxes[i][j]).copyTo(xyz_crop);
00169                         cv::Vec2f offset;
00170                         offset[0] = head_bounding_boxes[i].x + face_bounding_boxes[i][j].x;
00171                         offset[1] = head_bounding_boxes[i].y + face_bounding_boxes[i][j].y;
00172                         cv::Size norm_size = cv::Size(160, 160);
00173                         //face_normalizer_.captureScene(bgr_crop,xyz_crop,offset);
00174                         face_normalizer_.normalizeFace(bgr_crop, xyz_crop, norm_size, offset);
00175                         crops.push_back(bgr_crop);
00176 
00177                 }
00178         }
00179 
00180         cob_perception_msgs::ColorDepthImageCropArray image_array;
00181         image_array.cdia = *face_positions;
00182         image_array.crops.resize(crops.size());
00183         for (unsigned int i = 0; i < crops.size(); i++)
00184         {
00185                 cv_bridge::CvImage cv_ptr;
00186                 cv_ptr.image = crops[i];
00187                 cv_ptr.encoding = sensor_msgs::image_encodings::BGR8;
00188                 image_array.crops[i] = *(cv_ptr.toImageMsg());
00189         }
00190 
00191         norm_face_publisher_.publish(image_array);
00192 }
00193 
00194 //#######################
00195 //#### main programm ####
00196 int main(int argc, char** argv)
00197 {
00198         // Initialize ROS, specify name of node
00199         ros::init(argc, argv, "face_normalizer");
00200 
00201         // Create a handle for this node, initialize node
00202         ros::NodeHandle nh;
00203 
00204         // Create FaceNormalizerNode class instance
00205         FaceNormalizerNode face_recognizer_node(nh);
00206 
00207         // Create action nodes
00208         //DetectObjectsAction detect_action_node(object_detection_node, nh);
00209         //AcquireObjectImageAction acquire_image_node(object_detection_node, nh);
00210         //TrainObjectAction train_object_node(object_detection_node, nh);
00211 
00212         ros::spin();
00213 
00214         return 0;
00215 }


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