Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions DatasetSave.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, config, scenario_name):
self.DEPTH_PATH = None
self.SEMANTIC_PATH = None

self.NEW_VIEW_PATH = None

self.generate_path(self.config["SAVE_CONFIG"]["ROOT_PATH"], scenario_name)
self.captured_frame_no = self.get_current_files_num()

Expand All @@ -44,7 +46,8 @@ def generate_path(self, root_path, scenario_name):
self.OUTPUT_FOLDER = os.path.join(root_path, PHASE)
folders = ['calib', 'image', 'image_label', 'bbox_img',
'velodyne', 'lidar_label', 'ego_state', 'extrinsic',
'depth', 'semantic']
'depth', 'semantic', 'new_view', 'new_view/label',
'new_view/new_bbox']

for folder in folders:
directory = os.path.join(self.OUTPUT_FOLDER, folder)
Expand All @@ -65,7 +68,8 @@ def generate_path(self, root_path, scenario_name):
self.EXTRINSIC_TXT_PATH = os.path.join(self.OUTPUT_FOLDER, 'extrinsic/{0:06}.txt')
self.DEPTH_PATH = os.path.join(self.OUTPUT_FOLDER, 'depth/{0:06}_depth_{1}.png')
self.SEMANTIC_PATH = os.path.join(self.OUTPUT_FOLDER, 'semantic/{0:06}_semantic_{1}.png')

self.NEW_VIEW_PATH = os.path.join(self.OUTPUT_FOLDER, 'new_view/{0:06}_new_view_{1}.png')
self.NEW_BBOX_PATH = os.path.join(self.OUTPUT_FOLDER, 'new_view/new_bbox/{0:06}_new_bbox_{1}.png')


def get_current_files_num(self):
Expand Down Expand Up @@ -169,6 +173,23 @@ def save_datasets(self, data):
for i in range(5)
]

# 新视角图片路径生成
new_view_files = [
self.NEW_VIEW_PATH.format(self.captured_frame_no, i)
for i in range(5)
]

# 新视角标签路径生成(添加以下代码)
new_view_label_files = [
os.path.join(self.OUTPUT_FOLDER, f'new_view/label/{self.captured_frame_no:06}_new_view_{i}.txt')
for i in range(5)
]

new_bbox_files = [
self.NEW_BBOX_PATH.format(self.captured_frame_no, i)
for i in range(5)
]

# 其他路径保持不变
calib_filename = self.CALIBRATION_PATH.format(self.captured_frame_no)
lidar_label_filename = self.LIDAR_LABEL_PATH.format(self.captured_frame_no)
Expand Down Expand Up @@ -202,13 +223,27 @@ def save_datasets(self, data):
dt["sensor_data"][[8, 9, 10, 15, 16][seg_cam]]
)

# 激光雷达数据保存(保持原始TODO注释)
save_lidar_data(lidar_files[0], dt["sensor_data"][2], extrinsic[2])
save_lidar_data(lidar_files[1], dt["sensor_data"][19], extrinsic[19])
save_lidar_data(lidar_files[2], dt["sensor_data"][20], extrinsic[20])
save_lidar_data(lidar_files[3], dt["sensor_data"][21], extrinsic[21])
save_lidar_data(lidar_files[4], dt["sensor_data"][22], extrinsic[22])
# save_lidar_data(lidar_files[1], dt["sensor_data"][6], extrinsic[6]) # 保持注释状态
# 新视角图像保存
for i, view_idx in enumerate([6, 23, 24, 25, 26]):
save_image_data(new_view_files[i], dt["sensor_data"][view_idx])
# 保存对应标签
save_kitti_label_data(
new_view_label_files[i],
dt[f"image_labels_kitti_{5+i}"] # 假设新增的5个标签存储在5-9索引
)
save_bbox_image_data(
new_bbox_files[i],
dt[f"bbox_img_{5+i}"] # 假设bbox_img_5到9对应新视角
)
# save_bbox_image_data(new_bbox_files[1], dt["bbox_img_5"])
# print(new_bbox_files[0])

# 激光雷达数据保存
sensor_indices = [2, 19, 20, 21, 22]
for i, sensor_idx in enumerate(sensor_indices):
save_lidar_data(lidar_files[i],
dt["sensor_data"][sensor_idx],
extrinsic[sensor_idx])
save_kitti_label_data(lidar_label_filename, dt["pc_labels_kitti"])

# 保存标签和标注数据
Expand All @@ -221,6 +256,9 @@ def save_datasets(self, data):
bbox_img_filenames[cam_idx],
dt[f"bbox_img{'_'+str(cam_idx) if cam_idx>0 else ''}"]
)

# 将新视角的label保存到新视角的文件夹

# EGO状态保存保持不变
save_ego_data(
ego_state_filename,
Expand All @@ -234,7 +272,7 @@ def save_datasets(self, data):
# # 保存深度和语义数据
# for i, depth_idx in enumerate([1, 11, 12]):
# save_depth_image_data(depth_files[i], dt["sensor_data"][depth_idx])
# for i, seg_idx in enumerate([8, 9, 10]):
# for i, seg_idx in enumerate(32, 33, 34, 35, 36]):
# save_semantic_image_data(semantic_files[i], dt["sensor_data"][seg_idx])

self.captured_frame_no += 1
67 changes: 6 additions & 61 deletions SimulationScene.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,78 +41,23 @@ def __init__(self, config):
self.random_seed = 17 # 固定随机种子
random.seed(self.random_seed)
np.random.seed(self.random_seed)
# 新增场景运行器实例
self.scenario_runner = None
self._init_scenario_runner()



def _init_scenario_runner(self):
"""初始化场景运行器参数"""
self.args = argparse.Namespace(
host='127.0.0.1',
port=2000,
timeout=10.0,
trafficManagerPort=8001,
trafficManagerSeed=0,
sync=True,
scenario=self.config.get("SCENARIO", "FollowLeadingVehicleWithObstacle_2"),
openscenario=None,
openscenarioparams=None,
route=None,
agent='autopilot_agent.py',
agentConfig='',
output=True,
file=False,
junit=False,
json=False,
outputDir='',
configFile='',
additionalScenario=None,
debug=False,
reloadWorld=True,
record=None,
randomize=False,
repetitions=1,
waitForEgo=True,
# 以下参数在help中列出但未在代码中使用
version=False,
list=False,
osc=False
)
self.client = carla.Client(self.args.host, self.args.port)

# self.scenario_runner = ScenarioRunner(self.args)
print("Init")

def run_scenario(self):
"""执行场景"""
try:
success = self.scenario_runner.run()
print(f"场景执行 {'成功' if success else '失败'}")

time.sleep(5) # 延长等待时间至5秒
self.world = self.client.get_world() # 刷新world对象

return success
except Exception as e:
logging.error(f"场景执行异常: {str(e)}")
return False
finally:
self.scenario_runner.destroy()

def set_map(self):
"""
设置场景地图
"""
self.world = self.client.get_world()


def set_weather(self):
"""
设置场景天气
"""
pass
weather = carla.WeatherParameters(
cloudiness=10.0,
precipitation=0.0,
sun_altitude_angle=70.0
)
self.world.set_weather(weather)

def set_synchrony(self):
"""
Expand Down
147 changes: 143 additions & 4 deletions configs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ SENSOR_CONFIG:
# location: [0, 0, 1.0]
# rotation: [0, 0, 0]

VIEW_RGB_1:
NEW_RGB_1:
BLUEPRINT: sensor.camera.rgb
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, -3.2, 1.6]
rotation: [0, 30, 0]
location: [1.5, 0, 1.6]
rotation: [0, 0, 0]

VIEW_RGB_BEV:
BLUEPRINT: sensor.camera.rgb
Expand Down Expand Up @@ -162,6 +162,145 @@ SENSOR_CONFIG:
BLUEPRINT: sensor.lidar.ray_cast
ATTRIBUTE: {range: 70, rotation_frequency: 100, upper_fov: 20, lower_fov: -10, points_per_second: 20480000, channels: 128}

NEW_RGB_2:
BLUEPRINT: sensor.camera.rgb
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, 0, 0]

NEW_RGB_3:
BLUEPRINT: sensor.camera.rgb
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, 15, 0]

NEW_RGB_4:
BLUEPRINT: sensor.camera.rgb
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, -3.2, 1.6]
rotation: [0, 0, 0]

NEW_RGB_5:
BLUEPRINT: sensor.camera.rgb
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, -15, 0]

NEW_DEPTH_1:
BLUEPRINT: sensor.camera.depth
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.5, 0, 1.6]
rotation: [0, 0, 0]

NEW_DEPTH_2:
BLUEPRINT: sensor.camera.depth
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, 0, 0]

NEW_DEPTH_3:
BLUEPRINT: sensor.camera.depth
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, 15, 0]

NEW_DEPTH_4:
BLUEPRINT: sensor.camera.depth
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, -3.2, 1.6]
rotation: [0, 0, 0]

NEW_DEPTH_5:
BLUEPRINT: sensor.camera.depth
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, -15, 0]

NEW_SEM_1:
BLUEPRINT: sensor.camera.semantic_segmentation
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.5, 0, 1.6]
rotation: [0, 0, 0]

NEW_SEM_2:
BLUEPRINT: sensor.camera.semantic_segmentation
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, 0, 0]

NEW_SEM_3:
BLUEPRINT: sensor.camera.semantic_segmentation
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, 15, 0]

NEW_SEM_4:
BLUEPRINT: sensor.camera.semantic_segmentation
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, -3.2, 1.6]
rotation: [0, 0, 0]

NEW_SEM_5:
BLUEPRINT: sensor.camera.semantic_segmentation
ATTRIBUTE:
image_size_x: 960
image_size_y: 640
fov: 90
TRANSFORM:
location: [1.0, 3.2, 1.6]
rotation: [0, -15, 0]

FILTER_CONFIG:
PRELIMINARY_FILTER_DISTANCE: 50
Expand All @@ -172,5 +311,5 @@ FILTER_CONFIG:

SAVE_CONFIG:
ROOT_PATH: data
STEP: 3
STEP: 10
MAX_RECORD_COUNT: 200
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def main():

# 新增初始化等待阶段
print("初始化完成,开始预运行...")
INIT_WAIT_FRAMES = 10 # 等待100帧(约5秒,假设20FPS)
INIT_WAIT_FRAMES = 30 # 等待100帧(约5秒,假设20FPS)
for _ in range(INIT_WAIT_FRAMES):
scene.update_spectator() # 保持视角更新
scene.world.tick()
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

SCENARIO_NAME="DynamicObjectCrossing_4"
SCENARIO_NAME="FollowLeadingVehicleWithObstacle_1"
# MAIN_PY_ARGS="--no-save"
MAIN_PY_ARGS=""

Expand Down
Loading