将 ARCore 用作机器学习模型的输入

您可以使用 ARCore 在机器学习流水线中捕获的摄像头画面来打造智能增强现实体验。 ARCore 机器学习套件示例演示了如何使用机器学习套件Google Cloud Vision API 识别现实世界的对象。 该示例使用机器学习模型对相机视图中的对象进行分类,并为虚拟场景中的对象附加标签。

ARCore 机器学习套件示例是用 Kotlin 编写的。它还以 ml_kotlin 示例应用的形式在 ARCore SDK GitHub 代码库中提供。

使用 ARCore 的 CPU 图像

默认情况下,ARCore 会捕获至少两组图像流:

  • 用于特征识别和图像处理的 CPU 图片流。默认情况下,CPU 图片的分辨率为 VGA (640x480)。如果需要,可以将 ARCore 配置为使用其他更高分辨率的图像流。
  • GPU 纹理流,其中包含高分辨率纹理,分辨率通常为 1080p。这通常用作朝向用户的相机预览。它存储在由 Session.setCameraTextureName() 指定的 OpenGL 纹理中。
  • SharedCamera.setAppSurfaces() 指定的任何其他视频流。

CPU 映像大小注意事项

如果使用默认的 VGA 大小的 CPU 流,则不会产生额外费用,因为 ARCore 使用该流来理解现实世界。由于需要捕获额外的视频流,因此请求使用其他分辨率的视频流可能成本高昂。请注意,模型的分辨率可能很快就会变高:如果图片的宽度和高度翻倍,图片中的像素数也会翻一番。

如果您的模型在分辨率较低的图片上仍然表现良好,那么缩小图片可能是有益的。

配置其他高分辨率 CPU 图片流

机器学习模型的性能可能取决于用作输入的图片的分辨率。您可以使用 Session.setCameraConfig() 更改当前的 CameraConfig,并从 Session.getSupportedCameraConfigs() 中选择有效配置,以调整这些视频流的分辨率。

Java

CameraConfigFilter cameraConfigFilter =
    new CameraConfigFilter(session)
        // World-facing cameras only.
        .setFacingDirection(CameraConfig.FacingDirection.BACK);
List<CameraConfig> supportedCameraConfigs =
    session.getSupportedCameraConfigs(cameraConfigFilter);

// Select an acceptable configuration from supportedCameraConfigs.
CameraConfig cameraConfig = selectCameraConfig(supportedCameraConfigs);
session.setCameraConfig(cameraConfig);

Kotlin

val cameraConfigFilter =
  CameraConfigFilter(session)
    // World-facing cameras only.
    .setFacingDirection(CameraConfig.FacingDirection.BACK)
val supportedCameraConfigs = session.getSupportedCameraConfigs(cameraConfigFilter)

// Select an acceptable configuration from supportedCameraConfigs.
val cameraConfig = selectCameraConfig(supportedCameraConfigs)
session.setCameraConfig(cameraConfig)

检索 CPU 映像

使用 Frame.acquireCameraImage() 检索 CPU 映像。不再需要这些图片后,应立即对其进行处置。

Java

Image cameraImage = null;
try {
  cameraImage = frame.acquireCameraImage();
  // Process `cameraImage` using your ML inference model.
} catch (NotYetAvailableException e) {
  // NotYetAvailableException is an exception that can be expected when the camera is not ready
  // yet. The image may become available on a next frame.
} catch (RuntimeException e) {
  // A different exception occurred, e.g. DeadlineExceededException, ResourceExhaustedException.
  // Handle this error appropriately.
  handleAcquireCameraImageFailure(e);
} finally {
  if (cameraImage != null) {
    cameraImage.close();
  }
}

Kotlin

// NotYetAvailableException is an exception that can be expected when the camera is not ready yet.
// Map it to `null` instead, but continue to propagate other errors.
fun Frame.tryAcquireCameraImage() =
  try {
    acquireCameraImage()
  } catch (e: NotYetAvailableException) {
    null
  } catch (e: RuntimeException) {
    // A different exception occurred, e.g. DeadlineExceededException, ResourceExhaustedException.
    // Handle this error appropriately.
    handleAcquireCameraImageFailure(e)
  }

// The `use` block ensures the camera image is disposed of after use.
frame.tryAcquireCameraImage()?.use { image ->
  // Process `image` using your ML inference model.
}

处理 CPU 映像

为了处理 CPU 映像,可以使用各种机器学习库。

在 AR 场景中显示结果

图像识别模型通常通过指示中心点或表示检测到的对象的边界多边形来输出检测到的对象。

使用模型输出的边界框的中心点或中心,可以将锚点附加到检测到的对象。使用 Frame.hitTest() 估计对象在虚拟场景中的姿势。

IMAGE_PIXELS 坐标转换为 VIEW 坐标:

Java

// Suppose `mlResult` contains an (x, y) of a given point on the CPU image.
float[] cpuCoordinates = new float[] {mlResult.getX(), mlResult.getY()};
float[] viewCoordinates = new float[2];
frame.transformCoordinates2d(
    Coordinates2d.IMAGE_PIXELS, cpuCoordinates, Coordinates2d.VIEW, viewCoordinates);
// `viewCoordinates` now contains coordinates suitable for hit testing.

Kotlin

// Suppose `mlResult` contains an (x, y) of a given point on the CPU image.
val cpuCoordinates = floatArrayOf(mlResult.x, mlResult.y)
val viewCoordinates = FloatArray(2)
frame.transformCoordinates2d(
  Coordinates2d.IMAGE_PIXELS,
  cpuCoordinates,
  Coordinates2d.VIEW,
  viewCoordinates
)
// `viewCoordinates` now contains coordinates suitable for hit testing.

使用以下 VIEW 坐标执行点击测试,并根据结果创建锚点:

Java

List<HitResult> hits = frame.hitTest(viewCoordinates[0], viewCoordinates[1]);
HitResult depthPointResult = null;
for (HitResult hit : hits) {
  if (hit.getTrackable() instanceof DepthPoint) {
    depthPointResult = hit;
    break;
  }
}
if (depthPointResult != null) {
  Anchor anchor = depthPointResult.getTrackable().createAnchor(depthPointResult.getHitPose());
  // This anchor will be attached to the scene with stable tracking.
  // It can be used as a position for a virtual object, with a rotation prependicular to the
  // estimated surface normal.
}

Kotlin

val hits = frame.hitTest(viewCoordinates[0], viewCoordinates[1])
val depthPointResult = hits.filter { it.trackable is DepthPoint }.firstOrNull()
if (depthPointResult != null) {
  val anchor = depthPointResult.trackable.createAnchor(depthPointResult.hitPose)
  // This anchor will be attached to the scene with stable tracking.
  // It can be used as a position for a virtual object, with a rotation prependicular to the
  // estimated surface normal.
}

性能考虑因素

请遵循以下建议来节省处理能力并降低能耗:

  • 不要对每个传入帧运行机器学习模型。请考虑改为以较低的帧速率运行对象检测。
  • 考虑使用在线机器学习推断模型来降低计算复杂性。

后续步骤