00001 #include "camera_object_learner_server_alg_node.h"
00002
00003 CameraObjectLearnerServerAlgNode::CameraObjectLearnerServerAlgNode(void) :
00004 algorithm_base::IriBaseAlgorithm<CameraObjectLearnerServerAlgorithm>()
00005 {
00006
00007
00008 this->received_camera_info = false;
00009 this->received_person = false;
00010 this->init = false;
00011
00012
00013
00014 this->camera_info_subscriber_ = this->public_node_handle_.subscribe("camera_info", 100, &CameraObjectLearnerServerAlgNode::camera_info_callback, this);
00015 this->people_subscriber_ = this->public_node_handle_.subscribe("people", 100, &CameraObjectLearnerServerAlgNode::people_callback, this);
00016
00017
00018 this->init_server_ = this->public_node_handle_.advertiseService("init", &CameraObjectLearnerServerAlgNode::initCallback, this);
00019
00020
00021
00022
00023
00024
00025 }
00026
00027 CameraObjectLearnerServerAlgNode::~CameraObjectLearnerServerAlgNode(void)
00028 {
00029
00030 }
00031
00032 void CameraObjectLearnerServerAlgNode::mainNodeThread(void)
00033 {
00034
00035
00036
00037
00038
00039
00040
00041 }
00042
00043
00044
00045 void CameraObjectLearnerServerAlgNode::camera_info_callback(const sensor_msgs::CameraInfo::ConstPtr& msg)
00046 {
00047 static bool first=true;
00048 ROS_DEBUG("CameraObjectLearnerServerAlgNode::camera_info_callback: New Message Received");
00049
00050
00051
00052
00053
00054
00055 if(first)
00056 {
00057 ROS_INFO("CameraObjectLearnerServerAlgNode::camera_info_callback: camera_info loaded!");
00058 this->camera_model.fromCameraInfo(msg);
00059 this->received_camera_info = true;
00060 first=false;
00061 }
00062
00063
00064
00065
00066 }
00067
00068 void CameraObjectLearnerServerAlgNode::people_callback(const iri_perception_msgs::detectionArray::ConstPtr& msg)
00069 {
00070 ROS_DEBUG("CameraObjectLearnerServerAlgNode::people_callback: New Message Received");
00071
00072
00073 this->alg_.lock();
00074
00075
00076
00077 double mindist = 100;
00078 double dist = 100;
00079 for(unsigned int i=0; i<msg->detection.size(); i++)
00080 {
00081 double x = msg->detection[i].pose.pose.position.x;
00082 double y = msg->detection[i].pose.pose.position.y;
00083 double z = msg->detection[i].pose.pose.position.z;
00084
00085
00086 dist = x*x+y*y;
00087 if(dist < mindist*mindist && x>0.5 && x<1.5 && y>-0.25 && y<0.25)
00088 {
00089 mindist = dist;
00090 this->init_point.header = msg->header;
00091 this->init_point.point.x = x;
00092 this->init_point.point.y = y;
00093 this->init_point.point.z = z;
00094 }
00095 }
00096 if(mindist==100)
00097 this->received_person = false;
00098 else
00099 this->received_person = true;
00100
00101 this->alg_.unlock();
00102
00103 }
00104
00105
00106 bool CameraObjectLearnerServerAlgNode::initCallback(iri_camera_object_learner_server::init::Request &req, iri_camera_object_learner_server::init::Response &res)
00107 {
00108 ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: New Request Received!");
00109
00110
00111 this->alg_.lock();
00112 res.x = 0;
00113 res.y = 0;
00114 res.width = 0;
00115 res.height = 0;
00116
00117 if(this->init)
00118 {
00119 if(this->received_camera_info && this->received_person)
00120 {
00121 if(this->compute_init_rect())
00122 {
00123 res.x = this->init_rect.x;
00124 res.y = this->init_rect.y;
00125 res.width = this->init_rect.width;
00126 res.height = this->init_rect.height;
00127 ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: res x,y,w,h: %d, %d, %d, %d", res.x, res.y, res.width, res.height);
00128 }
00129 else
00130 {
00131 ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: Rectangle not loaded!");
00132 }
00133 }
00134 else
00135 {
00136 ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: camera_info or person_detection not received yet!");
00137 }
00138 }
00139 else
00140 {
00141 ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: init param false!");
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 this->alg_.unlock();
00156
00157
00158 return true;
00159 }
00160
00161
00162
00163
00164
00165 void CameraObjectLearnerServerAlgNode::node_config_update(Config &config, uint32_t level)
00166 {
00167 this->alg_.lock();
00168 this->init = config.init;
00169 this->window_width = config.window_width;
00170 this->window_height = config.window_height;
00171 this->window_offset = config.window_offset;
00172 this->alg_.unlock();
00173 }
00174
00175 void CameraObjectLearnerServerAlgNode::addNodeDiagnostics(void)
00176 {
00177 }
00178
00179
00180 int main(int argc,char *argv[])
00181 {
00182 return algorithm_base::main<CameraObjectLearnerServerAlgNode>(argc, argv, "camera_object_learner_server_alg_node");
00183 }
00184
00185 bool CameraObjectLearnerServerAlgNode::compute_init_rect()
00186 {
00187 ROS_INFO("CameraObjectLearnerServerAlgNode::compute_init_rect");
00188 bool ok = false;
00189 try
00190 {
00191 tf::StampedTransform laser_to_camera_transf;
00192 ros::Time now = ros::Time::now();
00193 ros::Duration timeout(5.0f);
00194 std::string target_frame = this->camera_model.tfFrame();
00195 std::string source_frame = init_point.header.frame_id;
00196 bool tf_exists = tf_listener.waitForTransform(target_frame, source_frame, now, timeout, ros::Duration(0.01));
00197 if(tf_exists)
00198 {
00199
00200 tf_listener.lookupTransform(target_frame, source_frame, now, laser_to_camera_transf);
00201
00202 tf::Point laser_point(this->init_point.point.x,this->init_point.point.y,this->init_point.point.z);
00203 tf::Point cam_point(laser_to_camera_transf * laser_point);
00204
00205
00206 double width_m = this->window_width;
00207 double height_m = this->window_height;
00208 double height_offset_m = this->window_offset;
00209 ROS_INFO("cam_point: %f, %f, %f", cam_point.x(), cam_point.y(), cam_point.z());
00210
00211 cv::Point3d cv_cam_point0(cam_point.x(), cam_point.y()-height_offset_m-height_m/2.0, cam_point.z());
00212 cv::Point3d cv_cam_point1(cam_point.x()-width_m/2.0, cam_point.y()-height_offset_m-height_m, cam_point.z());
00213 cv::Point3d cv_cam_point2(cam_point.x()+width_m/2.0, cam_point.y()-height_offset_m, cam_point.z());
00214
00215 cv::Point2d image_point0 = this->camera_model.project3dToPixel(cv_cam_point0);
00216 cv::Point2d image_point1 = this->camera_model.project3dToPixel(cv_cam_point1);
00217 cv::Point2d image_point2 = this->camera_model.project3dToPixel(cv_cam_point2);
00218
00219
00220
00221
00222 int width = abs(image_point1.x - image_point2.x);
00223 int height = abs(image_point1.y - image_point2.y);
00224 this->init_rect.width = width;
00225 this->init_rect.height = height;
00226 this->init_rect.x = image_point0.x;
00227 this->init_rect.y = image_point0.y;
00228 ok = true;
00229 }
00230 else
00231 {
00232 ROS_INFO("CameraObjectLearnerServerAlgNode::compute_init_rect::No transform: %s-->%s", target_frame.c_str(), source_frame.c_str());
00233 }
00234 }
00235 catch (tf::TransformException& ex)
00236 {
00237 ROS_WARN("TF exception:\n%s", ex.what());
00238 }
00239 return ok;
00240 }