$search
00001 #include "cv.h" 00002 #include <cv_bridge/cv_bridge.h> 00003 #include <opencv2/imgproc/imgproc.hpp> 00004 #include "highgui.h" 00005 00006 #include <ros/ros.h> 00007 #include <image_transport/image_transport.h> 00008 #include <sensor_msgs/image_encodings.h> 00009 00010 #include <pr2_mechanism_controllers/BaseOdometryState.h> 00011 00012 #include <boost/thread/condition.hpp> 00013 00014 #include <pcl/ros/conversions.h> 00015 #include <pcl/point_cloud.h> 00016 #include <pcl/point_types.h> 00017 #include <pcl/io/pcd_io.h> 00018 #include <pcl/visualization/pcl_visualizer.h> 00019 #include <pcl/visualization/point_cloud_handlers.h> 00020 #include <sensor_msgs/PointCloud2.h> 00021 #include <pcl/visualization/cloud_viewer.h> 00022 00023 #include <cob_read_text/text_detect.h> 00024 00025 namespace enc = sensor_msgs::image_encodings; 00026 00027 static const char WINDOW[] = "Image window"; 00028 00029 class TextReader 00030 { 00031 public: 00032 ros::NodeHandle nh_; 00033 image_transport::ImageTransport it_; 00034 image_transport::Subscriber image_sub_; 00035 image_transport::Publisher image_pub_; 00036 ros::Subscriber robot_state_sub_; 00037 ros::Subscriber depth_sub_; 00038 DetectText detector; 00039 ros::Time last_movement_; 00040 00041 pthread_mutex_t pr2_velocity_lock_; 00042 pthread_mutex_t pr2_image_lock_; 00043 00044 cv_bridge::CvImagePtr cv_ptr; 00045 cv_bridge::CvImagePtr detection_ptr; 00046 00047 float x_; 00048 float y_; 00049 bool okToDetect_; 00050 bool initialized_; 00051 00052 TextReader(const char* correlation, const char* dictionary) : 00053 it_(nh_)//, cloud(new pcl::PointCloud<pcl::PointXYZRGB>), viewer(new pcl::visualization::PCLVisualizer("3D Viewer")) 00054 { 00055 /* 00056 * advertise on topic "text_detect" the result 00057 * subscribe on topic "image_color" for the camera picture with Callback-function "imageCb" 00058 * subscribe on topic "/base_odometry/state" with Callback-function "robotStateCb" for detecting camera movement 00059 */ 00060 image_pub_ = it_.advertise("text_detect", 1); 00061 image_sub_ = it_.subscribe("image_color", 1, &TextReader::imageCb, this); 00062 robot_state_sub_ = nh_.subscribe("/base_odometry/state", 1, &TextReader::robotStateCb, this); 00063 00064 // depth_sub_ = nh_.subscribe("/camera/depth_registered/points", 1, &TextReader::depthCb, this); 00065 00066 detector = DetectText(); 00067 detector.readLetterCorrelation(correlation); 00068 detector.readWordList(dictionary); 00069 00070 pthread_mutex_init(&pr2_velocity_lock_, NULL); 00071 pthread_mutex_init(&pr2_image_lock_, NULL); 00072 okToDetect_ = false; 00073 initialized_ = false; 00074 00075 x_ = 0; 00076 y_ = 0; 00077 } 00078 00079 ~TextReader() 00080 { 00081 } 00082 00083 /* call back function 00084 * in case msg from topic "state" tells that odometry has changed, update last_movement 00085 */ 00086 void robotStateCb(const pr2_mechanism_controllers::BaseOdometryStateConstPtr& msg) 00087 { 00088 pthread_mutex_trylock(&pr2_velocity_lock_); 00089 x_ = msg->velocity.linear.x; 00090 y_ = msg->velocity.linear.y; 00091 if (x_ != 0 || y_ != 0) 00092 last_movement_ = ros::Time::now(); 00093 pthread_mutex_unlock(&pr2_velocity_lock_); 00094 } 00095 00096 /* call back function 00097 * cv_ptr gets new picture from topic "image_color" 00098 */ 00099 void imageCb(const sensor_msgs::ImageConstPtr& msg) 00100 { 00101 try 00102 { 00103 pthread_mutex_trylock(&pr2_image_lock_); 00104 cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8); 00105 if (initialized_ == false) 00106 { 00107 detection_ptr = cv_bridge::toCvCopy(msg, enc::BGR8); 00108 initialized_ = true; 00109 } 00110 okToDetect_ = true; 00111 pthread_mutex_unlock(&pr2_image_lock_); 00112 } 00113 catch (cv_bridge::Exception& e) 00114 { 00115 ROS_ERROR("cv_bridge exception: %s", e.what()); 00116 okToDetect_ = false; 00117 return; 00118 } 00119 } 00120 00121 /* call back function for depth picture 00122 void depthCb(sensor_msgs::PointCloud2::ConstPtr recent_image) 00123 { 00124 pcl::fromROSMsg(*recent_image, *cloud); 00125 pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> color_hdl(cloud); 00126 if (!(viewer->updatePointCloud<pcl::PointXYZRGB> (cloud, color_hdl))) 00127 { 00128 viewer->addPointCloud(cloud, color_hdl, "cloud", 0); 00129 viewer->addCoordinateSystem(1.0, 0); 00130 } 00131 viewer->updatePointCloud<pcl::PointXYZRGB> (cloud, color_hdl); 00132 viewer->spinOnce(100); 00133 boost::this_thread::sleep(boost::posix_time::microseconds(100000)); 00134 } 00135 00136 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud; 00137 boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer; 00138 */ 00139 }; 00140 00141 00142 int main(int argc, char** argv) 00143 { 00144 if (argc < 3) 00145 { 00146 ROS_ERROR( "not enought input: read_text <correlation> <dictionary>"); 00147 return 1; 00148 } 00149 ros::init(argc, argv, "cob_read_text"); 00150 TextReader reader(argv[1], argv[2]); 00151 00152 DetectText &detector = reader.detector; 00153 ros::Rate r(1); 00154 ros::Time last_detection = ros::Time::now(); 00155 //int32_t sys_ret; 00156 while (ros::ok()) 00157 { 00158 /* 00159 * Detection starts when 00160 * - no exception was thrown while getting the new image in imageCb call back function, 00161 * - the last detection was done more than 2 seconds ago 00162 * - the camera is steady (last_movement was more than 2 seconds ago) 00163 */ 00164 if (reader.okToDetect_) 00165 { 00166 ROS_INFO("start detection........"); 00167 ros::Time now = ros::Time::now(); 00168 if (now - last_detection < ros::Duration(2)) // if 00169 { 00170 ROS_INFO("waiting..."); 00171 ros::spinOnce(); 00172 r.sleep(); 00173 continue; 00174 } 00175 pthread_mutex_lock(&(reader.pr2_velocity_lock_)); 00176 bool is_steady = (now - reader.last_movement_ > ros::Duration(2)); 00177 bool is_moving = (reader.x_ != 0 || reader.y_ != 0); 00178 pthread_mutex_unlock(&(reader.pr2_velocity_lock_)); 00179 if (!is_steady || is_moving) 00180 { 00181 ros::spinOnce(); 00182 r.sleep(); 00183 continue; 00184 } 00185 00186 // do the detection 00187 pthread_mutex_lock(&(reader.pr2_image_lock_)); 00188 detector.detect(reader.cv_ptr->image); 00189 pthread_mutex_unlock(&(reader.pr2_image_lock_)); 00190 00191 // publish the detection 00192 reader.detection_ptr->image = detector.getDetection(); 00193 reader.image_pub_.publish(reader.detection_ptr->toImageMsg()); 00194 00195 // text to speech: 00196 /* 00197 int count = 0; 00198 for (size_t i = 0; i < detector.getWords().size(); i++) 00199 { 00200 ros::spinOnce(); 00201 count++; 00202 pthread_mutex_lock(&(reader.pr2_velocity_lock_)); 00203 is_moving = (reader.x_ != 0 || reader.y_ != 0); 00204 pthread_mutex_unlock(&(reader.pr2_velocity_lock_)); 00205 if (is_moving) 00206 { 00207 break; 00208 } 00209 text to speech 00210 string word = detector.getWords()[i]; 00211 if (word.compare("") != 0) 00212 { 00213 string command = "echo \"" + word + "\" | festival --tts"; 00214 sys_ret = system(command.c_str()); 00215 00216 } 00217 00218 } 00219 */ 00220 00221 //depth: 00222 /* if (!(reader.viewer->wasStopped())) 00223 { 00224 ros::spinOnce(); 00225 boost::this_thread::sleep(boost::posix_time::microseconds(100000)); 00226 if (reader.cloud->points.size() > 0) 00227 { 00228 // for (int idx = 5000; idx < (reader.cloud)->width+5000; ++idx) 00229 // { 00230 // cout << "[" << reader.cloud->points[idx].x << "|" << reader.cloud->points[idx].y << "]"; 00231 // } 00232 // if(!pcl_isnan(reader.cloud->points[idx].x)) 00233 // { 00234 // cout << "[" << reader.cloud->points[idx].x << "|" << reader.cloud->points[idx].y << "|" << reader.cloud->points[idx].z << "]" << endl; 00235 // } 00236 // exit(0); 00237 } 00238 00239 // break; 00240 }*/ 00241 00242 } 00243 00244 //Multiple View Geometry : x=553, y=290,height=97,width=230 00245 00246 00247 std::cout << "----------------------------------" << endl; 00248 std::cout << "Texts found: " << detector.getWords().size() << endl; 00249 for (int i = 0; i < detector.getWords().size(); i++) 00250 { 00251 std::cout << detector.getWords()[i] << ": x=" << ((detector.getBoxesBothSides())[0]).x << ", y=" 00252 << ((detector.getBoxesBothSides())[0]).y << ",height=" << ((detector.getBoxesBothSides())[0]).height 00253 << ",width=" << ((detector.getBoxesBothSides())[0]).width << endl; 00254 //if(!pcl_isnan(reader.cloud->points[idx].)z) cout << ",depth:" 00255 } 00256 00257 std::cout << "----------------------------------" << endl; 00258 00259 last_detection = ros::Time::now(); 00260 ros::spinOnce(); 00261 r.sleep(); 00262 } 00263 00264 return 0; 00265 }