people_detection_display_node.cpp
Go to the documentation of this file.
00001 
00060 #include <cob_people_detection/people_detection_display_node.h>
00061 
00062 using namespace ipa_PeopleDetector;
00063 
00064 inline bool isnan_(double num)
00065 {
00066         return (num != num);
00067 }
00068 ;
00069 
00070 PeopleDetectionDisplayNode::PeopleDetectionDisplayNode(ros::NodeHandle nh) :
00071         node_handle_(nh)
00072 {
00073         it_ = 0;
00074         sync_input_3_ = 0;
00075 
00076         // parameters
00077         std::cout << "\n---------------------------\nPeople Detection Display Parameters:\n---------------------------\n";
00078         node_handle_.param("display", display_, false);
00079         std::cout << "display = " << display_ << "\n";
00080         node_handle_.param("display_timing", display_timing_, false);
00081         std::cout << "display_timing = " << display_timing_ << "\n";
00082 
00083         // subscribers
00084         //      people_segmentation_image_sub_.subscribe(*it_, "people_segmentation_image", 1);
00085         it_ = new image_transport::ImageTransport(node_handle_);
00086         colorimage_sub_.subscribe(*it_, "colorimage_in", 1);
00087         face_recognition_subscriber_.subscribe(node_handle_, "face_position_array", 1);
00088         face_detection_subscriber_.subscribe(node_handle_, "face_detections", 1);
00089         //      pointcloud_sub_.subscribe(node_handle_, "pointcloud_in", 1);
00090 
00091         // input synchronization
00092         //      sync_input_3_ = new message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<cob_perception_msgs::DetectionArray, cob_perception_msgs::ColorDepthImageArray, sensor_msgs::PointCloud2> >(30);
00093         //      sync_input_3_->connectInput(face_recognition_subscriber_, face_detection_subscriber_, pointcloud_sub_);
00094         sync_input_3_ = new message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<cob_perception_msgs::DetectionArray,
00095                         cob_perception_msgs::ColorDepthImageArray, sensor_msgs::Image> >(60);
00096         sync_input_3_->connectInput(face_recognition_subscriber_, face_detection_subscriber_, colorimage_sub_);
00097         sync_input_3_->registerCallback(boost::bind(&PeopleDetectionDisplayNode::inputCallback, this, _1, _2, _3));
00098 
00099         // publishers
00100         people_detection_image_pub_ = it_->advertise("face_position_image", 1);
00101 
00102         std::cout << "PeopleDetectionDisplay initialized." << std::endl;
00103 }
00104 
00105 PeopleDetectionDisplayNode::~PeopleDetectionDisplayNode()
00106 {
00107         if (it_ != 0)
00108                 delete it_;
00109         if (sync_input_3_ != 0)
00110                 delete sync_input_3_;
00111 }
00112 
00114 unsigned long PeopleDetectionDisplayNode::convertColorImageMessageToMat(const sensor_msgs::Image::ConstPtr& image_msg, cv_bridge::CvImageConstPtr& image_ptr, cv::Mat& image)
00115 {
00116         try
00117         {
00118                 image_ptr = cv_bridge::toCvShare(image_msg, sensor_msgs::image_encodings::BGR8);
00119         } catch (cv_bridge::Exception& e)
00120         {
00121                 ROS_ERROR("PeopleDetection: cv_bridge exception: %s", e.what());
00122                 return ipa_Utils::RET_FAILED;
00123         }
00124         image = image_ptr->image;
00125 
00126         return ipa_Utils::RET_OK;
00127 }
00128 
00130 //void PeopleDetectionDisplayNode::inputCallback(const cob_perception_msgs::DetectionArray::ConstPtr& face_recognition_msg, const cob_perception_msgs::ColorDepthImageArray::ConstPtr& face_detection_msg, const sensor_msgs::PointCloud2::ConstPtr& pointcloud_msg)
00131 void PeopleDetectionDisplayNode::inputCallback(const cob_perception_msgs::DetectionArray::ConstPtr& face_recognition_msg,
00132                 const cob_perception_msgs::ColorDepthImageArray::ConstPtr& face_detection_msg, const sensor_msgs::Image::ConstPtr& color_image_msg)
00133 {
00134         // convert color image to cv::Mat
00135         cv_bridge::CvImageConstPtr color_image_ptr;
00136         cv::Mat color_image;
00137         convertColorImageMessageToMat(color_image_msg, color_image_ptr, color_image);
00138 
00139         //      // get color image from point cloud
00140         //      pcl::PointCloud<pcl::PointXYZRGB> point_cloud_src;
00141         //      pcl::fromROSMsg(*pointcloud_msg, point_cloud_src);
00142         //
00143         //      cv::Mat color_image = cv::Mat::zeros(point_cloud_src.height, point_cloud_src.width, CV_8UC3);
00144         //      for (unsigned int v=0; v<point_cloud_src.height; v++)
00145         //      {
00146         //              for (unsigned int u=0; u<point_cloud_src.width; u++)
00147         //              {
00148         //                      pcl::PointXYZRGB point = point_cloud_src(u,v);
00149         //                      if (isnan_(point.z) == false)
00150         //                              color_image.at<cv::Point3_<unsigned char> >(v,u) = cv::Point3_<unsigned char>(point.b, point.g, point.r);
00151         //              }
00152         //      }
00153 
00154         // insert all detected heads and faces
00155         for (int i = 0; i < (int)face_detection_msg->head_detections.size(); i++)
00156         {
00157                 // paint head
00158                 const cob_perception_msgs::Rect& head_rect = face_detection_msg->head_detections[i].head_detection;
00159                 cv::Rect head(head_rect.x, head_rect.y, head_rect.width, head_rect.height);
00160                 cv::rectangle(color_image, cv::Point(head.x, head.y), cv::Point(head.x + head.width, head.y + head.height), CV_RGB(148, 219, 255), 2, 8, 0);
00161 
00162                 // paint faces
00163                 for (int j = 0; j < (int)face_detection_msg->head_detections[i].face_detections.size(); j++)
00164                 {
00165                         const cob_perception_msgs::Rect& face_rect = face_detection_msg->head_detections[i].face_detections[j];
00166                         cv::Rect face(face_rect.x + head.x, face_rect.y + head.y, face_rect.width, face_rect.height);
00167                         cv::rectangle(color_image, cv::Point(face.x, face.y), cv::Point(face.x + face.width, face.y + face.height), CV_RGB(191, 255, 148), 2, 8, 0);
00168                 }
00169         }
00170 
00171         // insert recognized faces
00172         for (int i = 0; i < (int)face_recognition_msg->detections.size(); i++)
00173         {
00174                 const cob_perception_msgs::Rect& face_rect = face_recognition_msg->detections[i].mask.roi;
00175                 cv::Rect face(face_rect.x, face_rect.y, face_rect.width, face_rect.height);
00176 
00177                 if (face_recognition_msg->detections[i].detector == "head")
00178                         cv::rectangle(color_image, cv::Point(face.x, face.y), cv::Point(face.x + face.width, face.y + face.height), CV_RGB(0, 0, 255), 2, 8, 0);
00179                 else
00180                         cv::rectangle(color_image, cv::Point(face.x, face.y), cv::Point(face.x + face.width, face.y + face.height), CV_RGB(0, 255, 0), 2, 8, 0);
00181 
00182                 if (face_recognition_msg->detections[i].label == "Unknown")
00183                         // Distance to face class is too high
00184                         cv::putText(color_image, "Unknown", cv::Point(face.x, face.y + face.height + 25), cv::FONT_HERSHEY_PLAIN, 2, CV_RGB(255, 0, 0), 2);
00185                 else if (face_recognition_msg->detections[i].label == "No face")
00186                         // Distance to face space is too high
00187                         cv::putText(color_image, "No face", cv::Point(face.x, face.y + face.height + 25), cv::FONT_HERSHEY_PLAIN, 2, CV_RGB(255, 0, 0), 2);
00188                 else
00189                         // Face classified
00190                         cv::putText(color_image, face_recognition_msg->detections[i].label.c_str(), cv::Point(face.x, face.y + face.height + 25), cv::FONT_HERSHEY_PLAIN, 2, CV_RGB(0, 255, 0),
00191                                         2);
00192         }
00193 
00194         // display image
00195         if (display_ == true)
00196         {
00197                 cv::imshow("Detections and Recognitions", color_image);
00198                 cv::waitKey(10);
00199         }
00200 
00201         // publish image
00202         cv_bridge::CvImage cv_ptr;
00203         cv_ptr.image = color_image;
00204         cv_ptr.encoding = "bgr8";
00205         people_detection_image_pub_.publish(cv_ptr.toImageMsg());
00206 
00207         if (display_timing_ == true)
00208                 ROS_INFO("%d Display: Time stamp of image message: %f. Delay: %f.", color_image_msg->header.seq, color_image_msg->header.stamp.toSec(),
00209                                 ros::Time::now().toSec() - color_image_msg->header.stamp.toSec());
00210 }
00211 
00212 //#######################
00213 //#### main programm ####
00214 int main(int argc, char** argv)
00215 {
00216         // Initialize ROS, specify name of node
00217         ros::init(argc, argv, "people_detection_display");
00218 
00219         // Create a handle for this node, initialize node
00220         ros::NodeHandle nh;
00221 
00222         // Create FaceRecognizerNode class instance
00223         PeopleDetectionDisplayNode people_detection_display_node(nh);
00224 
00225         // Create action nodes
00226         //DetectObjectsAction detect_action_node(object_detection_node, nh);
00227         //AcquireObjectImageAction acquire_image_node(object_detection_node, nh);
00228         //TrainObjectAction train_object_node(object_detection_node, nh);
00229 
00230         ros::spin();
00231 
00232         return 0;
00233 }


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Fri Aug 28 2015 10:24:13