Skip to content

Commit c9acfe2

Browse files
checking rate lim
1 parent 19f761f commit c9acfe2

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

launch/extended_object_detection_oakd_example.launch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<param name="objectBasePath" value="$(arg objectBasePath)"/>
1212

1313
<param name="subscribe_depth" value="true"/>
14+
<param name="rate_limit_sec" value="0.1"/>
1415

1516

1617
<remap from="camera/image_raw" to="/rgb_stereo_publisher/color/image"/>

src/extended_object_detection_node/eod_node.cpp

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
#include "eod_node.h"
2-
2+
#include <cstdint>
3+
#include <sensor_msgs/image_encodings.h>
34

45
EOD_ROS::EOD_ROS(ros::NodeHandle nh, ros::NodeHandle nh_p){
56
nh_ = nh;
67
nh_p_ = nh_p;
78

9+
frame_sequence = 0;
10+
811
rgb_it_ = new image_transport::ImageTransport(nh_);
912

1013
sub_rgb_.subscribe(*rgb_it_, "camera/image_raw", 10);
1114
sub_info_.subscribe(nh_, "camera/info", 10);
1215

16+
// get params
1317
nh_p_.param("subscribe_depth", subscribe_depth, false);
18+
nh_p_.param("rate_limit_sec", rate_limit_sec, 0.1);
19+
20+
// set up message filters
1421
if( !subscribe_depth){
1522
ROS_INFO("Configuring filter on rgb image and info...");
1623

1724
rgb_sync_.reset( new RGBSynchronizer(RGBInfoSyncPolicy(10), sub_rgb_, sub_info_) );
18-
rgb_sync_->registerCallback(boost::bind(&EOD_ROS::rgb_info_cb, this, boost::placeholders::_1, boost::placeholders::_2));
19-
25+
rgb_sync_->registerCallback(boost::bind(&EOD_ROS::rgb_info_cb, this, boost::placeholders::_1, boost::placeholders::_2));
2026
}
2127
else{
2228
ROS_INFO("Configuring filter on rgbd images and infos...");
@@ -25,18 +31,76 @@ EOD_ROS::EOD_ROS(ros::NodeHandle nh, ros::NodeHandle nh_p){
2531
sub_depth_info_.subscribe(nh_, "depth/info", 10);
2632

2733
rgbd_sync_.reset( new RGBDSynchronizer(RGBDInfoSyncPolicy(10), sub_rgb_, sub_info_, sub_depth_, sub_depth_info_) );
28-
rgbd_sync_->registerCallback(boost::bind(&EOD_ROS::rgbd_info_cb, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
29-
34+
rgbd_sync_->registerCallback(boost::bind(&EOD_ROS::rgbd_info_cb, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
3035
}
3136
ROS_INFO("Configured!");
3237
}
3338

34-
void EOD_ROS::rgb_info_cb(const sensor_msgs::ImageConstPtr& image, const sensor_msgs::CameraInfoConstPtr& info){
39+
bool EOD_ROS::check_time(ros::Time stamp){
40+
if( frame_sequence == 0)
41+
return true;
42+
return (stamp - prev_detected_time).toSec() > rate_limit_sec;
43+
}
44+
45+
void EOD_ROS::rgb_info_cb(const sensor_msgs::ImageConstPtr& rgb_image, const sensor_msgs::CameraInfoConstPtr& rgb_info){
3546
ROS_INFO("Get Image!");
47+
48+
// CHECK RATE
49+
//if( !check_time(rgb_image->header.stamp) ) {
50+
if( !check_time(ros::Time::now()) ) {
51+
ROS_WARN("Skipped frame");
52+
return;
53+
}
54+
55+
cv::Mat rgb;
56+
try{
57+
rgb = cv_bridge::toCvCopy(rgb_image, "bgr8")->image;
58+
}
59+
catch (cv_bridge::Exception& e){
60+
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", rgb_image->encoding.c_str());
61+
}
62+
63+
detect(rgb, cv::Mat());
3664
}
3765

3866
void EOD_ROS::rgbd_info_cb(const sensor_msgs::ImageConstPtr& rgb_image, const sensor_msgs::CameraInfoConstPtr& rgb_info, const sensor_msgs::ImageConstPtr& depth_image, const sensor_msgs::CameraInfoConstPtr& depth_info){
3967
ROS_INFO("Got RGBD!");
68+
69+
// CHECK RATE
70+
//if( !check_time(rgb_image->header.stamp) ) {
71+
if( !check_time(ros::Time::now()) ) {
72+
ROS_WARN("Skipped frame");
73+
return;
74+
}
75+
76+
cv::Mat rgb;
77+
try{
78+
rgb = cv_bridge::toCvCopy(rgb_image, "bgr8")->image;
79+
}
80+
catch (cv_bridge::Exception& e){
81+
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", rgb_image->encoding.c_str());
82+
}
83+
84+
cv::Mat depth;
85+
if (depth_image->encoding == sensor_msgs::image_encodings::TYPE_16UC1){
86+
depth = cv_bridge::toCvCopy(depth_image, sensor_msgs::image_encodings::TYPE_16UC1)->image;
87+
}
88+
else if(depth_image->encoding == sensor_msgs::image_encodings::TYPE_32FC1){
89+
depth = cv_bridge::toCvCopy(depth_image, sensor_msgs::image_encodings::TYPE_32FC1)->image;
90+
}
91+
else{
92+
ROS_ERROR_THROTTLE(5, "Depth image has unsupported encoding [%s]", depth_image->encoding.c_str());
93+
}
94+
95+
detect(rgb, depth);
96+
}
97+
98+
void EOD_ROS::detect(const cv::Mat& rgb, const cv::Mat& depth){
99+
prev_detected_time = ros::Time::now();
100+
101+
102+
frame_sequence++;
103+
40104
}
41105

42106
int main(int argc, char **argv)

src/extended_object_detection_node/eod_node.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <sensor_msgs/Image.h>
1414
#include <sensor_msgs/CameraInfo.h>
15-
#include <sensor_msgs/image_encodings.h>
15+
//#include <sensor_msgs/image_encodings.h>
1616

1717
//#include "ObjectBase.h"
1818

@@ -45,13 +45,23 @@ class EOD_ROS{
4545
boost::shared_ptr<RGBDSynchronizer> rgbd_sync_;
4646

4747
// params
48-
bool subscribe_depth;
48+
bool subscribe_depth;
49+
double rate_limit_sec;
4950

50-
// callbacks
51+
// vars
52+
int frame_sequence;
53+
ros::Time prev_detected_time;
54+
55+
// callbacks
5156
void rgb_info_cb(const sensor_msgs::ImageConstPtr& image, const sensor_msgs::CameraInfoConstPtr& info);
5257

5358
void rgbd_info_cb(const sensor_msgs::ImageConstPtr& image, const sensor_msgs::CameraInfoConstPtr& info, const sensor_msgs::ImageConstPtr& depth_image, const sensor_msgs::CameraInfoConstPtr& depth_info);
5459

60+
// functions
61+
void detect(const cv::Mat& rgb, const cv::Mat& depth);
62+
bool check_time(ros::Time stamp);
63+
64+
5565

5666

5767
};

0 commit comments

Comments
 (0)