Android SDK (Kotlin/자바)의 사용자 환경 이해

앱에서 Scene Semantics API를 사용하는 방법을 알아보세요.

Scene Semantics API를 사용하면 ML 모델 기반의 실시간 시맨틱 정보를 제공하여 개발자가 사용자 주변의 장면을 파악할 수 있습니다. 야외 장면의 이미지가 주어지면 API는 하늘, 건물, 나무, 도로, 보도, 차량, 사람 등 유용한 시맨틱 클래스 집합에서 각 픽셀의 라벨을 반환합니다. 픽셀 라벨 외에도 Scene Semantics API는 각 픽셀 라벨의 신뢰도 값과 야외 장면에서 특정 라벨의 보급률을 쿼리하는 간편한 방법을 제공합니다.

왼쪽부터 입력 이미지의 예, 픽셀 라벨의 시맨틱 이미지, 상응하는 신뢰도 이미지의 예가 나와 있습니다.

입력 이미지, 시맨틱 이미지, 시맨틱 신뢰도 이미지의 예

기본 요건

계속 진행하기 전에 기본 AR 개념ARCore 세션 구성 방법을 이해해야 합니다.

장면 시맨틱 사용 설정

새 ARCore 세션에서 사용자의 기기가 Scene Semantics API를 지원하는지 확인합니다. 처리 성능 제약으로 인해 일부 ARCore 호환 기기는 Scene Semantics API를 지원하지 않습니다.

리소스를 절약하기 위해 ARCore에서는 Scene Semantics가 기본적으로 사용 중지됩니다. 앱에서 Scene Semantics API를 사용하려면 시맨틱 모드를 사용 설정하세요.

Java

Config config = session.getConfig();

// Check whether the user's device supports the Scene Semantics API.
boolean isSceneSemanticsSupported =
    session.isSemanticModeSupported(Config.SemanticMode.ENABLED);
if (isSceneSemanticsSupported) {
  config.setSemanticMode(Config.SemanticMode.ENABLED);
}
session.configure(config);

Kotlin

val config = session.config

// Check whether the user's device supports the Scene Semantics API.
val isSceneSemanticsSupported = session.isSemanticModeSupported(Config.SemanticMode.ENABLED)
if (isSceneSemanticsSupported) {
  config.semanticMode = Config.SemanticMode.ENABLED
}
session.configure(config)

시맨틱 이미지 가져오기

Scene Semantics가 사용 설정되면 시맨틱 이미지를 가져올 수 있습니다. 시맨틱 이미지는 ImageFormat.Y8 이미지이며, 여기서 각 픽셀은 SemanticLabel로 정의된 시맨틱 라벨에 해당합니다.

Frame.acquireSemanticImage()를 사용하여 시맨틱 이미지를 가져옵니다.

Java

// Retrieve the semantic image for the current frame, if available.
try (Image semanticImage = frame.acquireSemanticImage()) {
  // Use the semantic image here.
} catch (NotYetAvailableException e) {
  // No semantic image retrieved for this frame.
  // The output image may be missing for the first couple frames before the model has had a
  // chance to run yet.
}

Kotlin

// Retrieve the semantic image for the current frame, if available.
try {
  frame.acquireSemanticImage().use { semanticImage ->
    // Use the semantic image here.
  }
} catch (e: NotYetAvailableException) {
  // No semantic image retrieved for this frame.
}

출력 시맨틱 이미지는 기기에 따라 세션 시작 후 약 1~3프레임 후에 제공되어야 합니다.

신뢰도 이미지 가져오기

각 픽셀에 대한 라벨을 제공하는 시맨틱 이미지 외에도 API는 해당 픽셀 신뢰도 값의 신뢰도 이미지도 제공합니다. 신뢰도 이미지는 ImageFormat.Y8 이미지로, 각 픽셀은 각 픽셀의 시맨틱 라벨과 연결된 확률에 해당하는 [0, 255] 범위의 값에 해당합니다.

Frame.acquireSemanticConfidenceImage()를 사용하여 시맨틱 신뢰도 이미지를 가져옵니다.

Java

// Retrieve the semantic confidence image for the current frame, if available.
try (Image semanticImage = frame.acquireSemanticConfidenceImage()) {
  // Use the semantic confidence image here.
} catch (NotYetAvailableException e) {
  // No semantic confidence image retrieved for this frame.
  // The output image may be missing for the first couple frames before the model has had a
  // chance to run yet.
}

Kotlin

// Retrieve the semantic confidence image for the current frame, if available.
try {
  frame.acquireSemanticConfidenceImage().use { semanticConfidenceImage ->
    // Use the semantic confidence image here.
  }
} catch (e: NotYetAvailableException) {
  // No semantic confidence image retrieved for this frame.
}

출력 신뢰도 이미지는 기기에 따라 세션 시작 후 약 1~3프레임 후에 제공되어야 합니다.

시맨틱 라벨의 픽셀 비율 쿼리

또한 sky와 같은 특정 클래스에 속하는 현재 프레임의 픽셀 비율을 쿼리할 수 있습니다. 이 쿼리는 시맨틱 이미지를 반환하고 특정 라벨에 대해 픽셀별 검색을 수행하는 것보다 더 효율적입니다. 반환된 분수가 [0.0, 1.0] 범위의 부동 소수점 값입니다.

Frame.getSemanticLabelFraction()를 사용하여 특정 라벨의 분수를 가져옵니다.

Java

// Retrieve the fraction of pixels for the semantic label sky in the current frame.
try {
  float outFraction = frame.getSemanticLabelFraction(SemanticLabel.SKY);
  // Use the semantic label fraction here.
} catch (NotYetAvailableException e) {
  // No fraction of semantic labels was retrieved for this frame.
}

Kotlin

// Retrieve the fraction of pixels for the semantic label sky in the current frame.
try {
  val fraction = frame.getSemanticLabelFraction(SemanticLabel.SKY)
  // Use the semantic label fraction here.
} catch (e: NotYetAvailableException) {
  // No fraction of semantic labels was retrieved for this frame.
}