개발일지

6/30 - (5/10000)

wandering developer 2024. 6. 30. 22:06

 

nuscene 이번에는 radar 데이터를 그려봤다.

 

import os
from pyquaternion import Quaternion
import numpy as np
from nuscenes.utils.data_classes import RadarPointCloud
from nuscenes.nuscenes import NuScenes
import matplotlib.pyplot as plt

# Initialize NuScenes
nusc = NuScenes(version='v1.0-mini', dataroot='/data/sets/nuscenes', verbose=True)

x_min_value = 1e+4
x_max_value = -1e+4

y_min_value = 1e+4
y_max_value = -1e+4


def load_and_transform_radar_pointcloud(nusc, radar_token):
    pointsensor = nusc.get('sample_data', radar_token)
    pc = RadarPointCloud.from_file(os.path.join(nusc.dataroot, pointsensor['filename']))
    
    cs_record = nusc.get('calibrated_sensor', pointsensor['calibrated_sensor_token'])
    pc.rotate(Quaternion(cs_record['rotation']).rotation_matrix)
    pc.translate(np.array(cs_record['translation']))

    poserecord = nusc.get('ego_pose', pointsensor['ego_pose_token'])
    pc.rotate(Quaternion(poserecord['rotation']).rotation_matrix)
    pc.translate(np.array(poserecord['translation']))
    
    return pc

def plot_radar_data(nusc, sample_token, ax):
    sample_record = nusc.get('sample', sample_token)

    # Radar tokens
    radar_tokens = {
        'front': sample_record['data']['RADAR_FRONT'],
        'front_left': sample_record['data']['RADAR_FRONT_LEFT'],
        'front_right': sample_record['data']['RADAR_FRONT_RIGHT'],
        'back_left': sample_record['data']['RADAR_BACK_LEFT'],
        'back_right': sample_record['data']['RADAR_BACK_RIGHT']
    }

    # Load and transform point clouds
    pcs = {key: load_and_transform_radar_pointcloud(nusc, token) for key, token in radar_tokens.items()}

    # Clear previous plot
    # ax.clear()

    # Plot the point clouds
    for pc in pcs.values():
        ax.plot(pc.points[0], pc.points[1], '.')
        
    global x_min_value, x_max_value,y_min_value,y_max_value
    # Set axis limits
    if np.min(pc.points[0]) < x_min_value:
        x_min_value = np.min(pc.points[0])
        
    if np.max(pc.points[0]) > x_max_value:
        x_max_value = np.max(pc.points[0])
    
    if np.min(pc.points[1]) < y_min_value:
        y_min_value = np.min(pc.points[1])
        
    if np.max(pc.points[1]) > y_max_value:
        y_max_value = np.max(pc.points[1])
    
    ax.set_xlim(x_min_value,x_max_value )
    ax.set_ylim(y_min_value,y_max_value)
            
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_title('Radar Point Clouds')
    ax.grid(True)

# Set up the plot
fig, ax = plt.subplots(figsize=(10, 10))

# Iterate through all samples in the scene
scene = nusc.scene[1]  # Adjust the scene index as needed
current_sample_token = scene['first_sample_token']

while current_sample_token:
    plot_radar_data(nusc, current_sample_token, ax)
    plt.pause(1)  # Pause to update the plot
    current_sample = nusc.get('sample', current_sample_token)
    current_sample_token = current_sample['next']  # Move to the next sample

plt.show()

 

 

 

global 로 좌표 변환을 하면 아래 처럼 지도가 그려진다.

 

아래 처럼 그림이 그려지는것을 볼 수 있을 것이다.

 

위 코드를 이해하려면 token 이라는 개념을 이해해야 한다.

 

위 코드 작성하는데 많은 시간이 소모되었다.                        

 

해당 코드 참조함.

 

https://github.com/nutonomy/nuscenes-devkit/blob/5325d1b400950f777cd701bdd5e30a9d57d2eaa8/python-sdk/nuscenes/nuscenes.py#L784

 

nuscenes-devkit/python-sdk/nuscenes/nuscenes.py at 5325d1b400950f777cd701bdd5e30a9d57d2eaa8 · nutonomy/nuscenes-devkit

The devkit of the nuScenes dataset. Contribute to nutonomy/nuscenes-devkit development by creating an account on GitHub.

github.com

 

 Scene - 0 Scene - 1

 

 

Nuscene 의 데이터 구조.

 

Radar 를 그리면서 보정도해야해서 

반응형