Skip to content

Commit

Permalink
Multithreading mismatched image header fix
Browse files Browse the repository at this point in the history
  • Loading branch information
umer936 committed Aug 28, 2018
1 parent f610c00 commit 13abde3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
9 changes: 8 additions & 1 deletion darknet_ros/include/darknet_ros/YoloObjectDetector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ typedef struct
int num, Class;
} RosBox_;

typedef struct
{
IplImage* image;
std_msgs::Header header;
} IplImageWithHeader_;

class YoloObjectDetector
{
public:
Expand Down Expand Up @@ -166,6 +172,7 @@ class YoloObjectDetector
int demoClasses_;

network *net_;
std_msgs::Header headerBuff_[3];
image buff_[3];
image buffLetter_[3];
int buffId_[3];
Expand Down Expand Up @@ -232,7 +239,7 @@ class YoloObjectDetector

void yolo();

IplImage* getIplImage();
IplImageWithHeader_ getIplImageWithHeader();

bool getImageStatus(void);

Expand Down
20 changes: 14 additions & 6 deletions darknet_ros/src/YoloObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ void YoloObjectDetector::cameraCallback(const sensor_msgs::ImageConstPtr& msg)

try {
cam_image = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
imageHeader_ = msg->header;
} catch (cv_bridge::Exception& e) {
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
Expand All @@ -196,6 +195,8 @@ void YoloObjectDetector::cameraCallback(const sensor_msgs::ImageConstPtr& msg)
if (cam_image) {
{
boost::unique_lock<boost::shared_mutex> lockImageCallback(mutexImageCallback_);
int index = buffIndex_;
imageHeader_ = msg->header;
camImageCopy_ = cam_image->image.clone();
}
{
Expand Down Expand Up @@ -406,8 +407,10 @@ void *YoloObjectDetector::detectInThread()

void *YoloObjectDetector::fetchInThread()
{
IplImage* ROS_img = getIplImage();
IplImageWithHeader_ imageAndHeader = getIplImageWithHeader();
IplImage* ROS_img = imageAndHeader.image;
ipl_into_image(ROS_img, buff_[buffIndex_]);
headerBuff_[buffIndex_] = imageAndHeader.header;
{
boost::shared_lock<boost::shared_mutex> lock(mutexImageCallback_);
buffId_[buffIndex_] = actionId_;
Expand Down Expand Up @@ -500,10 +503,14 @@ void YoloObjectDetector::yolo()
layer l = net_->layers[net_->n - 1];
roiBoxes_ = (darknet_ros::RosBox_ *) calloc(l.w * l.h * l.n, sizeof(darknet_ros::RosBox_));

IplImage* ROS_img = getIplImage();
IplImageWithHeader_ imageAndHeader = getIplImageWithHeader();
IplImage* ROS_img = imageAndHeader.image;
buff_[0] = ipl_to_image(ROS_img);
buff_[1] = copy_image(buff_[0]);
buff_[2] = copy_image(buff_[0]);
headerBuff_[0] = imageAndHeader.header;
headerBuff_[1] = headerBuff_[0];
headerBuff_[2] = headerBuff_[0];
buffLetter_[0] = letterbox_image(buff_[0], net_->w, net_->h);
buffLetter_[1] = letterbox_image(buff_[0], net_->w, net_->h);
buffLetter_[2] = letterbox_image(buff_[0], net_->w, net_->h);
Expand Down Expand Up @@ -549,11 +556,12 @@ void YoloObjectDetector::yolo()

}

IplImage* YoloObjectDetector::getIplImage()
IplImageWithHeader_ YoloObjectDetector::getIplImageWithHeader()
{
boost::shared_lock<boost::shared_mutex> lock(mutexImageCallback_);
IplImage* ROS_img = new IplImage(camImageCopy_);
return ROS_img;
IplImageWithHeader_ header = {.image = ROS_img, .header = imageHeader_};
return header;
}

bool YoloObjectDetector::getImageStatus(void)
Expand Down Expand Up @@ -614,7 +622,7 @@ void *YoloObjectDetector::publishInThread()
}
boundingBoxesResults_.header.stamp = ros::Time::now();
boundingBoxesResults_.header.frame_id = "detection";
boundingBoxesResults_.image_header = imageHeader_;
boundingBoxesResults_.image_header = headerBuff_[(buffIndex_ + 1) % 3];
boundingBoxesPublisher_.publish(boundingBoxesResults_);
} else {
std_msgs::Int8 msg;
Expand Down

0 comments on commit 13abde3

Please sign in to comment.