Habitat环境学习三:如何让Agent在环境中随机行走

如何让Agent在环境中随机行走

  • 1、首先配置模拟器

1、首先配置模拟器

定义场景通过test_scene定义;

# @title Configure Sim Settings

# 包含了要探索房屋的网格和语义信息
test_scene = "./data/scene_datasets/mp3d_example/17DRP5sb8fy/17DRP5sb8fy.glb"

# 设置是否需要使用相关的传感器
rgb_sensor = True  # @param {type:"boolean"}
depth_sensor = True  # @param {type:"boolean"}
semantic_sensor = True  # @param {type:"boolean"}

# 定义传感器
sim_settings = {
    "width": 256,  # Spatial resolution of the observations,分辨率
    "height": 256,
    "scene": test_scene,  # Scene path
    "default_agent": 0,
    "sensor_height": 1.5,  # Height of sensors in meters,高度
    "color_sensor": rgb_sensor,  # RGB sensor
    "depth_sensor": depth_sensor,  # Depth sensor
    "semantic_sensor": semantic_sensor,  # Semantic sensor
    "seed": 1,  # used in the random navigation
    "enable_physics": False,  # kinematics only,是否需要启动交互性,对于导航任务不需要,对于重排任务需要
}

进行特定的配置,包括全局模拟器和特定的Agent。

def make_cfg(settings):
    sim_cfg = habitat_sim.SimulatorConfiguration() # 获取一个全局配置的结构体
    sim_cfg.gpu_device_id = 0 # 在0号gpu上进行配置
    sim_cfg.scene_id = settings["scene"] # 场景设置
    sim_cfg.enable_physics = settings["enable_physics"] # 物理功能是否启用,默认情况下为false

    # Note: all sensors must have the same resolution
    sensor_specs = []
	
	# 创建实例然后填充传感器配置参数
    color_sensor_spec = habitat_sim.CameraSensorSpec()
    color_sensor_spec.uuid = "color_sensor" # uuid必须唯一
    color_sensor_spec.sensor_type = habitat_sim.SensorType.COLOR
    color_sensor_spec.resolution = [settings["height"], settings["width"]]
    color_sensor_spec.position = [0.0, settings["sensor_height"], 0.0]
    color_sensor_spec.sensor_subtype = habitat_sim.SensorSubType.PINHOLE
    sensor_specs.append(color_sensor_spec)

    depth_sensor_spec = habitat_sim.CameraSensorSpec()
    depth_sensor_spec.uuid = "depth_sensor"
    depth_sensor_spec.sensor_type = habitat_sim.SensorType.DEPTH
    depth_sensor_spec.resolution = [settings["height"], settings["width"]]
    depth_sensor_spec.position = [0.0, settings["sensor_height"], 0.0]
    depth_sensor_spec.sensor_subtype = habitat_sim.SensorSubType.PINHOLE
    sensor_specs.append(depth_sensor_spec)

    semantic_sensor_spec = habitat_sim.CameraSensorSpec()
    semantic_sensor_spec.uuid = "semantic_sensor"
    semantic_sensor_spec.sensor_type = habitat_sim.SensorType.SEMANTIC
    semantic_sensor_spec.resolution = [settings["height"], settings["width"]]
    semantic_sensor_spec.position = [0.0, settings["sensor_height"], 0.0]
    semantic_sensor_spec.sensor_subtype = habitat_sim.SensorSubType.PINHOLE
    sensor_specs.append(semantic_sensor_spec)

    # Here you can specify the amount of displacement in a forward action and the turn angle
    # 有了传感器后必须将其加到agent上
    agent_cfg = habitat_sim.agent.AgentConfiguration()
    # 给agent配置所有的传感器
    agent_cfg.sensor_specifications = sensor_specs
    # agent可以做到的动作,是一个字典包含前进、左转和右转
    agent_cfg.action_space = {
        "move_forward": habitat_sim.agent.ActionSpec(
            "move_forward", habitat_sim.agent.ActuationSpec(amount=0.25)
        ),
        "turn_left": habitat_sim.agent.ActionSpec(
            "turn_left", habitat_sim.agent.ActuationSpec(amount=30.0)
        ),
        "turn_right": habitat_sim.agent.ActionSpec(
            "turn_right", habitat_sim.agent.ActuationSpec(amount=30.0)
        ),
    }
	# 返回habita的全局配置
    return habitat_sim.Configuration(sim_cfg, [agent_cfg])

对环境进行实例化:

# 运行刚刚定义的函数
cfg = make_cfg(sim_settings)
# Needed to handle out of order cell run in Colab
try:  # Got to make initialization idiot proof
    sim.close()
except NameError:
    pass
sim = habitat_sim.Simulator(cfg)

打印房间和房间中的物体:

def print_scene_recur(scene, limit_output=10):
    # 打印几层楼和房间和房间中的物体
    print(
        f"House has {len(scene.levels)} levels, {len(scene.regions)} regions and {len(scene.objects)} objects"
    )
    # 打印aabb的center和sizes
    print(f"House center:{scene.aabb.center} dims:{scene.aabb.sizes}")

    count = 0
    # 循环遍历这些楼层
    for level in scene.levels:
        # 获取id号、aabb的center和aabb的sizes
        print(
            f"Level id:{level.id}, center:{level.aabb.center},"
            f" dims:{level.aabb.sizes}"
        )
        for region in level.regions:
            # 一个房间,每个房间都有类别名称
            print(
                f"Region id:{region.id}, category:{region.category.name()},"
                f" center:{region.aabb.center}, dims:{region.aabb.sizes}"
            )
            # 房间中的物体和物体的位置
            for obj in region.objects:
                print(
                    f"Object id:{obj.id}, category:{obj.category.name()},"
                    f" center:{obj.aabb.center}, dims:{obj.aabb.sizes}"
                )
                count += 1
                if count >= limit_output:
                    return


# Print semantic annotation information (id, category, bounding box details)
# about levels, regions and objects in a hierarchical fashion
scene = sim.semantic_scene
print_scene_recur(scene)

你可能感兴趣的:(Habitat仿真环境,具身智能,学习,java,前端)