ご自身のアプリで 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 フレーム後に利用可能になります(デバイスによって異なります)。
セマンティック ラベルのピクセル数をクエリする
また、現在のフレーム内の、空などの特定のクラスに属するピクセルの割合をクエリすることもできます。このクエリは、セマンティック画像を返して特定のラベルをピクセル単位で検索するよりも効率的です。返される分数は、[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. }