有ROS2中的网络摄像头发布存在小问题。

huangapple go评论103阅读模式
英文:

have little problem with webcam publish in ROS2

问题

I am currently your Chinese translator, I will only return the translated parts and avoid including any additional content. Here is the translation of your provided code:

这是发布者的代码:

  1. #include <chrono>
  2. #include <memory>
  3. #include "cv_bridge/cv_bridge.h"
  4. #include "rclcpp/rclcpp.hpp"
  5. #include "sensor_msgs/msg/image.hpp"
  6. #include "std_msgs/msg/header.hpp"
  7. #include <opencv2/opencv.hpp>
  8. #include <stdio.h>
  9. #include <opencv2/core/types.hpp>
  10. #include <opencv2/core/hal/interface.h>
  11. #include <image_transport/image_transport.hpp>
  12. #include <opencv2/imgproc/imgproc.hpp>
  13. using namespace std::chrono_literals;
  14. using namespace cv;
  15. class MinimalPublisher : public rclcpp::Node {
  16. public:
  17. MinimalPublisher() : Node("minimal_publisher"), count_(0) {
  18. auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
  19. publisher_ = this->create_publisher<sensor_msgs::msg::Image>("topic", qos_profile);
  20. timer_ = this->create_wall_timer(10ms, std::bind(&MinimalPublisher::timer_callback, this));
  21. }
  22. private:
  23. void timer_callback() {
  24. cv_bridge::CvImagePtr cv_ptr;
  25. cv::VideoCapture cap(0);
  26. cv::Mat img(cv::Size(1280, 720), CV_8UC3);
  27. cap >> img;
  28. sensor_msgs::msg::Image::SharedPtr msg = cv_bridge::CvImage(std_msgs::msg::Header(), "bgr8", img).toImageMsg();
  29. publisher_->publish(*msg);
  30. RCLCPP_INFO(this->get_logger(), "publishing");
  31. }
  32. rclcpp::TimerBase::SharedPtr timer_;
  33. rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr publisher_;
  34. size_t count_;
  35. };
  36. int main(int argc, char *argv[]) {
  37. printf("Starting...");
  38. rclcpp::init(argc, argv);
  39. rclcpp::spin(std::make_shared<MinimalPublisher>());
  40. rclcpp::shutdown();
  41. return 0;
  42. }

订阅者的代码:

  1. #include <chrono>
  2. #include <memory>
  3. #include "cv_bridge/cv_bridge.h"
  4. #include "rclcpp/rclcpp.hpp"
  5. #include "sensor_msgs/msg/image.hpp"
  6. #include "std_msgs/msg/header.hpp"
  7. #include <opencv2/opencv.hpp>
  8. #include <opencv2/highgui.hpp>
  9. #include <stdio.h>
  10. #include <opencv2/core/types.hpp>
  11. #include <opencv2/core/hal/interface.h>
  12. #include <image_transport/image_transport.hpp>
  13. #include <opencv2/imgproc/imgproc.hpp>
  14. using std::placeholders::_1;
  15. class MinimalSubscriber : public rclcpp::Node {
  16. public:
  17. MinimalSubscriber() : Node("minimal_subscriber") {
  18. auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
  19. subscription_ = this->create_subscription<sensor_msgs::msg::Image>(
  20. "topic", qos_profile, std::bind(&MinimalSubscriber::topic_callback, this, _1));
  21. }
  22. private:
  23. void topic_callback(sensor_msgs::msg::Image::SharedPtr msg) const {
  24. RCLCPP_INFO(this->get_logger(), "In callback");
  25. cv_bridge::CvImagePtr cv_ptr;
  26. cv_ptr = cv_bridge::toCvCopy(msg, "bgr8");
  27. cv::imshow("minimal_subscriber", cv_ptr->image);
  28. cv::waitKey(1);
  29. }
  30. rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr subscription_;
  31. };
  32. int main(int argc, char *argv[]) {
  33. rclcpp::init(argc, argv);
  34. rclcpp::spin(std::make_shared<MinimalSubscriber>());
  35. rclcpp::shutdown();
  36. return 0;
  37. }

希望能帮到你,如果有其他需要翻译的,请随时告诉我。

英文:

I'm currently using Ubuntu 20.04, ROS2-foxy
what I'm trying to do is publish webcam image with using cv2, cv_bridge and subscribe it.
I successfully publish and subscribe the webcam but not in real-time.
publish node is publishing webcam every two seconds. which means publishing image like delayed image.
I tried to search and fix the problem but and can't make it so I need some help.
I will post my publisher and subscriber code down here.

This is publisher code.

  1. #include &lt;chrono&gt;
  2. #include &lt;memory&gt;
  3. #include &quot;cv_bridge/cv_bridge.h&quot;
  4. #include &quot;rclcpp/rclcpp.hpp&quot;
  5. #include &quot;sensor_msgs/msg/image.hpp&quot;
  6. #include &quot;std_msgs/msg/header.hpp&quot;
  7. #include &lt;opencv2/opencv.hpp&gt;
  8. #include &lt;stdio.h&gt;
  9. #include &lt;opencv2/core/types.hpp&gt;
  10. #include &lt;opencv2/core/hal/interface.h&gt;
  11. #include &lt;image_transport/image_transport.hpp&gt;
  12. #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  13. using namespace std::chrono_literals;
  14. using namespace cv;
  15. class MinimalPublisher : public rclcpp::Node {
  16. public:
  17. MinimalPublisher() : Node(&quot;minimal_publisher&quot;), count_(0) {
  18. auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
  19. publisher_ = this-&gt;create_publisher&lt;sensor_msgs::msg::Image&gt;(&quot;topic&quot;, qos_profile);
  20. timer_ = this-&gt;create_wall_timer(10ms, std::bind(&amp;MinimalPublisher::timer_callback, this));
  21. }
  22. private:
  23. void timer_callback() {
  24. cv_bridge::CvImagePtr cv_ptr;
  25. cv::VideoCapture cap(0);
  26. cv::Mat img(cv::Size(1280, 720), CV_8UC3);
  27. cap &gt;&gt; img;
  28. sensor_msgs::msg::Image::SharedPtr msg = cv_bridge::CvImage(std_msgs::msg::Header(), &quot;bgr8&quot;, img).toImageMsg();
  29. publisher_-&gt;publish(*msg);
  30. RCLCPP_INFO(this-&gt;get_logger(), &quot;publishing&quot;);
  31. }
  32. rclcpp::TimerBase::SharedPtr timer_;
  33. rclcpp::Publisher&lt;sensor_msgs::msg::Image&gt;::SharedPtr publisher_;
  34. size_t count_;
  35. };
  36. int main(int argc, char *argv[]) {
  37. printf(&quot;Starting...&quot;);
  38. rclcpp::init(argc, argv);
  39. rclcpp::spin(std::make_shared&lt;MinimalPublisher&gt;());
  40. rclcpp::shutdown();
  41. return 0;
  42. }

Subscriber code:

  1. #include &lt;chrono&gt;
  2. #include &lt;memory&gt;
  3. #include &quot;cv_bridge/cv_bridge.h&quot;
  4. #include &quot;rclcpp/rclcpp.hpp&quot;
  5. #include &quot;sensor_msgs/msg/image.hpp&quot;
  6. #include &quot;std_msgs/msg/header.hpp&quot;
  7. #include &lt;opencv2/opencv.hpp&gt;
  8. #include &lt;opencv2/highgui.hpp&gt;
  9. #include &lt;stdio.h&gt;
  10. #include &lt;opencv2/core/types.hpp&gt;
  11. #include &lt;opencv2/core/hal/interface.h&gt;
  12. #include &lt;image_transport/image_transport.hpp&gt;
  13. #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  14. using std::placeholders::_1;
  15. class MinimalSubscriber : public rclcpp::Node {
  16. public:
  17. MinimalSubscriber()
  18. : Node(&quot;minimal_subscriber&quot;)
  19. {
  20. auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
  21. subscription_ = this-&gt;create_subscription&lt;sensor_msgs::msg::Image&gt;(
  22. &quot;topic&quot;, qos_profile, std::bind(&amp;MinimalSubscriber::topic_callback, this, _1));
  23. }
  24. private :
  25. void topic_callback(sensor_msgs::msg::Image::SharedPtr msg) const
  26. {
  27. RCLCPP_INFO(this-&gt;get_logger(), &quot;In callback&quot;);
  28. cv_bridge::CvImagePtr cv_ptr;
  29. cv_ptr = cv_bridge::toCvCopy(msg,&quot;bgr8&quot;);
  30. cv::imshow(&quot;minimal_subscriber&quot;, cv_ptr-&gt;image);
  31. cv::waitKey(1);
  32. }
  33. rclcpp::Subscription&lt;sensor_msgs::msg::Image&gt;::SharedPtr subscription_;
  34. };
  35. int main(int argc, char *argv[])
  36. {
  37. rclcpp::init(argc, argv);
  38. rclcpp::spin(std::make_shared&lt;MinimalSubscriber&gt;());
  39. rclcpp::shutdown();
  40. return 0;
  41. }

I want to see my webcam streaming working normally. I want this simple job happen in C++, cv_bridge, cv2.

答案1

得分: 1

每次调用timer_callback函数时,都会使用cv::VideoCapture cap(0);打开一个新的视频捕获流。视频流的打开和关闭导致了您正在经历的延迟问题。

已解决!

这是详细的代码:https://github.com/Robotisim/solving_ros_problems/commit/b099e5d787d91205225416953516813ba2dce7c3

英文:

Every time the timer_callback function is called, you're opening a new video capture stream with cv::VideoCapture cap(0);. The opening and closing of the video stream is be causing the delay you're experiencing.

Fixed it !

here is a detailed code : https://github.com/Robotisim/solving_ros_problems/commit/b099e5d787d91205225416953516813ba2dce7c3

huangapple
  • 本文由 发表于 2023年5月18日 12:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定