nutonomy / nuscenes-devkit

The devkit of the nuScenes dataset.
https://www.nuScenes.org
Other
2.25k stars 624 forks source link

render_sample_data #955

Closed iakRulan closed 1 year ago

iakRulan commented 1 year ago

Hello author, if I want to visualize only 3DBOX like pedestrians on the image, how can I achieve it?

whyekit-motional commented 1 year ago

@kkrl1111 you could try something like this:

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from matplotlib.axes import Axes

from nuscenes.nuscenes import NuScenes
from nuscenes.utils.geometry_utils import BoxVisibility

def render_camera_data(
    nusc: NuScenes, sample_data_token: str, category: str, ax: Axes = None, out_path: str = None
) -> None:
    sd_record = nusc.get('sample_data', sample_data_token)
    assert 'CAM' in sd_record['channel']

    data_path, boxes, camera_intrinsic = nusc.get_sample_data(
        sample_data_token, box_vis_level=BoxVisibility.ANY
    )
    data = Image.open(data_path)

    # Init axes.
    if ax is None:
        _, ax = plt.subplots(1, 1, figsize=(9, 16))

    # Show image.
    ax.imshow(data)

    # Show boxes.
    for box in boxes:
        if category in box.name:
            c = np.array(nusc.explorer.get_color(box.name)) / 255.0
            box.render(ax, view=camera_intrinsic, normalize=True, colors=(c, c, c))

    # Limit visible range.
    ax.set_xlim(0, data.size[0])
    ax.set_ylim(data.size[1], 0)

    ax.axis('off')
    ax.set_title(f"{sd_record['channel']}")
    ax.set_aspect('equal')

    if out_path is not None:
        plt.savefig(out_path, bbox_inches='tight', pad_inches=0, dpi=200)

    plt.show()

sensor = 'CAM_FRONT'

nusc_ = NuScenes(version='v1.0-mini', dataroot='/data/sets/nuscenes', verbose=False)

my_sample = nusc_.sample[0]
cam_front_data = nusc_.get('sample_data', my_sample['data'][sensor])
render_camera_data(nusc_, cam_front_data['token'], category='human')

The above code snippet will produce: image