leggedrobotics / darknet_ros

YOLO ROS: Real-Time Object Detection for ROS
BSD 3-Clause "New" or "Revised" License
2.14k stars 1.16k forks source link

How do you get boundary box information from the subscriber code and draw the box in the imported video based on that information? #347

Closed Lee-Siyoung closed 2 years ago

Lee-Siyoung commented 2 years ago

I will send boundingbox information and video from Jetson to another computer and draw yolo box. But there was an error in the catkin_make process.

In the main function this is my code:

void imageCallback(const sensor_msgs::ImageConstPtr& msg, const darknet_ros_msgs::BoundingBoxes::ConstPtr& bbox)
{
  try
  {
    cv::Mat real = cv_bridge::toCvShare(msg, "bgr8")->image;
    if(bbox->bounding_boxes[0].probability > 0.0)
    {
        cv::rectangle(real, (bbox->bounding_boxes[0].xmin, bbox->bounding_boxes[0].ymin), (bbox->bounding_boxes[0].xmax, bbox->bounding_boxes[0].ymax),(0,0,255), 2);
    }

    cv::imshow("view", real);
  }
  catch (cv_bridge::Exception& e)
  {
    ROS_ERROR("cannot decode image");
  }

  cv::waitKey(1);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "opencv_sub");

  cv::namedWindow("view");
  cv::startWindowThread();

  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe("/camera/color/image_raw", 1000, imageCallback);
  ros::Subscriber cood_sub = nh.subscribe("/darknet_ros/bounding_boxes",100, msgCallback);
  ros::spin();
  cv::destroyWindow("view");
}

void msgCallback(const darknet_ros_msgs::BoundingBoxes::ConstPtr& msg)
{
    cout<<"Bouding Boxes (header):" << msg->header <<endl;
    cout<<"Bouding Boxes (image_header):" << msg->image_header <<endl;
    cout<<"Bouding Boxes (Class):" << msg->bounding_boxes[0].Class <<endl;
    cout<<"Bouding Boxes (xmin):" << msg->bounding_boxes[0].xmin <<endl;
    cout<<"Bouding Boxes (xmax):" << msg->bounding_boxes[0].xmax <<endl;
    cout<<"Bouding Boxes (ymin):" << msg->bounding_boxes[0].ymin <<endl;
    cout<<"Bouding Boxes (ymax):" << msg->bounding_boxes[0].ymax <<endl;
    cout<<"Bouding Boxes (probability):" << msg->bounding_boxes[0].probability <<endl;
    cout<<"\033[2J\033[1;1H";     // clear terminal

}

I'm a beginner at ros, opencv. Can you help me?

It's error

1


the callback should have only one parameter in its signature. So I changed it to the code below, but I'm not getting any information from the boundingbox.

void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
  try
  {
    cv::Mat real = cv_bridge::toCvShare(msg, "bgr8")->image;
    if(darknet_ros_msgs::BoundingBoxes::bounding_boxes[0].probability > 0.0)
    {
        cv::rectangle(real, (darknet_ros_msgs::BoundingBoxes::bounding_boxes[0].xmin, darknet_ros_msgs::BoundingBoxes::bounding_boxes[0].ymin), (darknet_ros_msgs::BoundingBoxes::bounding_boxes[0].xmax, darknet_ros_msgs::BoundingBoxes::bounding_boxes[0].ymax),(0,0,255), 2);
    }
    cv::imshow("view", real);
  }
  catch (cv_bridge::Exception& e)
  {
    ROS_ERROR("cannot decode image");
  }
  cv::waitKey(1);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "opencv_sub");

  cv::namedWindow("view");
  cv::startWindowThread();
  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe("/camera/color/image_raw", 1000, imageCallback);
  ros::Subscriber cood_sub = nh.subscribe("/darknet_ros/bounding_boxes",100, msgCallback);
  ros::spin();
  cv::destroyWindow("view");
}

void msgCallback(const darknet_ros_msgs::BoundingBoxes::ConstPtr& msg)
{
    cout<<"Bouding Boxes (header):" << msg->header <<endl;
    cout<<"Bouding Boxes (image_header):" << msg->image_header <<endl;
    cout<<"Bouding Boxes (Class):" << msg->bounding_boxes[0].Class <<endl;
    cout<<"Bouding Boxes (xmin):" << msg->bounding_boxes[0].xmin <<endl;
    cout<<"Bouding Boxes (xmax):" << msg->bounding_boxes[0].xmax <<endl;
    cout<<"Bouding Boxes (ymin):" << msg->bounding_boxes[0].ymin <<endl;
    cout<<"Bouding Boxes (ymax):" << msg->bounding_boxes[0].ymax <<endl;
    cout<<"Bouding Boxes (probability):" << msg->bounding_boxes[0].probability <<endl;
    cout<<"\033[2J\033[1;1H";     // clear terminal
}

123

I've solved it. :)