Unityのお勉強のためにPepperのカメラ画像を取得して表示してみた。
前提
-
Pepperの操作には以下のライブラリを利用させていただく。
-
PepperのAPIはALVideoDeviceProxy::getImageRemote()を利用する。
- 事前にALVideoDeviceProxy::subscribeCamera()を実行する必要あり
環境
- 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 ();
}
}
}
やってみた感想
- 取得画像を640 x 480とかにするとかなり遅くなる。
- Pepperの画像種別指定とTexture2Dへの渡し方で結構苦労した。
- 画像をYUVとかにするとパフォーマンスが良くなるみたいだが現状はtexture2Dへ渡せない?