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

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

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)

相关推荐

  1. Habitat环境学习如何Agent环境随机行走

    2024-01-31 19:56:01       60 阅读
  2. 如何VSCode配置Python环境

    2024-01-31 19:56:01       31 阅读
  3. 【爬虫】Scrapy配置随机User-Agent中间件

    2024-01-31 19:56:01       91 阅读
  4. 如何指定的conda虚拟环境启动Juptyter

    2024-01-31 19:56:01       54 阅读
  5. 【Python】如何Ubuntu环境切换Python版本

    2024-01-31 19:56:01       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-31 19:56:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-31 19:56:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-31 19:56:01       87 阅读
  4. Python语言-面向对象

    2024-01-31 19:56:01       96 阅读

热门阅读

  1. 面试题整理01

    2024-01-31 19:56:01       57 阅读
  2. 苹果Vision Pro小白入门实战项目-适合新手入门

    2024-01-31 19:56:01       61 阅读
  3. Synchronized和volatile的区别

    2024-01-31 19:56:01       57 阅读
  4. Python 截取字符串的方法

    2024-01-31 19:56:01       58 阅读
  5. [linux] which和find有什么区别?

    2024-01-31 19:56:01       57 阅读
  6. Leetcode 2808 . 使循环数组所有元素相等

    2024-01-31 19:56:01       68 阅读
  7. <网络安全>《11 网络安全审计系统》

    2024-01-31 19:56:01       51 阅读
  8. 初识C++中面向对象

    2024-01-31 19:56:01       51 阅读
  9. 网络安全战略中的法律问题

    2024-01-31 19:56:01       49 阅读
  10. 记 2024-01-30 fiber 学习

    2024-01-31 19:56:01       60 阅读