0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Magic Leap Oneでハンドジェスチャーを認識する

Posted at

やること

サムズアップした手をMagicLeapが認識したら、Unityエディタ上にログが出るようにします。
動作の確認はMagicLeapとPCを繋いでZeroIterationを使って行います。

環境

PC : Windows10
Unity : 2019.3.7f1
Lumin SDK : v.0.24.1

実装

まずはGestureTrackingManagerクラスを作成します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;

  public class GestureTrackingManager : MonoBehaviour
  {
      private void Start()
      {
      }
      private void Update()
      {
      }
  }

使用するジェスチャーを定義し、Startで登録します。使用するジェスチャーが複数ある場合はリストにして登録しておきます。
今回はサムズアップのみ使用します。

/// <summary>使用するジェスチャー</summary>
private MLHandTracking.HandKeyPose _gesture;

private void Start()
{
    MLHandTracking.Start();

    // トラッキングするジェスチャーを登録
    _gesture = new MLHandTracking.HandKeyPose.Thumb;

    // 登録したジェスチャーのトラッキングを有効にする
    MLHandTracking.KeyPoseManager.EnableKeyPoses(_gesture, true, false);
        }

次は実際に認識する処理です。
下記GetGestureメソッドは指定したジェスチャーを認識したらtrueを返します。

bool GetGesture(MLHandTracking.Hand hand)
{
    if (hand != null)
    {
        if (hand.KeyPose == MLHandTracking.HandKeyPose.Thumb && hand.HandKeyPoseConfidence > 0.9f)
        {
            return true;
        }
    }
    return false;
}

左右どちらかの手でサムズアップしたらログを出します。

public void TrackGesture()
{
    if(GetGesture(MLHandTracking.Left) || GetGesture(MLHandTracking.Right))
    {
        Debug.Log("Hand gesture Thumb was detected.");
    }
}

TrackGestureメソッドをUpdateで呼び出します。
クラス全体は下記です。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;

namespace Manager
{
    public class GestureTrackingManager : MonoBehaviour
    {
        /// <summary>使用するジェスチャーリスト</summary>
        private MLHandTracking.HandKeyPose[] _gestures;

        private void Start()
        {
            MLHandTracking.Start();

            // トラッキングするジェスチャーを登録
            _gestures = new MLHandTracking.HandKeyPose[2];
            _gestures[0] = MLHandTracking.HandKeyPose.Ok;
            _gestures[1] = MLHandTracking.HandKeyPose.Thumb;

            // 登録したジェスチャーのトラッキングを有効にする
            MLHandTracking.KeyPoseManager.EnableKeyPoses(_gestures, true, false);
        }

        private void Update()
        {
            TrackGesture();
        }

        private void OnDestroy()
        {
            MLHandTracking.Stop();
        }

        public void TrackGesture()
        {
            if (GetGesture(MLHandTracking.Left) || GetGesture(MLHandTracking.Right))
            {
                Debug.Log("Hand gesture Thumb was detected.");
            }
        }

        bool GetGesture(MLHandTracking.Hand hand)
        {
            if (hand != null)
            {
                if (hand.KeyPose == MLHandTracking.HandKeyPose.Thumb && hand.HandKeyPoseConfidence > 0.9f)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

コンソールに以下のようなログが出ます。
スクリーンショット 2021-01-08 195648.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?