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
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
00084
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
00090
00091
00092
00093
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
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
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
00135 cv_bridge::CvImageConstPtr color_image_ptr;
00136 cv::Mat color_image;
00137 convertColorImageMessageToMat(color_image_msg, color_image_ptr, color_image);
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 for (int i = 0; i < (int)face_detection_msg->head_detections.size(); i++)
00156 {
00157
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
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
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
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
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
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
00195 if (display_ == true)
00196 {
00197 cv::imshow("Detections and Recognitions", color_image);
00198 cv::waitKey(10);
00199 }
00200
00201
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
00214 int main(int argc, char** argv)
00215 {
00216
00217 ros::init(argc, argv, "people_detection_display");
00218
00219
00220 ros::NodeHandle nh;
00221
00222
00223 PeopleDetectionDisplayNode people_detection_display_node(nh);
00224
00225
00226
00227
00228
00229
00230 ros::spin();
00231
00232 return 0;
00233 }