LoginSignup
16
2

More than 3 years have passed since last update.

AzureKinectで肩乗りペット

Last updated at Posted at 2020-12-08

クソアプリ2 Advent Calendar 2020
9日目がんばります💪

はじめに

みなさんは肩乗りぺット好きですか?
主人への愛が感じられるとても微笑ましい行動です。
どれもとても可愛らしいですね。

images1.jpg
images2.jpg
images3.jpg
images4.jpg

これらの事象を基に私は仮説を立てました。
「どんな忌み嫌われるモノでも人の肩に乗れば可愛くなるのではないか」

そこで皆が嫌がるモノ、「クソ」で実験してみたいと思います。(無理矢理)

つくる

さっそくクソ(を可愛くする)アプリを作っていきましょう。

目下R&Dに勤しんでいるAzureKinectDKとUnityを使って実現します。
AzureKinectDKは5万円弱で、ウェアラブルセンサー無しにボディトラッキングできちゃう優れものです。
AzureKinectDKで肩の位置をトラッキングして、Unityで肩の座標にクソペットを表示してやります。

Unityのソースコードはこれだけ。

using Microsoft.Azure.Kinect.BodyTracking;
using Microsoft.Azure.Kinect.Sensor;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
namespace BodyEffect
{
    public class ShoulderPet : MonoBehaviour
    {
        [SerializeField]
        GameObject kusoPetPrefab;
        [SerializeField]
        RawImage cameraScreen;
        private Device kinect;
        private Tracker tracker;
        private Texture2D texture;
        private GameObject kusoPet;
        private void Awake()
        {
            this.kinect = Device.Open(0);
            this.kinect.StartCameras(new DeviceConfiguration
            {
                ColorFormat = ImageFormat.ColorBGRA32,
                ColorResolution = ColorResolution.R720p,
                DepthMode = DepthMode.NFOV_2x2Binned,
                SynchronizedImagesOnly = true,
                CameraFPS = FPS.FPS30
            });
            var width = kinect.GetCalibration().ColorCameraCalibration.ResolutionWidth;
            var height = kinect.GetCalibration().ColorCameraCalibration.ResolutionHeight;
            this.texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
            this.texture.Apply();
            this.cameraScreen.texture = this.texture;
            this.tracker = Tracker.Create(kinect.GetCalibration(), TrackerConfiguration.Default);
            this.kusoPet = Instantiate(this.kusoPetPrefab, this.transform);
            var t = this.KinectLoop();
        }
        private void OnDestroy()
        {
            this.kinect.StopCameras();
        }
        private async Task KinectLoop()
        {
            while (true)
            {
                using (var capture = await Task.Run(() => this.kinect.GetCapture()).ConfigureAwait(true))
                {
                    var colorImage = capture.Color;
                    var colorArray = colorImage.GetPixels<BGRA>().ToArray();
                    var colors = new Color32[colorArray.Length];
                    for (var i = 0; i < colorArray.Length; i++)
                    {
                        colors[i].b = colorArray[i].B;
                        colors[i].g = colorArray[i].G;
                        colors[i].r = colorArray[i].R;
                        colors[i].a = 255;
                    }
                    this.texture.SetPixels32(colors);
                    this.texture.Apply();
                    this.tracker.EnqueueCapture(capture);
                    var frame = tracker.PopResult();
                    if (frame.NumberOfBodies > 0)
                    {
                        var joint = frame.GetBodySkeleton(0).GetJoint(JointId.ShoulderRight);
                        // 生座標は動きすぎるので適当に調整
                        var jointPos = new Vector3(
                                -1 * joint.Position.X / 30,
                                -1 * joint.Position.Y / 30 + 6,
                                joint.Position.Z / 30
                            );
                        this.kusoPet.transform.localPosition = jointPos;
                    }
                }
            }
        }
    }
}

こいつを空オブジェクトにAddComponentして、各種オブジェクトをアタッチしてやります。
image_2020_12_3.png
注意点としては、カメラ映像をRawImageUIを利用して投影しているところ。
image_2020_12_3 (2).png
ペットにあたるGameObjectがUIより手前に表示できるようにする必要があります。
image_2020_12_3 (1).png
ここまでできたらデスクトップアプリにビルドして完成!

Play

Videotogif (3).gif
元々可愛くデフォルメされた画像を利用してしまったので実験効果がわかりにくいですね。
次回は実写で検証してみたいと思います。(嘘)

16
2
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
16
2