Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
YvesSchoenberg committed Oct 16, 2023
1 parent 4069d15 commit 5d801ce
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 455 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ros_utils.get_images_from_bag(
output_folder="/output_imgs/",
file_format="jpg",
create_manifest=True,
topics="/camera/image_raw",
topics=["/camera/image_raw"],
naming="sequential",
resize=[640,480],
sample=2,
Expand Down Expand Up @@ -65,7 +65,7 @@ Do you have a request for a data format that's not listed above? Raise an issue

## Community

If you have any questions, comments, or want to chat, please join [our Slack channel](#).
If you have any questions, comments, or want to chat, please join [our Discord channel](https://discord.com/invite/rvXqP6EjwF).

## Contribute
### How to Contribute
Expand Down
Empty file removed python/README.md
Empty file.
14 changes: 4 additions & 10 deletions python/robologs_ros_utils/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import click

from robologs_ros_utils.sources.ros1 import (
clip_rosbag,
get_csv_data_from_bag,
get_images_from_bag,
get_summary_from_bag,
get_videos_from_bag,
split_rosbag,
)
from robologs_ros_utils.sources.ros1 import (get_csv_data_from_bag,
get_images_from_bag,
get_summary_from_bag,
get_videos_from_bag)


@click.group(invoke_without_command=True)
Expand All @@ -33,9 +29,7 @@ def cli(ctx):
cli.add_command(get_images_from_bag.get_images)
cli.add_command(get_videos_from_bag.get_videos)
cli.add_command(get_summary_from_bag.get_summary)
cli.add_command(clip_rosbag.clip_rosbag)
cli.add_command(get_csv_data_from_bag.get_csv_data_from_bag)
cli.add_command(split_rosbag.split_rosbag)


def main():
Expand Down
3 changes: 2 additions & 1 deletion python/robologs_ros_utils/sources/ros1/argument_parsers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
def get_width_height_from_args(resize_arg: str) -> list:
"""
Get width and height from resize argument
Args:
resize_arg (str):
Returns:
Returns: list of width and height
"""
if not resize_arg:
Expand Down
62 changes: 0 additions & 62 deletions python/robologs_ros_utils/sources/ros1/clip_rosbag.py

This file was deleted.

4 changes: 1 addition & 3 deletions python/robologs_ros_utils/sources/ros1/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import click

from . import clip_rosbag, get_images_from_bag, get_summary_from_bag, get_videos_from_bag, split_rosbag
from . import get_images_from_bag, get_summary_from_bag, get_videos_from_bag


@click.group()
Expand All @@ -14,5 +14,3 @@ def ros():
ros.add_command(get_images_from_bag.get_images)
ros.add_command(get_videos_from_bag.get_videos)
ros.add_command(get_summary_from_bag.get_summary)
ros.add_command(clip_rosbag.clip_rosbag)
ros.add_command(split_rosbag.split_rosbag)
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os

import click

from robologs_ros_utils.sources.ros1 import ros_utils
from robologs_ros_utils.utils import file_utils


def process_topics(ctx, param, value):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os.path
import json
import os
import os.path

import click

Expand Down
72 changes: 39 additions & 33 deletions python/robologs_ros_utils/sources/ros1/ros_img_tools.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,75 @@
import glob
import io
import os
import subprocess

import cv2
import numpy as np
from PIL import Image


def convert_compressed_depth_to_cv2(compressed_depth):
"""
Convert a compressedDepth topic image into a cv2 image.
compressed_depth must be from a topic /bla/compressedDepth
as it's encoded in PNG
Code from: https://answers.ros.org/question/249775/display-compresseddepth-image-python-cv2/
Converts a ROS compressedDepth image into an OpenCV image.
Args:
compressed_depth: A sensor_msgs/CompressedImage message from a ROS compressedDepth topic.
Returns:
OpenCV image representing the depth image.
Raises:
Exception: If the compression type is not 'compressedDepth' or the depth image could not be decoded.
Note:
Code adapted from: https://answers.ros.org/question/249775/display-compresseddepth-image-python-cv2/
"""
depth_fmt, compr_type = compressed_depth.format.split(";")
# remove white space
depth_fmt = depth_fmt.strip()
compr_type = compr_type.strip().replace(" png", "")

if compr_type != "compressedDepth":
raise Exception("Compression type is not 'compressedDepth'." "You probably subscribed to the wrong topic.")
raise Exception("Compression type is not 'compressedDepth'. Incorrect topic subscribed.")

depth_header_size = 12
raw_data = compressed_depth.data[depth_header_size:]

depth_img_raw = cv2.imdecode(np.frombuffer(raw_data, np.uint8), 0)
depth_img_raw = cv2.imdecode(np.frombuffer(raw_data, np.uint8), cv2.IMREAD_UNCHANGED)

if depth_img_raw is None:
# probably wrong header size
raise Exception("Could not decode compressed depth image." "You may need to change 'depth_header_size'!")
result = cv2.normalize(depth_img_raw, depth_img_raw, 0, 255, norm_type=cv2.NORM_MINMAX)
raise Exception("Could not decode compressed depth image. Adjust 'depth_header_size' if necessary.")

im_color = cv2.applyColorMap(depth_img_raw, cv2.COLORMAP_JET)
depth_img_norm = cv2.normalize(depth_img_raw, depth_img_raw, 0, 255, norm_type=cv2.NORM_MINMAX)
depth_img_colormap = cv2.applyColorMap(depth_img_raw, cv2.COLORMAP_JET)

return im_color
return depth_img_colormap


def convert_image_to_cv2(msg):
"""
Converts a ROS image message into an OpenCV image.
Args:
msg ():
msg: A sensor_msgs/Image message.
Returns:
OpenCV image.
"""
np_arr = np.fromstring(msg.data, np.uint8)
image_np = cv2.imdecode(np_arr, cv2.IMREAD_UNCHANGED)
return image_np
np_arr = np.frombuffer(msg.data, np.uint8)
return cv2.imdecode(np_arr, cv2.IMREAD_UNCHANGED)


def create_video_from_images(input_path, output_path, output_name="video.mp4", frame_rate=12, resize=None):
"""
Creates a video from a collection of images stored in a specified directory.
Args:
input_path ():
output_path ():
output_name ():
frame_rate ():
resize ():
input_path (str): Directory where the input images are stored.
output_path (str): Directory where the output video will be saved.
output_name (str): Name of the output video file. Defaults to 'video.mp4'.
frame_rate (int): Frame rate of the output video. Defaults to 12.
resize (float, optional): Factor by which to resize the images. If None, images are not resized.
Returns:
None
"""
img_array = []
output_video_path = os.path.join(output_path, output_name)
Expand All @@ -69,21 +78,18 @@ def create_video_from_images(input_path, output_path, output_name="video.mp4", f
img_list = sorted(glob.glob(os.path.join(input_path, "./*.jpg")))

if not img_list:
sorted(glob.glob(os.path.join(input_path, "./*.png")))
img_list = sorted(glob.glob(os.path.join(input_path, "./*.png")))

if not img_list:
return
print("No images found in the specified directory.")
return

for filename in img_list:
img = cv2.imread(filename)
if resize:
img = cv2.resize(img, (0, 0), fx=resize, fy=resize, interpolation=cv2.INTER_LANCZOS4)

if len(img.shape) == 3:
height, width, _ = img.shape
else:
height, width = img.shape

height, width = (img.shape[0], img.shape[1]) if len(img.shape) == 3 else img.shape
size = (width, height)
img_array.append(img)

Expand All @@ -93,6 +99,6 @@ def create_video_from_images(input_path, output_path, output_name="video.mp4", f
out.write(img_array[i])
out.release()

# Using FFmpeg to convert the temporary video to final format
subprocess.call(["ffmpeg", "-i", output_video_path_temp, "-vcodec", "libx264", "-y", output_video_path])
os.remove(output_video_path_temp)
return
Loading

0 comments on commit 5d801ce

Please sign in to comment.