-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb3toimg.py
More file actions
31 lines (25 loc) · 1.02 KB
/
db3toimg.py
File metadata and controls
31 lines (25 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import cv2
import numpy as np
import rosbag2_py
from cv_bridge import CvBridge
def render_video_from_rosbag(bag_path, topic_name, output_file, fps=30):
# ROS2 reader
reader = rosbag2_py.SequentialReader()
storage_options = rosbag2_py.StorageOptions(uri=bag_path, storage_id="sqlite3")
converter_options = rosbag2_py.ConverterOptions("", "")
reader.open(storage_options, converter_options)
bridge = CvBridge()
writer = None
while reader.has_next():
(topic, data, t) = reader.read_next()
if topic == topic_name:
# Convert ROS image message to OpenCV
img_msg = bridge.deserialize(data, "sensor_msgs/msg/Image")
frame = bridge.imgmsg_to_cv2(img_msg, desired_encoding="bgr8")
if writer is None:
h, w, _ = frame.shape
writer = cv2.VideoWriter(output_file, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
writer.write(frame)
if writer:
writer.release()
print(f"Video saved at {output_file}")