-2

`hi

cam_reader.hpp

#include "rclcpp/rclcpp.hpp" #include "opencv2/opencv.hpp" #include "sensor_msgs/msg/image.hpp" #include "cv_bridge/cv_bridge.h"

class CamReader : public rclcpp::Node { public: CamReader();

cv::VideoCapture cap;

private:

Subscribers

Publishers rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr image_publisher_;

Timers rclcpp::TimerBase::SharedPtr get_image_timer_;

Callbacks void get_image_callback_();

};

cam_reader.hpp #include <cam_reader/cam_reader.hpp>

CamReader::CamReader () Node("cam_reader")

char videopath[] = "/home/pi/Meurtres_au_paradis.S01E01.mp4"; this->cap.open(*videopath); this->cap.open(0); if (!this->cap.isOpened()) { RCLCPP_INFO(this->get_logger(), "Error opening camera"); }

get_image_timer_ = this->create_wall_timer( std::chrono::milliseconds(2000), std::chrono::milliseconds(50), std::bind(&CamReader::get_image_callback_, this));

auto default_qos = rclcpp::QoS(rclcpp::SystemDefaultsQoS());

Force to be reliable, some DDS have set defaultQoS to best effort default_qos.reliable();

image_publisher_ = this->create_publisher<sensor_msgs::msg::Image>("/image", default_qos); }

void CamReader::get_image_callback_() { cv::Mat frame;

this->cap >> frame; // get a new frame from camera

cv_bridge::CvImage img_bridge; sensor_msgs::msg::Image cam_msg;

std_msgs::msg::Header header; // empty header

img_bridge = cv_bridge::CvImage(header, sensor_msgs::image_encodings::RGB8, frame); img_bridge.toImageMsg(cam_msg); // from cv_bridge to sensor_msgs::Image

image_publisher_->publish(cam_msg); }

int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared()); rclcpp::shutdown(); return 0; }

It's ok with this->cap.open(0); but error with this->cap.open(*videopath); pi@pi-desktop:~/face-recognition$ ros2 run cam_reader cam_reader_node WARN:0@0.203] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (2401) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device '/dev/video47'. WARN:0@0.203] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline WARN:0@0.204] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created WARN:0@0.204] global /home/pi/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video47): can't open camera by index [INFO] [1679388758.511573816] [cam_reader]: Error opening camera

There is no problem playing the video with a non-ROS2 c++ program and I don't understand the error on opening the video. I have to go through c++ for face detection with DNN because python is too slow.

1 Answers1

0

In the example that I took as a basis, the opening of the video was done in the callback. But while trying to open the video I had a compilation error which disappeared with the introduction of the pointer on videopath (*videopath). Despite everything without compilation error I had the same error at runtime. Since I transferred the opening of the video in the opening of the node. I just noticed that by removing the pointer this->cap.open(*videopath) in this->cap.open(videopath); the video worked. So it's solved.