PAYATHON 2026

Isaac Sim 5.1 如何直接获取相机图像而不写入磁盘?

数据阿明

可以。Isaac Sim 5.1 可以通过 Replicator 的 rgb annotator 直接获取相机图像。将 annotator 绑定到相机对应的 render product,完成一次采集后调用 get_data(),就能在内存中处理像素数据。这个流程无需 omni.replicator.core.capture 或挂载 BasicWriter。Isaac Sim 5.1 官方示例

为什么不需要 BasicWriter?

annotator 可以将相机图像提供给 Python 代码,Writer 则负责后续输出。直接读取 annotator 数据,就可以省去保存图片再重新读取的步骤。

对于已有 USD 场景,只需指定现有相机的 prim 路径,无需重新创建相机。

如何读取已有相机的图像?

下面的示例适用于 Isaac Sim 的 Script Editor。先打开 USD 场景,再把 /World/Camera 替换为场景中实际的相机路径。(640, 480) 是示例采集分辨率,可以按需调整。

如果此前已经挂载了 BasicWriter,请先对对应的 writer 实例调用 writer.detach(),否则采集时仍可能触发原来的图像写盘操作。

import asyncio

import omni.usd
import omni.replicator.core as rep
from pxr import UsdGeom


async def capture_camera_image():
    camera_path = "/World/Camera"
    stage = omni.usd.get_context().get_stage()
    if stage is None:
        raise RuntimeError("No USD stage is open")

    camera_prim = stage.GetPrimAtPath(camera_path)
    if not camera_prim.IsValid() or not camera_prim.IsA(UsdGeom.Camera):
        raise ValueError(f"Invalid camera prim: {camera_path}")

    render_product = rep.create.render_product(
        camera_path, resolution=(640, 480)
    )
    rgb_annotator = rep.annotators.get("rgb")
    rgb_annotator.attach(render_product)

    try:
        await rep.orchestrator.step_async()
        rgba = rgb_annotator.get_data()
        if rgba.size == 0:
            raise RuntimeError("Camera image is not available yet")

        rgb = rgba[..., :3].copy()
        print("RGB image:", rgb.shape, rgb.dtype)
        return rgb
    finally:
        rgb_annotator.detach()
        render_product.destroy()


image_task = asyncio.ensure_future(capture_camera_image())

任务成功完成后,可以通过 image_task.result() 获取返回的 RGB 数组。也可以在自己的异步处理流程中直接使用 rgb。

annotator 名称虽然是 rgb,输出的图像却包含 R、G、B、A 四个通道,像素类型为 uint8。示例通过 [..., :3] 取出 RGB,并用 .copy() 保留独立数据,以便释放采集资源后继续使用。Replicator API 文档

读取时需要注意什么?

  • 等待采集完成后再调用 get_data()。刚挂载 annotator 时,数据可能尚未就绪。示例使用 await rep.orchestrator.step_async() 等待一次采集完成。
  • Script Editor 中使用异步方式。在已经启动 SimulationApp 的独立脚本中,可以先调用 rep.orchestrator.step(),再调用 get_data()。两种方式均见 Isaac Sim 5.1 官方示例。
  • 连续采集时,将 render product 和 annotator 的创建放在循环外,每次完成采集后读取数据,结束时再执行 detach() 和 destroy()。如果 render product 与其他组件共用,应在所有使用方结束后再销毁。
  • 空数据和黑色图像需要分开检查。空数据优先检查相机路径、绑定关系和采集是否完成;有像素数据但画面全黑时,再检查相机朝向、场景照明和裁剪范围。

备注:内容仅供参考。