2022-01-31 13:35:42 +01:00
|
|
|
"""Example: Save images as rosbag1."""
|
|
|
|
|
|
|
|
|
|
import numpy
|
|
|
|
|
|
|
|
|
|
from rosbags.rosbag1 import Writer
|
2022-09-22 21:46:00 +02:00
|
|
|
from rosbags.serde import serialize_ros1
|
2022-01-31 13:35:42 +01:00
|
|
|
from rosbags.typesys.types import builtin_interfaces__msg__Time as Time
|
|
|
|
|
from rosbags.typesys.types import sensor_msgs__msg__CompressedImage as CompressedImage
|
|
|
|
|
from rosbags.typesys.types import std_msgs__msg__Header as Header
|
|
|
|
|
|
|
|
|
|
TOPIC = '/camera'
|
|
|
|
|
FRAMEID = 'map'
|
|
|
|
|
|
|
|
|
|
# Contains filenames and their timestamps
|
|
|
|
|
IMAGES = [
|
|
|
|
|
('homer.jpg', 42),
|
|
|
|
|
('marge.jpg', 43),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_images() -> None:
|
|
|
|
|
"""Iterate over IMAGES and save to output bag."""
|
|
|
|
|
with Writer('output.bag') as writer:
|
|
|
|
|
conn = writer.add_connection(TOPIC, CompressedImage.__msgtype__)
|
|
|
|
|
|
|
|
|
|
for path, timestamp in IMAGES:
|
|
|
|
|
message = CompressedImage(
|
|
|
|
|
Header(
|
|
|
|
|
stamp=Time(
|
|
|
|
|
sec=int(timestamp // 10**9),
|
|
|
|
|
nanosec=int(timestamp % 10**9),
|
|
|
|
|
),
|
|
|
|
|
frame_id=FRAMEID,
|
|
|
|
|
),
|
|
|
|
|
format='jpeg', # could also be 'png'
|
2022-04-11 00:07:53 +02:00
|
|
|
data=numpy.fromfile(path, dtype=numpy.uint8),
|
2022-01-31 13:35:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
writer.write(
|
|
|
|
|
conn,
|
|
|
|
|
timestamp,
|
2022-09-22 21:46:00 +02:00
|
|
|
serialize_ros1(message, CompressedImage.__msgtype__),
|
2022-01-31 13:35:42 +01:00
|
|
|
)
|