Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions rosbags2video/bag2video.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def write_frames(
convert = {topics[i]: i for i in range(len(topics))}
frame_duration = 1.0 / fps

# Initialize with 3 channels, will be adjusted if needed
images = [
np.zeros((sizes[i][1], sizes[i][0], 3), np.uint8) for i in range(len(topics))
]
Expand Down Expand Up @@ -64,7 +65,26 @@ def write_frames(
time = stamp_to_sec(msg.header.stamp)

if init:
image = message_to_cvimage(msg, encoding)
# Try to convert with the specified encoding first
try:
image = message_to_cvimage(msg, encoding)
except Exception as e:
# If conversion fails and it's likely a grayscale image, try mono8
if "8UC1" in str(e) or "mono" in str(e).lower():
logging.info(
f"Detected grayscale image, switching to mono8 encoding for topic {topic}"
)
encoding = "mono8"
image = message_to_cvimage(msg, encoding)
# Convert grayscale to 3-channel for consistency
if len(image.shape) == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
else:
raise e

# Ensure all images are 3-channel for consistency
if len(image.shape) == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

if timestamp_all:
image = embed_timestamp(image, time, add_timestamp, add_raw_timestamp)
Expand Down Expand Up @@ -104,7 +124,26 @@ def write_frames(
frame_num = frame_num_next
num_msgs += 1

image = message_to_cvimage(msg, encoding)
# Try to convert with the current encoding
try:
image = message_to_cvimage(msg, encoding)
except Exception as e:
# If conversion fails and it's likely a grayscale image, try mono8
if "8UC1" in str(e) or "mono" in str(e).lower():
logging.info(
f"Detected grayscale image, switching to mono8 encoding for topic {topic}"
)
encoding = "mono8"
image = message_to_cvimage(msg, encoding)
# Convert grayscale to 3-channel for consistency
if len(image.shape) == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
else:
raise e

# Ensure all images are 3-channel for consistency
if len(image.shape) == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)

if timestamp_all:
image = embed_timestamp(image, time, add_timestamp, add_raw_timestamp)
Expand Down
23 changes: 23 additions & 0 deletions rosbags2video/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ def get_sizes(bag_reader, topics=None, index=0, scale=1.0):
for topic in topics:
try:
connections = [x for x in bag_reader.connections if x.topic == topic]

# Check if topic exists in the bag
if not connections:
logging.critical(f"Topic '{topic}' not found in bag file.")
logging.critical("Available topics:")
for conn in bag_reader.connections:
logging.critical(f" {conn.topic}: {conn.msgtype}")
sys.exit(1)

# Check if the topic is an image type
connection = connections[0]
if not (
connection.msgtype.endswith("/Image")
or connection.msgtype.endswith("/CompressedImage")
):
logging.critical(
f"Topic '{topic}' is not an image topic (type: {connection.msgtype})."
)
logging.critical(
"Only sensor_msgs/Image and sensor_msgs/CompressedImage topics are supported."
)
sys.exit(1)

for connection, timestamp, rawdata in bag_reader.messages(
connections=connections
):
Expand Down