Are there any out-of-box solution to create video

Hi all,

After using visualize_solution, are there some out-of-box solution to draw GiFs or generate a video?

Thanks a lot!

I’m not sure which function visualize_solution you’re referring to, but you can visualize Solution objects in commonroad-io using the function solution.create_dynamic_obstacle() to create an obstacle and create a video from that obstacle or almost any other commonroad-io object. The documentation on creating videos can be found here:

rnd = MPRenderer()
obstacle = solution.create_dynamic_obstacle()
rnd.create_video([obstacle], "/somepath.mp4")
1 Like

Thank you for your quick answer. I tried this function, but why does my code get error?

ego_vehicle = DynamicObstacle(obstacle_id=100, obstacle_type=ego_vehicle_type,
                              obstacle_shape=ego_vehicle_shape, initial_state=trajectory.state_list[0],
                              prediction=ego_vehicle_prediction)
rnd = MPRenderer()
obj_list = [ego_vehicle]
rnd.create_video(obj_list, video_path)

ValueError: Invalid file object: <_io.BufferedReader name=7>

Probably the video_path is not valid or you need to install ffmpeg on your system https://stackoverflow.com/questions/53198609/trying-to-save-an-animated-of-graph-with-matplotlib-in-python-invalid-file-ob/54263809.

HI,

I used exact same way as you suggested, and I met an error.

# create PlanningProblemSolution object
kwarg = {'planning_problem_id': planning_problem.planning_problem_id,
         'vehicle_model': VehicleModel.KS,  # used vehicle model, change if needed
         'vehicle_type': VehicleType.FORD_ESCORT,  # used vehicle type, change if needed
         'cost_function': CostFunction.TR1,  # cost function, DO NOT use JB1
         'trajectory': trajectory_solution}

planning_problem_solution = PlanningProblemSolution(**kwarg)

# create Solution object
kwarg = {'scenario_id': scenario.scenario_id,
         'planning_problem_solutions': [planning_problem_solution]}

solution = Solution(**kwarg)
rnd = MPRenderer()
obstacle = solution.create_dynamic_obstacle()
rnd.create_video([obstacle], path_video_solution)

Error:

AttributeError: ‘dict’ object has no attribute ‘draw’

Could you give some suggestions?

Hi, I have double checked, the latest error is the same.

ffmpeg is already the newest version (7:3.4.8-0ubuntu0.2).
The video path looks okay.

Error message:

Error
Traceback (most recent call last):
File “~/PycharmProjects/motion_planning_common_road/RSMP/test/test_spatiotemporal_lattice_planner.py”, line 139, in test_execute_search_one_scenario_straight_road
rnd.create_video([obstacle], ‘test.mp4’)
File “~//anaconda3/envs/commonroad-py36/lib/python3.6/site-packages/commonroad/visualization/mp_renderer.py”, line 385, in create_video
extra_args=["-g", “1”, “-keyint_min”, str(interval_seconds)])
File “~//.local/lib/python3.6/site-packages/matplotlib/animation.py”, line 1169, in save
anim._init_draw()
File “~//.local/lib/python3.6/site-packages/matplotlib/animation.py”, line 1718, in _init_draw
self._drawn_artists = self._init_func()
File “~//anaconda3/envs/commonroad-py36/lib/python3.6/site-packages/commonroad/visualization/mp_renderer.py”, line 331, in init_frame
self.draw_list(obj_lists, draw_params=draw_params)
File “~//anaconda3/envs/commonroad-py36/lib/python3.6/site-packages/commonroad/visualization/mp_renderer.py”, line 169, in draw_list
elem.draw(self, draw_params, call_stack)
AttributeError: ‘dict’ object has no attribute ‘draw’

Sorry, solution.create_dynamic_obstacle() actually creates a dictionary with the planning problem ID as the key, so this needs to be converted to a list first

rnd.create_video(list(obstacle.values()), path_video_solution)

It is interesting that I still get an error.

ValueError: Invalid file object: <_io.BufferedReader name=9>

Are there any working examples for it?

Thanks a lot!

This error seems to be related to a problem with ffmpeg in your local environment as described in the link I sent previously.