独自のアプリで Scene Semantics API を使用する方法を確認する。
Scene Semantics API は、ML モデルベースのリアルタイムのセマンティック情報を提供することで、開発者がユーザーの周囲の状況を把握できるようにします。屋外の画像を指定すると、API は空、建物、木、道路、歩道、車両、人など、一連の有用なセマンティック クラスにわたる各ピクセルのラベルを返します。ピクセルラベルに加えて、Scene Semantics API は、各ピクセルラベルの信頼値と、屋外シーンにおける特定のラベルの占有率を照会する簡単な方法も提供します。
<ph type="x-smartling-placeholder">
左から順に、入力画像、ピクセルラベルのセマンティック画像、対応する信頼度画像の例を示しています。
前提条件
AR の基礎的なコンセプトを理解しておいてください。 と ARCore セッションを構成する方法を確認してください。
シーンのセマンティクスを有効にする
新しい ARCore セッションで、ユーザーのデバイスが Scene Semantics API をサポートしているかどうかを確認します。処理能力に制約があるため、すべての ARCore 対応デバイスが Scene Semantics API をサポートしているわけではありません。
リソースを節約するため、ARCore の Scene Semantics はデフォルトで無効になっています。アプリで Scene Semantics API を使用するには、セマンティック モードを有効にします。
// Check whether the user's device supports the Scene Semantics API. int32_t is_scene_semantics_supported = 0; ArSession_isSemanticModeSupported(ar_session, AR_SEMANTIC_MODE_ENABLED, &is_scene_semantics_supported); // Configure the session for AR_SEMANTIC_MODEL_ENABLED. ArConfig* ar_config = NULL; ArConfig_create(ar_session, &ar_config); if (is_scene_semantics_supported) { ArConfig_setSemanticMode(ar_session, ar_config, AR_SEMANTIC_MODE_ENABLED); } CHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS); ArConfig_destroy(ar_config);
セマンティック画像を取得する
シーンのセマンティクスを有効にすると、セマンティック画像を取得できます。セマンティック画像は AR_IMAGE_FORMAT_Y8
画像で、各ピクセルは ArSemanticLabel
で定義されたセマンティック ラベルに対応しています。
ArFrame_acquireSemanticImage()
を使用してセマンティック画像を取得します。
// Retrieve the semantic image for the current frame, if available. ArImage* semantic_image = NULL; if (ArFrame_acquireSemanticImage(ar_session, ar_frame, &semantic_image) != AR_SUCCESS) { // 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. return; } // If a semantic image is available, use it here.
デバイスに応じて、セッションの開始から約 1 ~ 3 フレーム後に出力セマンティック画像を利用できるようになる必要があります。
信頼度の画像を取得する
各ピクセルのラベルを提供するセマンティック画像に加えて、API は対応するピクセル信頼値の信頼画像も提供します。信頼度の画像は AR_IMAGE_FORMAT_Y8
画像です。ここで、各ピクセルは [0, 255]
の範囲の値に対応し、各ピクセルのセマンティック ラベルに関連付けられた確率に対応します。
ArFrame_acquireSemanticConfidenceImage()
を使用して、セマンティック信頼度の画像を取得します。
// Retrieve the semantic confidence image for the current frame, if available. ArImage* semantic_confidence_image = NULL; if (ArFrame_acquireSemanticConfidenceImage(ar_session, ar_frame, &semantic_confidence_image) != AR_SUCCESS) { // 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. return; } // If a semantic confidence image is available, use it here.
出力信頼度の画像は、デバイスに応じて、セッションの開始から約 1 ~ 3 フレーム後に利用可能になります。
セマンティック ラベルのピクセル数をクエリする
また、現在のフレーム内で Sky などの特定のクラスに属するピクセルの割合をクエリすることもできます。このクエリは、セマンティック画像を返して特定のラベルをピクセル単位で検索するよりも効率的です。返される小数は、範囲 [0.0, 1.0]
内の浮動小数点値です。
ArFrame_getSemanticLabelFraction()
を使用して、特定のラベルに対する割合を取得します。
// Retrieve the fraction of pixels for the semantic label sky in the current frame. float out_fraction = 0.0f; if (ArFrame_getSemanticLabelFraction(ar_session, ar_frame, AR_SEMANTIC_LABEL_SKY, &out_fraction) != AR_SUCCESS) { // No fraction of semantic labels was retrieved for this frame. }