本日はチュートリアルの実施枠です。
Academyの「MR and Azure 302: Computer vision」の実施内容をまとめます。
docs.microsoft.com
前回記事の続きです。
bluebirdofoz.hatenablog.com
今回は「Chapter 5」です。
Chapter 5:Create the ResultsLabel class
最初に作成するスクリプトは ResultsLabel クラスです。
これは、次の処理を行います。
・適切なワールド空間で、カメラの位置を基準にしてラベルを作成する。
・画像解析からのタグの表示。
1.Script フォルダを作成します。
Asset フォルダで右クリックし、Create > Folder を選択します。
2.作成した Script フォルダを開き、フォルダ内で右クリックして、Creapte -> C# Script を選択します。
Script の名称は ResultsLabel に設定します。
3.新しいスクリプトをダブルクリックしてVisual Studioで開きます。
4.以下の通り、スクリプトを編集します。
・ResultsLabel.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ResultsLabel : MonoBehaviour { public static ResultsLabel instance; public GameObject cursor; public Transform labelPrefab; [HideInInspector] public Transform lastLabelPlaced; [HideInInspector] public TextMesh lastLabelPlacedText; private void Awake() { // allows this instance to behave like a singleton // このクラスをシングルトンと同じように動作させます instance = this; } /// <summary> /// Instantiate a Label in the appropriate location relative to the Main Camera. /// メインカメラに対して、適切な場所にラベルをインスタンス化します。 /// </summary> public void CreateLabel() { lastLabelPlaced = Instantiate(labelPrefab, cursor.transform.position, transform.rotation); lastLabelPlacedText = lastLabelPlaced.GetComponent<TextMesh>(); // Change the text of the label to show that has been placed // The final text will be set at a later stage // 配置されていることを示すラベルのテキストを変更する // 最終的にテキストは後の段階で設定されます lastLabelPlacedText.text = "Analysing..."; } /// <summary> /// Set the Tags as Text of the last Label created. /// ラベルのテキストにタグを追加します /// </summary> public void SetTagsToLastLabel(Dictionary<string, float> tagsDictionary) { lastLabelPlacedText = lastLabelPlaced.GetComponent<TextMesh>(); // At this point we go through all the tags received and set them as text of the label // 受け取ったすべてのタグを調べ、ラベルのテキストとして設定します lastLabelPlacedText.text = "I see: \n"; foreach (KeyValuePair<string, float> tag in tagsDictionary) { lastLabelPlacedText.text += tag.Key + ", Confidence: " + tag.Value.ToString("0.00 \n"); } } }
5.Visual Studio で変更を保存して Unity に戻ります。
6.Hierarchy パネルの空の領域を右クリックして、Create Empty で空の GameObject を追加します。
名前を Scripts に変更します。
7.作成した Scripts オブジェクトに Assets フォルダから ResultsLabel クラスをドラッグして適用します。
8.ResultsLabel(Script) コンポーネントのスロットを以下のように設定します。
a.Cursor スロットに Chapter 4 で作成した Cursor オブジェクトを設定する。
b.LabelPrefab スロットに Chapter 4 で作成した LabelText.prefab を設定する。
Chapter 5 はここまでです。
次回は Chapter 6 を実施します。