本文へスキップ
バージョン: 3.4

オブジェクト検出を有効にする方法

Lightship Object Detectionは、Lightshipのコンテキスト認識システムに200以上のクラスを追加し、画像内のオブジェクトの周囲にセマンティックにラベル付けされた2Dバウンディングボックスを可能にします。 オブジェクト検出クラスの詳細については、機能ページを参照してください。

前提条件

ARDKをインストールしたUnityプロジェクトと、基本的なARシーンが必要です。 詳しくは、ARDK 3のインストールおよびARシーンの設定を参照してください。

オブジェクト検出UIの設定

オブジェクト検出UIを設定する:

  1. Hierarchy(階層)**で、**Main Camera(メインカメラ)**を選択し、**Inspector(インスペクタ)**で、**Add Component(コンポーネントの追加)**をクリックし、**AR Object Detection Manager(ARオブジェクト検出マネージャ)**を追加します。

  2. 出力を表示するためのUI要素を作成する:

    1. Hierarchy**で、メインシーンを右クリックし、UIにマウスオーバーし、Text - Text Mesh Proを選択します。
    2. TMP Importer "ダイアログボックスが表示されたら、Import TMP Essentialsを選択します。
    3. 新しいText (TMP)オブジェクトを選択し、名前をObjectsDetectedTextに変更します。
    4. ObjectsDetectedTextを選択したまま、InspectorでRect Transformメニューを見つけます。 アンカー・プリセットボックスをクリックし、Shiftを押したまま左上のボックスを選択してテキストを左上にアンカーします。
    5. ポジションの値は以下のように設定する:
      • ポスX50
      • Yポジション:-200
      • ポジション Z: 0
      • 幅:900
      • 身長:300
    The Rect Transform menu
  3. 読みやすくするために、テキストの頂点色を黒に設定する。

    Setting the text vertex color to black

オブジェクト検出スクリプトの追加

オブジェクト検出スクリプトを追加する:

  1. ProjectタブのAssetsディレクトリを右クリックし、Createにマウスオーバーして、C# Scriptを選択します。 それをObjectDetectionOutputManager**と名付け、以下のコードを追加する:
クリックしてオブジェクト検出スクリプトを表示
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Niantic.Lightship.AR.ObjectDetection;
using Niantic.Lightship.AR.Subsystems.ObjectDetection;
using Niantic.Lightship.AR.XRSubsystems; using TMPro; use Unityngine; public class ObjectDetection Output Manager : Monoehaviage.XRSubsystems;
using TMPro;
using UnityEngine;

public class ObjectDetectionOutputManager : MonoBehaviour
{
[SerializeField]
private ARObjectDetectionManager _objectDetectionManager;

[SerializeField]
public TMP_Text _objectsDetectedText;

private void Start()
{
_objectDetectionManager.enabled = true;
_objectDetectionManager.MetadataInitialized += OnMetadataInitialized;
}

private void OnMetadataInitialized(ARObjectDetectionModelEventArgs args)
{
_objectDetectionManager.ObjectDetectionsUpdated += ObjectDetectionsUpdated;
}

private void ObjectDetectionsUpdated(ARObjectDetectionsUpdatedEventArgs args)
{
//出力文字列を初期化する
string resultString = "";
var result = args.Results;

if (result == null)
{
Debug.Log("No results found.");
return;
} //結果文字列をリセットする if (result == null) { Debug.Log("No results found."); return; } //出力文字列を初期化する if (result == null) { Debug.Log("No results found.

//Reset our results string
resultString = "";

//Iterate through our results
for (int i = 0; i < result.Count; i++)
{
var detection = result[i];
var categorizations = detection.GetConfidentCategorizations();
if (categorizations.Count <= 0)
{
break;
}.

//Sort our categorizations by highest confidence
categorizations.Sort((a, b) => b.Confidence.CompareTo(a.Confidence));

//Iterate through found categoires and form our string to output
for (int j = 0; j < categorizations.Count; j++)
{
var categoryToDisplay = categorizations[j];

resultString += "Detected " + $"{categoryToDisplay.CategoryName}: " + "with " + $".{categoryToDisplay.Confidence} 信頼性 \n";
}.
}

//
_objectsDetectedText.text = resultString;
}


private void OnDestroy()
{
_objectDetectionManager.MetadataInitialized -= OnMetadataInitialized;
_objectDetectionManager.ObjectDetectionsUpdated -= ObjectDetectionsUpdated;
}.
}

  1. 新しいスクリプトを階層メインカメラにドラッグ&ドロップして、コンポーネントとして追加します。

  2. スクリプトにオブジェクトを割り当てます:

    1. Hierarchyで、Main Cameraを選択し、InspectorObjectDetectionOutputManager**スクリプトコンポーネントを見つけます。

    2. メインカメラObject Detection Managerフィールドに割り当て、次にObjectsDetectedText**Objects Detected Textフィールドに割り当てます。

Assigning the script's variable fields

結果例

これで、AR Playbackまたはモバイル・デバイスを使用してオブジェクト検出をテストできるようになり、以下の画像のように画面の左上隅に検出出力が表示されます:

An example of working object detection