LoginSignup
9
5

More than 5 years have passed since last update.

UnityでPepperのカメラ画像を取得して表示してみた

Posted at

Unityのお勉強のためにPepperのカメラ画像を取得して表示してみた。

前提

環境

  • Unity 5.5.2f1 personal
  • Pepper NAOqi Version 2.5.5.5
  • LibqiUnity_ver3.01_alpha

書いてみたコード

RawImageに表示させてみた。
そのままだと反対の表示になってしまうので180度回転させている。

using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using Baku.LibqiDotNet;
using Baku.LibqiDotNet.Service;
using Libqi = Baku.LibqiDotNet.Libqi;

public class PepperAccessor : MonoBehaviour {
    public UnityEngine.UI.RawImage imageUi;

    // Use this for initialization
    void Start () {
        GetImage ();
    }

    // Update is called once per frame
    void Update () {
    }

    private void GetImage() {
        QiSession session = new QiSessionFactory().CreateSocketIoSession();
        try {
            session.Connect("XX.XX.XX.XX");
            if (!session.IsConnected)
            {
                Debug.Log("Failed to connect");
                return;
            }
            ALVideoDevice video = ALVideoDevice.CreateService (session);

            string camera_id = "camera";
            // カメラを設定
            // Name:
            // CameraIndex:top camera指定
            // Resolution:Image of 160*120px指定
            // ColorSpace:Buffer contains triplet on the format 0xBBGGRR, equivalent to three unsigned char指定
            // Fps:とりあえず30fps指定
            string videoHandle = video.SubscribeCamera (camera_id, 0, 0, 11, 30);
            try {
                IQiResult result = video.GetImageRemote (videoHandle);
                try {
                    byte[] image = result [6].ToBytes ();
                    int width = result [0].ToInt32 ();
                    int height = result [1].ToInt32 ();

                    Texture2D texture = new Texture2D (width, height, TextureFormat.RGB24, false);
                    texture.LoadRawTextureData (image);
                    texture.Apply ();
                    imageUi.texture = texture;
                }
                finally {
                    video.ReleaseImage(camera_id);
                }
            }
            finally {
                video.Unsubscribe (camera_id);
            }
        }
        finally {
            session.Close ();
        }
    }
}

やってみた感想

9
5
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
9
5