本日はチュートリアルの実施枠です。
Academyの「MR and Azure 302b: Custom vision」の実施内容をまとめます。
docs.microsoft.com
前回記事の続きです。
bluebirdofoz.hatenablog.com
今回は「Chapter 6」です。
Chapter 6:Create the CustomVisionAnalyser class
最初に作成するスクリプトは CustomVisionAnalyser クラスです。
これは、次の処理を行います。
・取得した最新のイメージをバイト配列としてロードします。
・Azure Custom vision Serviceインスタンスにバイト配列を送信して解析します。
・応答をJSON文字列として受け取ります。
・レスポンスをデシリアライズし、結果の予測を SceneOrganiser クラスに渡します。
これにより、レスポンスの表示方法が処理されます。
1.Script フォルダを作成します。
Asset フォルダで右クリックし、Create > Folder を選択します。
2-3.作成した Script フォルダを開き、フォルダ内で右クリックして、Creapte -> C# Script を選択します。
Script の名称は CustomVisionAnalyser に設定します。
4.新しいスクリプトをダブルクリックしてVisual Studioで開きます。
5.以下の通り、スクリプトを編集します。
・CustomVisionAnalyser.cs
// 名前空間の追加 using System.Collections; using System.IO; using UnityEngine; using UnityEngine.Networking; using Newtonsoft.Json; public class CustomVisionAnalyser : MonoBehaviour { // メンバ変数の追加 /// <summary> /// Unique instance of this class /// クラスのユニークなインスタンス /// </summary> public static CustomVisionAnalyser Instance; /// <summary> /// Insert your Prediction Key here /// prediction-key をここに挿入してください /// </summary> private string predictionKey = "- Insert your key here -"; /// <summary> /// Insert your prediction endpoint here /// エンドポイントをここに挿入してください /// </summary> private string predictionEndpoint = "Insert your prediction endpoint here"; /// <summary> /// Byte array of the image to submit for analysis /// 分析のために提出する画像のバイト配列 /// </summary> [HideInInspector] public byte[] imageBytes; /// <summary> /// Initialises this class /// 初期化クラス /// </summary> private void Awake() { // Allows this instance to behave like a singleton // このクラスをシングルトンと同じように動作させます Instance = this; } /// <summary> /// Call the Computer Vision Service to submit the image. /// Computer Vision Service を呼び出して画像を送信します /// </summary> public IEnumerator AnalyseLastImageCaptured(string imagePath) { WWWForm webForm = new WWWForm(); using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(predictionEndpoint, webForm)) { // Gets a byte array out of the saved image // 保存されたイメージからバイト配列を取得します imageBytes = GetImageAsByteArray(imagePath); unityWebRequest.SetRequestHeader("Content-Type", "application/octet-stream"); unityWebRequest.SetRequestHeader("Prediction-Key", predictionKey); // The upload handler will help uploading the byte array with the request // アップロードハンドラはリクエストと共にバイト配列をアップロードします unityWebRequest.uploadHandler = new UploadHandlerRaw(imageBytes); unityWebRequest.uploadHandler.contentType = "application/octet-stream"; // The download handler will help receiving the analysis from Azure // ダウンロードハンドラは、Azure から分析結果を受け取ります unityWebRequest.downloadHandler = new DownloadHandlerBuffer(); // Send the request // リクエストを送信する yield return unityWebRequest.SendWebRequest(); string jsonResponse = unityWebRequest.downloadHandler.text; // The response will be in JSON format, therefore it needs to be deserialized // レスポンスはJSON形式であるため、デシリアライズする必要があります // The following lines refers to a class that you will build in later Chapters // Wait until then to uncomment these lines // 以下の行は、後の章で構築するクラスを参照しています。 // 解説でこれらの行のコメントを解除するまで待ってください //AnalysisObject analysisObject = new AnalysisObject(); //analysisObject = JsonConvert.DeserializeObject<AnalysisObject>(jsonResponse); //SceneOrganiser.Instance.SetTagsToLastLabel(analysisObject); } } /// <summary> /// Returns the contents of the specified image file as a byte array. /// 指定されたイメージファイルの内容をバイト配列として返します /// </summary> static byte[] GetImageAsByteArray(string imageFilePath) { FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read); BinaryReader binaryReader = new BinaryReader(fileStream); return binaryReader.ReadBytes((int)fileStream.Length); } }
※ AnalyseImageCapture のコルーチンの呼び出しがあります。
SceneOrganiser クラスは Chapter 10 で作成します。それまでこれらの行はコメントしたままにしておきます。
5.Visual Studio で変更を保存して Unity に戻ります。
Chapter 6 はここまでです。
次回は Chapter 7 を実施します。
bluebirdofoz.hatenablog.com