独自のアプリで Scene Semantics API を使用する方法を学びます。
Scene Semantics API を使用すると、ML モデルベースのリアルタイムのセマンティック情報を提供することで、デベロッパーはユーザーの周囲のシーンを理解できます。屋外シーンの画像を指定すると、API は、空、建物、木、道路、歩道、車両、人物など、有用なセマンティック クラスのセットで各ピクセルのラベルを返します。Scene Semantics API は、ピクセルラベルに加えて、各ピクセルラベルの信頼度値も提供します。また、屋外シーンで特定のラベルがどの程度存在するかを簡単にクエリすることもできます。
左から右に、入力画像の例、ピクセルラベルのセマンティック画像、対応する信頼度画像を示します。
前提条件
続行する前に、基本的な AR コンセプトと ARCore セッションを構成する方法を理解してください。
Scene Semantics を有効にする
新しい ARCore セッションで、ユーザーのデバイスが Scene Semantics API をサポートしているかどうかを確認します。処理能力の制約により、ARCore 対応デバイスのすべてが Scene Semantics API をサポートしているわけではありません。
リソースを節約するため、ARCore では Scene Semantics はデフォルトで無効になっています。セマンティック モードを有効にして、アプリで Scene Semantics API を使用できるようにします。
GARSessionConfiguration *configuration = [[GARSessionConfiguration alloc] init];
if ([self.garSession isSemanticModeSupported:GARSemanticModeEnabled]) {
configuration.semanticMode = GARSemanticModeEnabled;
}
NSError *error;
[self.garSession setConfiguration:configuration error:&error];
セマンティック画像を取得する
シーン セマンティクスが有効になると、セマンティック画像を取得できます。セマンティック画像は kCVPixelFormatType_OneComponent8
画像であり、各ピクセルは GARSemanticLabel
で定義されたセマンティック ラベルに対応しています。
GARFrame.semanticImage
を使用してセマンティック画像を取得します。
CVPixelBuffer semanticImage = garFrame.semanticImage;
if (semanticImage) {
// Use the semantic image here
} else {
// Semantic images are not available.
// The output image may be missing for the first couple frames before the model has had a
// chance to run yet.
}
出力セマンティック画像は、デバイスに応じて、セッション開始から約 1 ~ 3 フレーム後に利用できるようになります。
信頼性の画像を取得する
API は、ピクセルごとにラベルを提供するセマンティック画像に加えて、対応するピクセル信頼度の信頼度画像も提供します。信頼度の画像は kCVPixelFormatType_OneComponent8
画像です。各ピクセルは [0, 255]
の範囲内の値に対応し、各ピクセルのセマンティック ラベルに関連付けられた確率に対応します。
GARFrame.semanticConfidenceImage
を使用してセマンティック信頼度の画像を取得します。
CVPixelBuffer confidenceImage = garFrame.semanticConfidenceImage;
if (confidenceImage) {
// Use the semantic image here
} else {
// Semantic images are not available.
// The output image may be missing for the first couple frames before the model has had a
// chance to run yet.
}
出力信頼性画像は、デバイスに応じて、セッション開始から約 1 ~ 3 フレーム後に利用可能になります。
セマンティック ラベルのピクセル数の割合をクエリする
現在のフレーム内の特定のクラス(空など)に属するピクセルの割合をクエリすることもできます。このクエリは、セマンティック画像を返して特定のラベルをピクセル単位で検索するよりも効率的です。返される小数は、[0.0, 1.0]
の範囲内の浮動小数点数値です。
fractionForSemanticLabel:
を使用して、指定したラベルの分数を取得します。
// Ensure that semantic data is present for the GARFrame.
if (garFrame.semanticImage) {
float fraction = [garFrame fractionForSemanticLabel:GARSemanticLabelSky];
}