当前位置: 首页 > news >正文

nuScenes数据实战:用Python脚本一键提取Lidar点云和未标注的Sweeps帧(附完整代码)

nuScenes数据高效处理指南Python脚本实现点云与Sweeps帧自动化提取自动驾驶算法开发中数据准备往往占据70%以上的时间成本。本文将分享一套完整的Python解决方案帮助开发者快速从nuScenes数据集中提取LIDAR点云和未标注的sweeps帧数据大幅提升算法原型开发效率。1. 环境配置与数据准备1.1 安装必要的工具链处理nuScenes数据集需要以下核心组件pip install nuscenes-devkit matplotlib numpy open3d推荐使用conda创建独立环境以避免依赖冲突conda create -n nuscenes python3.8 conda activate nuscenes1.2 数据集目录结构规范正确的数据集存放结构对后续处理至关重要nuscenes/ ├── maps/ ├── samples/ ├── sweeps/ └── v1.0-trainval/ ├── attribute.json ├── calibrated_sensor.json ├── category.json └── ...其他元数据文件提示建议使用符号链接将数据目录统一到固定路径便于多项目共享数据集2. 核心数据提取流程2.1 LIDAR点云数据批量提取以下脚本实现了从指定场景中提取所有关键帧的点云数据from nuscenes.nuscenes import NuScenes from nuscenes.utils.data_classes import LidarPointCloud import os import numpy as np def export_lidar_frames(nusc, output_dir, sensorLIDAR_TOP): os.makedirs(output_dir, exist_okTrue) for scene in nusc.scene: sample_token scene[first_sample_token] while sample_token: sample nusc.get(sample, sample_token) lidar_data nusc.get(sample_data, sample[data][sensor]) # 加载并保存点云 pc LidarPointCloud.from_file( os.path.join(nusc.dataroot, lidar_data[filename]) ) points np.transpose(pc.points) output_path os.path.join( output_dir, f{lidar_data[token]}.npy ) np.save(output_path, points) sample_token sample[next]2.2 未标注Sweeps帧智能采集利用令牌链式访问机制获取连续时序数据def export_sweeps_sequence(nusc, output_dir, sensorLIDAR_TOP, num_sweeps5): os.makedirs(output_dir, exist_okTrue) for scene in nusc.scene: sample_token scene[first_sample_token] while sample_token: sample nusc.get(sample, sample_token) lidar_data nusc.get(sample_data, sample[data][sensor]) # 回溯获取历史sweeps current_token lidar_data[prev] for i in range(num_sweeps): if not current_token: break sweep_data nusc.get(sample_data, current_token) if not sweep_data[is_key_frame]: pc LidarPointCloud.from_file( os.path.join(nusc.dataroot, sweep_data[filename]) ) np.save( os.path.join(output_dir, f{sweep_data[token]}.npy), np.transpose(pc.points) ) current_token sweep_data[prev] sample_token sample[next]3. 高级功能实现3.1 数据可视化校验模块为确保数据提取正确性集成Open3D可视化import open3d as o3d def visualize_pointcloud(nusc, sample_token, sensorLIDAR_TOP): sample nusc.get(sample, sample_token) lidar_data nusc.get(sample_data, sample[data][sensor]) pc LidarPointCloud.from_file( os.path.join(nusc.dataroot, lidar_data[filename]) ) points np.transpose(pc.points[:, :3]) # 取XYZ坐标 pcd o3d.geometry.PointCloud() pcd.points o3d.utility.Vector3dVector(points) # 可视化配置 vis o3d.visualization.Visualizer() vis.create_window() vis.add_geometry(pcd) # 设置视角 ctr vis.get_view_control() ctr.set_front([0, 0, -1]) ctr.set_up([0, -1, 0]) vis.run() vis.destroy_window()3.2 元数据关联导出为后续半监督学习准备完整的元信息def export_metadata(nusc, output_file): import json metadata { scenes: [], samples: [], sweeps: [] } for scene in nusc.scene: scene_info { token: scene[token], name: scene[name], sample_count: scene[nbr_samples] } metadata[scenes].append(scene_info) sample_token scene[first_sample_token] while sample_token: sample nusc.get(sample, sample_token) sample_info { token: sample_token, timestamp: sample[timestamp], scene: scene[token], next: sample[next], prev: sample[prev] } metadata[samples].append(sample_info) sample_token sample[next] with open(output_file, w) as f: json.dump(metadata, f, indent2)4. 工程化实践建议4.1 性能优化技巧处理大规模数据集时的关键优化点优化方向实施方法预期效果并行处理使用multiprocessing分场景处理提升3-5倍速度内存管理分块加载数据及时释放资源降低内存占用30%磁盘IO使用SSD存储合并小文件写入减少50%IO时间缓存机制复用已解析的元数据避免重复计算4.2 典型应用场景这套工具链在以下场景中表现优异时序目标检测连续sweeps帧提供运动线索点云配准高频率采集数据提升ICP精度半监督学习利用大量未标注sweeps数据传感器标定多模态数据时间对齐验证# 示例构建时序数据加载器 class TemporalLoader: def __init__(self, nusc, scene_token, num_sweeps5): self.nusc nusc self.scene nusc.get(scene, scene_token) self.num_sweeps num_sweeps def __iter__(self): sample_token self.scene[first_sample_token] while sample_token: sample self.nusc.get(sample, sample_token) lidar_data self.nusc.get(sample_data, sample[data][LIDAR_TOP]) sequence self._get_sweep_sequence(lidar_data) yield sequence sample_token sample[next] def _get_sweep_sequence(self, lidar_data): sequence [] current_token lidar_data[token] for _ in range(self.num_sweeps): if not current_token: break data self.nusc.get(sample_data, current_token) pc LidarPointCloud.from_file( os.path.join(self.nusc.dataroot, data[filename]) ) sequence.append(np.transpose(pc.points)) current_token data[prev] return sequence[::-1] # 按时间顺序返回实际项目中建议将提取的数据转换为更高效的存储格如HDF5特别是当需要处理完整trainval集时。以下是将多个.npy文件合并为HDF5的实用代码片段import h5py def convert_to_hdf5(npy_dir, hdf5_path): files [f for f in os.listdir(npy_dir) if f.endswith(.npy)] with h5py.File(hdf5_path, w) as hf: for i, f in enumerate(files): data np.load(os.path.join(npy_dir, f)) hf.create_dataset( fframe_{i}, datadata, compressiongzip )
http://www.gsyq.cn/news/1363143.html

相关文章:

  • 嵌入式GPU如何实现边缘视觉应用820%性能跃迁:从架构解析到实战优化
  • XRDP远程桌面太卡?手把手教你优化Ubuntu 22.04的传输性能与画质
  • 告别踩坑:手把手教你为openEuler 22.03 LST配置RealVNC 6.11远程桌面(含序列号激活)
  • Bittensor:去中心化AI网络的架构、挑战与激励模型优化
  • 双系统Ubuntu 20.04装完没WiFi?别急着重装,试试这个Realtek网卡驱动手动编译大法
  • C51开发中汇编注释问题的解决方案
  • 汽车电子系统中GIC-600AE与CMN-600AE互连的安全机制解析
  • 从《炉石传说》猜卡组到垃圾邮件过滤:用Python手把手实现贝叶斯更新(附代码)
  • Mali Midgard GPU架构与OpenCL开发优化指南
  • 从‘封建网络’到‘选项框架’:手把手拆解5种主流HRL算法核心思想与PyTorch实现要点
  • openKylin双系统安装保姆级复盘:我踩过的三个坑(分区、引导、驱动)及完美解决方案
  • 别再硬改Seurat对象行名了!从ENSEMBL ID到Gene Symbol的正确转换姿势(附避坑代码)
  • 视觉着陆系统预测不确定性:从亚像素回归到RAIM完整性监测
  • 计算机视觉如何让外骨骼机器人实现预见式步态辅助控制
  • Linux下离线安装Mamba_SSM和Causal-Conv1d避坑指南(附CUDA 11.8 + PyTorch 2.0环境包)
  • 别再手动复制地址了!手把手教你配置Jupyter Notebook自动在Chrome/Edge浏览器打开(附路径查找技巧)
  • 紧急预警:92%的法律团队仍在用基础版Claude处理涉外合同(附GDPR/CCPA双合规审查Checklist)
  • 保姆级教程:用Python复现CDSM融合算法,在NuScenes上跑通3D目标检测
  • 【AI Agent健身行业落地实战指南】:2024年已验证的7大高转化场景与避坑清单
  • Silvaco TCAD 半导体器件仿真全攻略:从入门到精通
  • 基于滑模理论的异步电机控制系统设计与仿真
  • 基于随机森林与混淆矩阵的拉曼光谱香精识别模型
  • 最新版建筑施工安全教育培训(30页)-PPT
  • 在 Oracle EBS R12 / Cloud EBS 里,怎么新建一个利润中心段(用来承接 SAP 利润中心)
  • 中小企业AI落地实战:从能力配置到生态嵌入的五步导航图
  • 旅游客服响应时效提升至8.3秒?揭秘某出境游龙头AI Agent上线72小时后的5项关键调优动作
  • 为什么92%的医学生用错Claude读文献?——神经内科、肿瘤学、循证护理三大领域TOP10错误清单(含修正对照表)
  • Unity 2021.3新手实战:C#脚本+物理系统+UI交互三模块协同开发
  • 量子计算在组合优化与蛋白质折叠中的应用
  • 毫米波通信技术对比:Pinching天线与RIS性能分析