카테고리 없음

AirSim Unity에서 Data 저장하기

Wood Pecker 2021. 9. 23. 21:42

1. 개요

AirSim을 구현한 유니티에서 버튼을 클릭하면 자신이 원하는 데이터(Depth View, Segmemtation View, Scene View, Streo Views 등)를 로컬 PC에 저장할 수 있도록 간단한 스크립트를 만들어 본다.

 

2. Car 게임오브젝트의 컴포먼트로 다음의 스크립트를 부착한다.

using UnityEngine;
using System;
using System.IO;
using AirSimUnity.CarStructs;

namespace AirSimUnity
{

    public class MyCapture : MonoBehaviour
    {
        int count = 0;
        string path;
        public RenderTexture renderTextureDepth;//inspector 창에서 설정
        public RenderTexture renderTextureSeg;//inspector 창에서 설정
        public RenderTexture renderTextureScene;//inspector 창에서 설정
        Car car;

        void Start()
        {
            path = Application.dataPath;
            car = GetComponent<Car>();
        }

        void OnGUI()
        {
            if (GUI.Button(new Rect(50, 170, 100, 60), "Manual Capture"))
            {   //Assets아래에 DataCapture 폴더를 미리 만들어야 한다. 
                Vector3 pos = transform.position;
                Quaternion rot = transform.rotation;
                Vector3 vRot = transform.rotation.eulerAngles;

                CarState carState= car.GetCarState();
                string dataStr = count + "," + pos.x + "," + pos.y + "," + pos.z + "," + vRot.x + "," + vRot.y + "," + vRot.z;
                dataStr = dataStr + "," + carState.speed+","+carState.gear;
                Texture2D texture1 = new Texture2D(renderTextureDepth.width, renderTextureDepth.height);
                Texture2D texture2 = new Texture2D(renderTextureSeg.width, renderTextureSeg.height);
                Texture2D texture3 = new Texture2D(renderTextureScene.width, renderTextureScene.height);

                RenderTexture.active = renderTextureDepth;
                texture1.ReadPixels(new Rect(0, 0, renderTextureDepth.width, renderTextureDepth.height), 0, 0);
                byte[] bytesDepth = texture1.EncodeToJPG();

                RenderTexture.active = renderTextureSeg;
                texture2.ReadPixels(new Rect(0, 0, renderTextureSeg.width, renderTextureSeg.height), 0, 0);
                byte[] bytesSeg = texture2.EncodeToJPG();

                RenderTexture.active = renderTextureScene;
                texture3.ReadPixels(new Rect(0, 0, renderTextureScene.width, renderTextureScene.height), 0, 0);
                byte[] bytesScene = texture3.EncodeToJPG();
                RenderTexture.active = null;
                string countStr = string.Format("{0:D4}", count); // 0001
                string path1 = path + "/DataCapture/Depth" + countStr + ".jpg";
                string path2 = path + "/DataCapture/Seg" + countStr + ".jpg";
                string path3 = path + "/DataCapture/Scene" + countStr + ".jpg";
                string path4 = path + "/DataCapture/data.txt";

                System.IO.File.WriteAllBytes(path1, bytesDepth);
                System.IO.File.WriteAllBytes(path2, bytesSeg);
                System.IO.File.WriteAllBytes(path3, bytesScene);

                File.AppendAllText(path4, dataStr + Environment.NewLine);
                count = count + 1;
            }
        }
    }
}
  1. 실행결과
    자동으로 기록하는 방식이 아닌 사용자가 클릭할 때의 정보를 저장하는 방식이다.

4. ViewCamera의 배치와 CameraFiltersScript를 적절히 선택하면 스테레오 이미지를 얻을 수 있다.

                          Left Scene                                                             Right Scene

반응형