ROS subscribes to camera image messages and saves images as video frames

need

It is necessary to write a Python program to subscribe to the video message sent by the depth camera connected to the computer, record the video and save it as a picture frame by frame to the local for collecting and making the picture information of the data set

operating environment

Ubuntu18.04 + ROS Melodic + Python2.7

Python program

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge

class VideoRecorder:
    def __init__(self):
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber('/camera/color/image_raw', Image, self.image_callback)
        self.frames = []
        self.recording = False

    def image_callback(self, msg):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8")
            if self.recording:
                self.frames.append(cv_image)
        except Exception as e:
            print(e)

    def start_recording(self):
        self.frames = []
        self.recording = True

    def stop_recording(self):
        self.recording = False
        if self.frames:
            self.save_frames()

    def save_frames(self):
        for i, frame in enumerate(self.frames):
            filename = 'frame_{:04d}.jpg'.format(i)
            cv2.imwrite(filename, frame)
        print('Saved {} frames.'.format(len(self.frames)))

if __name__ == '__main__':
    rospy.init_node('video_recorder_node', anonymous=True)
    recorder = VideoRecorder()

    try:
        while not rospy.is_shutdown():
            cmd = raw_input("Enter 'start' to begin recording or 'stop' to stop recording: ")
            if cmd == 'start':
                recorder.start_recording()
            elif cmd == 'stop':
                recorder.stop_recording()
    except rospy.ROSInterruptException:
        pass

program explanation

This code is a Python program for ROS (Robot Operating System) environment to subscribe to camera image messages and save the images as video frames.

1. First, the code declares the use of the Python interpreter and sets the encoding format of the file to utf-8.

2. Imported the required libraries and modules:
   - `rospy`: Python client library for ROS, used to create ROS nodes, publish and subscribe messages, etc.
   - `cv2`: OpenCV library for image processing and computer vision tasks.
   - `Image`: message type from the `sensor_msgs.msg` module, used to transfer image data.
   - `CvBridge`: utility class for converting between ROS messages and OpenCV images.

3. Define a class named `VideoRecorder`, which contains the following methods:
   - `__init__(self)`: The initialization method of the class, in which some necessary settings are made. Created a `CvBridge` instance for conversion between ROS images and OpenCV images; subscribed to the camera image message named `/camera/color/image_raw`, and set the callback function to `image_callback`; initialized for storage A list `frames` of frames, and a flag `recording` indicating whether recording is in progress.
   - `image_callback(self, msg)`: callback function for image messages. Convert ROS image message to OpenCV format, if recording, add frame to `frames` list.
   - `start_recording(self)`: start recording method. Empty the `frames` list and set the `recording` flag to True.
   - `stop_recording(self)`: Stop recording method. Set the `recording` flag to False and call the `save_frames` method to save the recorded frames.
   - `save_frames(self)`: Save frames method. Traverse the `frames` list, save each frame image as a JPEG file, and output the number of saved frames to the console.

4. In the `if __name__ == '__main__':` statement block:
   - The ROS node is initialized, the node name is `video_recorder_node`, and it is set as an anonymous node.
   - An instance `recorder` of `VideoRecorder` class is created.
   - In an infinite loop, waiting for the user to enter a command. If the input is `start`, call the `start_recording` method to start recording; if the input is `stop`, call the `stop_recording` method to stop recording.
   - By catching `rospy.ROSInterruptException` to ensure that the program can exit normally when receiving a termination signal.

In summary, this code receives camera image messages through ROS, starts or stops recording image frames according to commands entered by the user, and then saves the recorded frames as JPEG files.

Guess you like

Origin blog.csdn.net/weixin_45498383/article/details/132232225