MRが楽しい

MRやVRについて学習したことを書き残す

公式チュートリアル「MR and Azure 304 7章」を試してみる

本日はチュートリアルの実施枠です。
Academyの「MR and Azure 304: Face recognition」の実施内容をまとめます。
docs.microsoft.com
前回記事の続きです。
bluebirdofoz.hatenablog.com
今回は「Chapter 7」です。

Chapter 7:Create the ImageCapture class

ImageCaptureクラスの目的はキャプチャする画像を分析し、その中の顔を識別して既知の人物かどうかを判断することです。
Azure顔認識サービスと通信するために必要なメソッドを利用します。
既知の人物が見つかった場合、このクラスはその名前をシーン内のUIテキストとして表示します。

1.Script フォルダを開きます。
2.フォルダ内で右クリックして、Creapte -> C# Script を選択します。
Script の名称は ImageCapture に設定します。
f:id:bluebirdofoz:20180925045201j:plain

3.新しいスクリプトをダブルクリックしてVisual Studioで開きます。
4.以下の通り、スクリプトを編集します。
・ImageCapture.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Experimental.XR;
using UnityEngine.UI;
using UnityEngine.XR;
using UnityEngine.XR.WSA.Input;

public class Gaze : MonoBehaviour
{
    internal GameObject gazedObject;
    public float gazeMaxDistance = 300;

    void Update()
    {
        // Uses a raycast from the Main Camera to determine which object is gazed upon.
        // メインカメラからのレイキャストを使用して、どのオブジェクトが注視されているかを判断します。
        Vector3 fwd = gameObject.transform.TransformDirection(Vector3.forward);
        Ray ray = new Ray(Camera.main.transform.position, fwd);
        RaycastHit hit;
        Debug.DrawRay(Camera.main.transform.position, fwd);

        if (Physics.Raycast(ray, out hit, gazeMaxDistance) && hit.collider != null)
        {
            if (gazedObject == null)
            {
                gazedObject = hit.transform.gameObject;

                // Set the gazedTarget in the Behaviours class
                // Behaviorsクラスでの注視ターゲットを設定します
                Behaviours.instance.gazedTarget = gazedObject;
            }
        }
        else
        {
            ResetGaze();
        }
    }

    // Turn the gaze off, reset the gazeObject in the Behaviours class.
    // 注視をオフにし、Behaviorsクラスの注視オブジェクトをリセットします。
    public void ResetGaze()
    {
        if (gazedObject != null)
        {
            Behaviours.instance.gazedTarget = null;
            gazedObject = null;
        }
    }
}

f:id:bluebirdofoz:20180925045212j:plain

5.Visual Studio で変更を保存して Unity に戻ります。
f:id:bluebirdofoz:20180925045249j:plain

Chapter 7 はここまでです。
次回は Chapter 8 ~ 10 を実施します。