Skip to content

Commit

Permalink
feat: add rosbag viewing and converting tools
Browse files Browse the repository at this point in the history
  • Loading branch information
diyjac committed Aug 25, 2017
1 parent 21fbdc3 commit f120ddc
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ cd SDC-System-Integration
git push -u origin <your branch>
```
3. Install python dependencies
* With Pygame for Diagnostics
* With Pygame and MoviePy for Diagnostics and converting rosbags
* For non-GPU Linux systems
```bash
sudo -H pip install -r requirements-pygame.txt
Expand All @@ -84,7 +84,7 @@ cd SDC-System-Integration
```bash
sudo -H pip install -r requirements-gpu-pygame.txt
```
* Without Pygame
* Without Pygame nor MoviePy
* For non-GPU Linux systems
```bash
pip install -r requirements.txt
Expand Down Expand Up @@ -130,3 +130,13 @@ rosrun tools diagScreen.py
```
![./imgs/sdc-t3-sysint-diag-screen.png](./imgs/sdc-sysint-diagnostics.gif)

10. To view sample Udacity provided rosbags, convert them to MP4 or GIFS use the following:
* __NOTE__: Requires pygame and moviepy!
```
cd ../tools
python view_rosbag_video.py --dataset <rosbags>
python rosbag_video_2_mp4.py --dataset <rosbags> <mp4 file>
python rosbag_video_2_gif.py --dataset <rosbags> <gif file>
```
![./imgs/just_traffic_light.gif](./imgs/just_traffic_light.gif)
![./imgs/loop_with_traffic_light.gif](./imgs/loop_with_traffic_light.gif)
1 change: 1 addition & 0 deletions requirements-gpu-pygame.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ keras>=2.0.0
tensorflow-gpu>=1.0.0
h5py
pygame
moviepy
1 change: 1 addition & 0 deletions requirements-pygame.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ keras>=2.0.0
tensorflow>=1.0.0
h5py
pygame
moviepy
70 changes: 70 additions & 0 deletions tools/rosbag_video_2_gif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/python
"""
view_rosbag_video.py: version 0.1.0
Note:
Part of this code was copied and modified from github.com/comma.ai/research (code: BSD License)
Todo:
Update steering angle projection. Current version is a hack from comma.ai's version
Update enable left, center and right camera selection. Currently all three cameras are displayed.
Update to enable display of trained steering data (green) as compared to actual (blue projection).
History:
2016/10/06: Update to add --skip option to skip the first X seconds of data from rosbag.
2016/10/02: Initial version to display left, center, right cameras and steering angle.
"""

import argparse
import sys
import re
import os
import cv2
import numpy as np
import rosbag
import datetime
import serialrosbags
from cv_bridge import CvBridge
import moviepy.editor as mpy

bridge = CvBridge()
bags = None

def make_frame(t):
global bridge
global bags
while bags.has_data():
try:
topic, msg, t = bags.read_messages()
if topic == '/image_raw':
return cv2.resize(bridge.imgmsg_to_cv2(msg, "rgb8"), (800, 600), interpolation=cv2.INTER_AREA)
except:
pass
return None

# ***** main loop *****
if __name__ == "__main__":
defaultOutput = 'out.gif'
parser = argparse.ArgumentParser(description='Udacity SDC Challenge-2 Video viewer 2')
parser.add_argument('--datasets', type=str, default="dataset1.bag:dataset2.bag:dataset3.bag:dataset4.bag", help='Dataset/ROS Bag name')
parser.add_argument('outfilename', type=str, default=defaultOutput, help='video output file')
args = parser.parse_args()
videopattern = re.compile("^.+\.gif$")

if videopattern.match(args.outfilename):
if os.path.exists(args.outfilename):
print("Video output file: %s exists. %s" % (
args.outfilename, pleaseRemove))
sys.exit(2)
else:
videoout = args.outfilename
valid = True
else:
print(invalidExt % ("video", validVideoExt))
sys.exit(3)

datasets = args.datasets
bags = serialrosbags.Bags(datasets, ['/current_pose','/image_raw'])

clip = mpy.VideoClip(make_frame, duration=14)
clip.write_gif(videoout, fps=24)

70 changes: 70 additions & 0 deletions tools/rosbag_video_2_mp4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/python
"""
view_rosbag_video.py: version 0.1.0
Note:
Part of this code was copied and modified from github.com/comma.ai/research (code: BSD License)
Todo:
Update steering angle projection. Current version is a hack from comma.ai's version
Update enable left, center and right camera selection. Currently all three cameras are displayed.
Update to enable display of trained steering data (green) as compared to actual (blue projection).
History:
2016/10/06: Update to add --skip option to skip the first X seconds of data from rosbag.
2016/10/02: Initial version to display left, center, right cameras and steering angle.
"""

import argparse
import sys
import re
import os
import cv2
import numpy as np
import rosbag
import datetime
import serialrosbags
from cv_bridge import CvBridge
import moviepy.editor as mpy

bridge = CvBridge()
bags = None

def make_frame(t):
global bridge
global bags
while bags.has_data():
try:
topic, msg, t = bags.read_messages()
if topic == '/image_raw':
return cv2.resize(bridge.imgmsg_to_cv2(msg, "rgb8"), (800, 600), interpolation=cv2.INTER_AREA)
except:
pass
return None

# ***** main loop *****
if __name__ == "__main__":
defaultOutput = 'out.mp4'
parser = argparse.ArgumentParser(description='Udacity SDC Challenge-2 Video viewer 2')
parser.add_argument('--datasets', type=str, default="dataset1.bag:dataset2.bag:dataset3.bag:dataset4.bag", help='Dataset/ROS Bag name')
parser.add_argument('outfilename', type=str, default=defaultOutput, help='video output file')
args = parser.parse_args()
videopattern = re.compile("^.+\.mp4$")

if videopattern.match(args.outfilename):
if os.path.exists(args.outfilename):
print("Video output file: %s exists. %s" % (
args.outfilename, pleaseRemove))
sys.exit(2)
else:
videoout = args.outfilename
valid = True
else:
print(invalidExt % ("video", validVideoExt))
sys.exit(3)

datasets = args.datasets
bags = serialrosbags.Bags(datasets, ['/current_pose','/image_raw'])

clip = mpy.VideoClip(make_frame, duration=14)
clip.write_videofile(videoout, fps=24, audio=False)

88 changes: 88 additions & 0 deletions tools/view_rosbag_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/python
"""
view_rosbag_video.py: version 0.1.0
Note:
Part of this code was copied and modified from github.com/comma.ai/research (code: BSD License)
Todo:
Update steering angle projection. Current version is a hack from comma.ai's version
Update enable left, center and right camera selection. Currently all three cameras are displayed.
Update to enable display of trained steering data (green) as compared to actual (blue projection).
History:
2016/10/06: Update to add --skip option to skip the first X seconds of data from rosbag.
2016/10/02: Initial version to display left, center, right cameras and steering angle.
"""

import argparse
import sys
import numpy as np
import pygame
import rosbag
import datetime
import cStringIO
import serialrosbags
from cv_bridge import CvBridge

#from keras.models import model_from_json

pygame.init()
size = None
width = None
height = None
screen = None

# ***** main loop *****
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Udacity SDC Challenge-2 Video viewer 2')
parser.add_argument('--datasets', type=str, default="dataset1.bag:dataset2.bag:dataset3.bag:dataset4.bag", help='Dataset/ROS Bag name')
parser.add_argument('--skip', type=int, default="0", help='skip seconds')
args = parser.parse_args()

datasets = args.datasets
skip = args.skip
startsec = 0
angle_steers = 0.0
blue = (0, 0, 255)
green = (0, 255, 0)

bridge = CvBridge()
bags = serialrosbags.Bags(datasets, ['/current_pose','/image_raw'])

while bags.has_data():
try:
topic, msg, t = bags.read_messages()
if startsec == 0:
startsec = t.to_sec()
if skip < 24*60*60:
skipping = t.to_sec() + skip
print "skipping ", skip, " seconds from ", startsec, " to ", skipping, " ..."
else:
skipping = skip
print "skipping to ", skip, " from ", startsec, " ..."
else:
if t.to_sec() > skipping:
if topic in ['/current_pose', '/image_raw']:
blue = (255, 0, 0)
print(topic, msg.header.seq, t-msg.header.stamp, t)

if topic == '/image_raw':

height = int(msg.height)
width = int(msg.width)
if size is None:
size = (width, height)
pygame.display.set_caption("Udacity SDC ROSBAG: camera video viewer")
screen = pygame.display.set_mode(size, pygame.DOUBLEBUF)
print "size: ", size

img = bridge.imgmsg_to_cv2(msg, "rgb8")
sim_img = pygame.image.fromstring(img.tobytes(), size, 'RGB')

screen.blit(sim_img, (0,0))
pygame.display.flip()

else:
print "skipping ", skip, " seconds from ", t.to_sec(), " to ", skipping, " ..."
except:
pass

0 comments on commit f120ddc

Please sign in to comment.