Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsk_rosbag_tools package #1738

Merged
merged 45 commits into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c90651d
Add jsk_rosbag_tools package
iory May 2, 2022
7218ec9
[jsk_rosbag_tools] Drop catkin virtualenv
iory Jun 1, 2022
1285a82
[jsk_rosbag_tools] Set version to 2.2.11
iory Jun 1, 2022
1a67212
[jsk_rosbag_tools] Modified sample bagfile to reduce test time
iory Jun 1, 2022
cdc7f5c
[jsk_rosbag_tools] Set time-limit to 360.0
iory Jun 1, 2022
fd1d92c
[jsk_rosbag_tools] Add makedirs library for python2
iory Jun 1, 2022
bf8fb8b
[jsk_rosbag_tools] Add topic_name_to_file_name
iory Jun 1, 2022
aa6058b
[jsk_rosbag_tools] Seperate bag_to_video function as library
iory Jun 1, 2022
a6169b3
[jsk_rosbag_tools] Use library's function
iory Jun 1, 2022
d91d628
[jsk_rosbag_tools] Import AudioFileClip and VideoFileClip
iory Jun 1, 2022
0684b85
[jsk_rosbag_tools] Fixed bag to video docs
iory Jun 1, 2022
b185faa
[jsk_rosbag_tools] Fixed sample docs
iory Jun 1, 2022
c8e4efb
[jsk_rosbag_tools] chmod u+x
iory Jun 1, 2022
49d0f51
[jsk_rosbag_tools] Include AudioFileClip and VideoFileClip to prevent…
iory Jun 1, 2022
5566452
[jsk_rosbag_tools] Makedirs in extract_audio
iory Jun 1, 2022
40ad381
[jsk_rosbag_tools] Add bag_to_audio.py
iory Jun 1, 2022
fecd5ff
[jsk_rosbag_tools] chmod u+x test_jsk_rosbag_tools.py
iory Jun 1, 2022
720818e
[jsk_rosbag_tools] Add shebang
iory Jun 1, 2022
d0f0e12
[jsk_rosbag_tools] Enable python2 compatibility
iory Jun 1, 2022
4c69aab
[jsk_rosbag_tools] Refactor dependencies
iory Jun 1, 2022
28c85e4
[jsk_rosbag_tools] Decompress as much as possible
iory Jun 2, 2022
d7969e5
[jsk_rosbag_tools] Fixed E225 missing whitespace around operator
iory Jun 2, 2022
6fdbfcf
[jsk_rosbag_tools] Rename extract_audio to bag_to_audio
iory Jun 2, 2022
186417d
[jsk_rosbag_tools] Fixed W503 line break before binary operator
iory Jun 2, 2022
46df725
[jsk_rosbag_tools] Check topic exists
iory Jun 2, 2022
117d511
[jsk_rosbag_tools] Fixed compr_type condition
iory Jun 2, 2022
b1a3918
[jsk_rosbag_tools] Allow to record mono8 topics for mask images
Affonso-Gui Jun 2, 2022
3b03d0e
[jsk_rosbag_tools] Fix bug when saving to local path with --image-topic
Affonso-Gui Jun 2, 2022
d3f7727
[jsk_rosbag_tools] Resize every frame
iory Jun 4, 2022
f3ec40d
[jsk_rosbag_tools] Add message to raise
iory Jun 4, 2022
34986eb
[jsk_rosbag_tools] Add fps option
iory Jun 9, 2022
84fe119
[jsk_rosbag_tools] Add .wav ext
iory Jun 9, 2022
12fcea8
[jsk_rosbag_tools] Add show_progress_bar option
iory Jun 9, 2022
15c0a94
[jsk_rosbag_tools] Fixed E226 missing whitespace around arithmetic op…
iory Jun 9, 2022
fd04e31
[jsk_rosbag_tools] Fixed W503 line break before binary operator
iory Jun 9, 2022
f334654
[jsk_rosbag_tools] Add resize_keeping_aspect_ratio_wrt_target_size to…
iory Jun 9, 2022
e881250
[jsk_rosbag_tools] Avoid raise ValueError on invalid compressed format.
iory Jun 9, 2022
ba11a33
[jsk_rosbag_tools] Refactor test to print command log
iory Jun 27, 2022
e5fc229
[jsk_rosbag_tools] Add dependencies of download data
iory Jun 27, 2022
6fb2642
[jsk_rosbag_tools] Drop installation of requirements.in and requireme…
iory Jun 27, 2022
1ec14ee
[jsk_rosbag_tools] Split test to avoid indigo's error
iory Jun 27, 2022
00f043b
[jsk_rosbag_tools] logerr subprocess command if failed
iory Jun 28, 2022
a1786d1
[jsk_rosbag_tools] Specify python version
iory Jun 28, 2022
fb24554
[jsk_rosbag_tools] Enable catkin_virtualenv for pip dependencies
iory Jun 30, 2022
007febb
[jsk_rosbag_tools] Add descriptions for static_tf_republisher.py.
iory Jun 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions jsk_common/package.xml
Expand Up @@ -19,6 +19,7 @@
<exec_depend>audio_video_recorder</exec_depend>
<exec_depend>dynamic_tf_publisher</exec_depend>
<exec_depend>image_view2</exec_depend>
<exec_depend>jsk_rosbag_tools</exec_depend>
<exec_depend>jsk_topic_tools</exec_depend>
<exec_depend>jsk_tools</exec_depend>
<exec_depend>multi_map_server</exec_depend>
Expand Down
1 change: 1 addition & 0 deletions jsk_rosbag_tools/.gitignore
@@ -0,0 +1 @@
!.gitignore
56 changes: 56 additions & 0 deletions jsk_rosbag_tools/CMakeLists.txt
@@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 2.8.3)
project(jsk_rosbag_tools)

find_package(
catkin REQUIRED
)

catkin_python_setup()

catkin_package(
CATKIN_DEPENDS
)

file(GLOB SCRIPTS_FILES scripts/*)
catkin_install_python(
PROGRAMS ${SCRIPTS_FILES}
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

catkin_download(download_audio_data
https://drive.google.com/uc?export=download&id=1rFZYoFjLqIWjEe0DaNiL3k9893m31nu7
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/samples/data
FILENAME 2022-05-07-hello-test.bag
MD5 3650e27dad2c7dc0e447033259290db6
)
catkin_download(download_video_data
https://drive.google.com/uc?export=download&id=1v4YNOHnHYxLOty1lYR2R6lfNF0itCwK7
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/samples/data
FILENAME 20220530173950_go_to_kitchen_rosbag.bag
MD5 d51fa8aeacd36f7aaa1597b67bd9ffdf
)
add_custom_target(download ALL DEPENDS download_audio_data download_video_data)

if(CATKIN_ENABLE_TESTING)
find_package(catkin REQUIRED COMPONENTS roslint rostest)

set(python_test_scripts
tests/test_jsk_rosbag_tools.py
tests/test_bag_to_video.py
)

roslint_python()
roslint_python(${python_test_scripts})
roslint_add_test()

catkin_install_python(PROGRAMS ${python_test_scripts}
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

add_rostest(tests/test_jsk_rosbag_tools.test
DEPENDENCIES download_audio_data download_video_data)
if("$ENV{ROS_DISTRO}" STRGREATER "indigo")
# could not install moviepy on indigo.
add_rostest(tests/test_bag_to_video.test
DEPENDENCIES download_audio_data download_video_data)
endif()
endif()
189 changes: 189 additions & 0 deletions jsk_rosbag_tools/README.md
@@ -0,0 +1,189 @@
# jsk_rosbag_tools

Tools such as creating video from rosbag and compressing rosbag images.

## bag_to_video.py

Create video from rosbag.

### Usage

```
usage: bag_to_video.py [-h] [--out OUT] [--fps FPS] [--samplerate SAMPLERATE] [--channels CHANNELS] [--audio-topic AUDIO_TOPIC] [--image-topic IMAGE_TOPIC [IMAGE_TOPIC ...]] input_bagfile

rosbag to video

positional arguments:
input_bagfile

optional arguments:
-h, --help show this help message and exit
--out OUT, -o OUT output directory path or filename.
If more than one --image-topic are specified,
this will be interpreted as a directory name.
Otherwise this is the file name.
--fps FPS
--samplerate SAMPLERATE, -r SAMPLERATE
sampling rate
--channels CHANNELS number of input channels
--audio-topic AUDIO_TOPIC
--image-topic IMAGE_TOPIC [IMAGE_TOPIC ...]
Topic name to extract.
```

### Example

```
rosrun jsk_rosbag_tools bag_to_video.py $(rospack find jsk_rosbag_tools)/samples/data/20220530173950_go_to_kitchen_rosbag.bag \
--samplerate 16000 --channels 1 --audio-topic /audio \
--image-topic /head_camera/rgb/throttled/image_rect_color/compressed \
-o /tmp/20220530173950_go_to_kitchen_rosbag.mp4
```

## bag_to_audio.py

Create audio file from rosbag.

### Usage

```
usage: bag_to_audio.py [-h] [--out OUT] [--samplerate SAMPLERATE] [--channels CHANNELS] [--audio-topic AUDIO_TOPIC] input_bagfile

rosbag to audio

positional arguments:
input_bagfile

optional arguments:
-h, --help show this help message and exit
--out OUT, -o OUT output filename. If `--audio-topic`_info is exists, you don't have to specify samplerate and channels.
--samplerate SAMPLERATE, -r SAMPLERATE
sampling rate
--channels CHANNELS number of input channels
--audio-topic AUDIO_TOPIC
```

### Example

```
rosrun jsk_rosbag_tools bag_to_audio.py $(rospack find jsk_rosbag_tools)/samples/data/20220530173950_go_to_kitchen_rosbag.bag \
--samplerate 16000 --channels 1 --audio-topic /audio \
-o /tmp/20220530173950_go_to_kitchen_rosbag.wav
```

## video_to_bag.py

Convert video file to bagfile.

### Usage

```
usage: video_to_bag.py [-h] [--out output_file] [--topic-name TOPIC_NAME] [--compress] [--no-progress-bar] inputvideo

Convert video to bag.

positional arguments:
inputvideo

optional arguments:
-h, --help show this help message and exit
--out output_file, -o output_file
name of the output bag file
--topic-name TOPIC_NAME
Converted topic name.
--compress Compress Image flag.
--no-progress-bar Don't show progress bar.
```

### Example

```
rosrun jsk_rosbag_tools video_to_bag.py /tmp/output_bag/head_camera--slash--rgb--slash--throttled--slash--image_rect_color--slash--compressed-with-audio.mp4 \
-o /tmp/output_bag/video.bag --compress
```

## compress_imgs.py

Convert `Image` messages to `CompressedImage` or `CompressedDepthImage`.

### Usage

```
usage: compress_imgs.py [-h] [--out OUT] [--compressed-topics [COMPRESSED_TOPICS [COMPRESSED_TOPICS ...]]] [--replace] [--no-progress-bar] input_bagfile

Convert Image messages to CompressedImage or CompressedDepthImage

positional arguments:
input_bagfile input bagfile path

optional arguments:
-h, --help show this help message and exit
--out OUT, -o OUT output bagfile path
--compressed-topics [COMPRESSED_TOPICS [COMPRESSED_TOPICS ...]]
this image topics are compressed
--replace
--no-progress-bar Don't show progress bar.
```

### Example

```
rosrun jsk_rosbag_tools compress_imgs.py $(rospack find jsk_rosbag_tools)/samples/data/20220530173950_go_to_kitchen_rosbag.bag \
-o /tmp/20220530173950_go_to_kitchen_rosbag-compressed.bag
```

## tf_static_to_tf.py

Convert tf_static to tf and save it as a rosbag.

```
usage: tf_static_to_tf.py [-h] [--out OUT] [--no-progress-bar] input_bagfile

Convert tf_static to tf and save it as a rosbag

positional arguments:
input_bagfile input bagfile path

optional arguments:
-h, --help show this help message and exit
--out OUT, -o OUT output bagfile path
--no-progress-bar Don't show progress bar.
```

### Example

```
rosrun jsk_rosbag_tools tf_static_to_tf.py $(rospack find jsk_rosbag_tools)/samples/data/20220530173950_go_to_kitchen_rosbag.bag
```

## merge.py

Merges two bagfiles.

### Usage

```
usage: merge.py [-h] [--out output_file] [--topics TOPICS] [-i] main_bagfile bagfile

Merges two bagfiles.

positional arguments:
main_bagfile path to a bagfile, which will be the main bagfile
bagfile path to a bagfile which should be merged to the main bagfile

optional arguments:
-h, --help show this help message and exit
--out output_file, -o output_file
name of the output file
--topics TOPICS, -t TOPICS
topics which should be merged to the main bag
-i reindex bagfile
```

### Example

```
rosrun jsk_rosbag_tools merge.py \
$(rospack find jsk_rosbag_tools)/samples/data/20220530173950_go_to_kitchen_rosbag.bag \
$(rospack find jsk_rosbag_tools)/samples/data/2022-05-07-hello-test.bag
```
37 changes: 37 additions & 0 deletions jsk_rosbag_tools/package.xml
@@ -0,0 +1,37 @@
<?xml version="1.0"?>
<package format="3">
<name>jsk_rosbag_tools</name>
<version>2.2.11</version>
<description>The rosbag tools</description>
<license>BSD</license>

<maintainer email="yanokura@jsk.imi.i.u-tokyo.ac.jp">Iori Yanokura</maintainer>
<author email="yanokura@jsk.imi.i.u-tokyo.ac.jp">Iori Yanokura</author>

<buildtool_depend>catkin</buildtool_depend>
<buildtool_depend>python-catkin-pkg-modules</buildtool_depend>

<exec_depend>audio_common_msgs</exec_depend>
<exec_depend>cv_bridge</exec_depend>
<exec_depend>ffmpeg</exec_depend>
<exec_depend>python-moviepy-pip</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 2">python-numpy</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 2">python-rospkg-modules</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 2">python-scipy</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 2">python-setuptools</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 2">python-termcolor</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 2">python-tqdm</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 2">python-yaml</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-numpy</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-rospkg-modules</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-scipy</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-setuptools</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-termcolor</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-tqdm</exec_depend>
<exec_depend condition="$ROS_PYTHON_VERSION == 3">python3-yaml</exec_depend>
<exec_depend>sensor_msgs</exec_depend>

<test_depend>roslint</test_depend>
<test_depend>rostest</test_depend>

</package>
1 change: 1 addition & 0 deletions jsk_rosbag_tools/python/jsk_rosbag_tools/__init__.py
@@ -0,0 +1 @@
# flake8: noqa
50 changes: 50 additions & 0 deletions jsk_rosbag_tools/python/jsk_rosbag_tools/bag_to_audio.py
@@ -0,0 +1,50 @@
import os
import os.path as osp

import numpy as np
import rosbag
from scipy.io.wavfile import write as wav_write

from jsk_rosbag_tools.extract import extract_oneshot_topic
from jsk_rosbag_tools.info import get_topic_dict
from jsk_rosbag_tools.makedirs import makedirs


def bag_to_audio(bag_filepath,
wav_outpath,
topic_name='/audio',
audio_info_topic_name=None,
samplerate=44100,
channels=1,
overwrite=True):
if os.path.exists(wav_outpath) and overwrite is False:
raise FileExistsError('{} file already exists.'.format(wav_outpath))
topic_dict = get_topic_dict(bag_filepath)
if topic_name not in topic_dict:
return

audio_info_topic_name = audio_info_topic_name or topic_name + '_info'
# if audio_info_topic exists, extract info from it.
if audio_info_topic_name in topic_dict:
audio_info = extract_oneshot_topic(bag_filepath, audio_info_topic_name)
if audio_info is not None:
samplerate = audio_info.sample_rate
channels = audio_info.channels

bag = rosbag.Bag(bag_filepath)
audio_buffer = []
for _, msg, _ in bag.read_messages(topics=[topic_name]):
if msg._type == 'audio_common_msgs/AudioData':
buf = np.frombuffer(msg.data, dtype='int16')
buf = buf.reshape(-1, channels)
audio_buffer.append(buf)
audio_buffer = np.concatenate(audio_buffer, axis=0)

makedirs(osp.dirname(wav_outpath))
wav_write(wav_outpath, rate=samplerate, data=audio_buffer)

valid = os.stat(wav_outpath).st_size != 0
if valid is False:
return False

return True