Esempio n. 1
0
        public void ExecuteFFMPEG()
        {
            VirtualCVLog.Log("Execute ffmpeg");

            // libx264 : mpegts
            // jpg : mjpeg
            string[] ffmpegOptions =
            {
                "-re",
                "-stream_loop","-1",
                "-i",          "pipe:",
                "-c:v",        "jpg",
                "-vf",         $"fps={FPS}",
                "-c:v",        "mjpeg",
                "-preset",     "veryfast",
                "-f",          "mjpeg",
                $"{URL}:{Port}"
            };

            ffmpegProcess.StartInfo.FileName               = Path.Combine(ffmpegPath, "ffmpeg.exe");
            ffmpegProcess.StartInfo.Arguments              = string.Join(" ", ffmpegOptions);
            ffmpegProcess.StartInfo.WorkingDirectory       = ffmpegPath;
            ffmpegProcess.StartInfo.UseShellExecute        = false;
            ffmpegProcess.StartInfo.RedirectStandardInput  = true;
            ffmpegProcess.StartInfo.RedirectStandardOutput = true;
            ffmpegProcess.StartInfo.CreateNoWindow         = true;

            ffmpegProcess.Start();

            ffmpegStreamWriter = ffmpegProcess.StandardInput;
        }
Esempio n. 2
0
        public static void SaveSettings(VirtualCVCameraParams _param)
        {
            param = _param;

            try
            {
                using (StreamWriter outputFile = new StreamWriter(settingFilePath))
                {
                    outputFile.WriteLine($"usePhysicalCamera={param.usePhysicalCamera}");
                    outputFile.WriteLine($"useDepthCameara={param.useDepthCameara}");
                    outputFile.WriteLine($"useStereoCamera={param.useStereoCamera}");
                    outputFile.WriteLine($"textureWidth={param.textureWidth}");
                    outputFile.WriteLine($"textureHeight={param.textureHeight}");
                    outputFile.WriteLine($"fov={param.fov}");
                    outputFile.WriteLine($"fps={param.fps}");
                    outputFile.WriteLine($"focal_length={param.focal_length}");
                    outputFile.WriteLine($"ipd={param.ipd}");
                    outputFile.WriteLine($"python_script={param.python_script}");
                }
            }
            catch (Exception e)
            {
                VirtualCVLog.LogE($"fail to save settings : {e}");
            }
        }
Esempio n. 3
0
        void SetCamera()
        {
            cam = GetComponent <Camera>();

            cam.fieldOfView           = VirtualCVSettings.GetParam().fov;
            cam.usePhysicalProperties = VirtualCVSettings.GetParam().usePhysicalCamera;
            cam.focalLength           = VirtualCVSettings.GetParam().focal_length;

            VirtualCVLog.Log($"Camera info : {name} - {cam.fieldOfView} - {cam.usePhysicalProperties} - {cam.focalLength}");
        }
Esempio n. 4
0
        void TakeScreenshot()
        {
            screenshotIndex++;
            string isRight            = (isRightCamera ? "right" : "left");
            string screenshotFileName = Path.Combine(screenshotPath, string.Format($"Screenshot_{isRight}_{screenshotIndex}.jpg"));

            VirtualCVLog.Log($"Screenshot saved : path - {screenshotFileName}");

            File.WriteAllBytes(screenshotFileName, cameraImage.EncodeToJPG());
        }
Esempio n. 5
0
        public static void WebSocketProc()
        {
            VirtualCVLog.Log("Start WebSocket Server");

            string serverURL = $"{URL}:{Port}";

            webSocketServer = new WebSocketServer(serverURL);
            webSocketServer.AddWebSocketService <Data>("/Data");
            webSocketServer.Start();
        }
Esempio n. 6
0
        void OnGUI()
        {
            GUILayout.Label("Camera settings", EditorStyles.boldLabel);

            param.usePhysicalCamera = EditorGUILayout.Toggle("Use physical camera", param.usePhysicalCamera);
            param.useDepthCameara   = EditorGUILayout.Toggle("Use depth camera", param.useDepthCameara);

            EditorGUILayout.Space();

            param.textureWidth  = EditorGUILayout.IntField("Texture width", param.textureWidth);
            param.textureHeight = EditorGUILayout.IntField("Texture height", param.textureHeight);

            EditorGUILayout.Space();

            param.fov          = EditorGUILayout.FloatField("Field of view", param.fov);
            param.fps          = EditorGUILayout.IntField("FPS", param.fps);
            param.focal_length = EditorGUILayout.FloatField("Focal length", param.focal_length);

            EditorGUILayout.Space();

            param.useStereoCamera = EditorGUILayout.Toggle("Use stereo camera", param.useStereoCamera);
            param.ipd             = EditorGUILayout.FloatField("Interpupillary distance", param.ipd);

            EditorGUILayout.Space();

            if (GUILayout.Button("Save settings"))
            {
                VirtualCVLog.Log("Settings saved");
                VirtualCVSettings.SaveSettings(param);
            }
            if (GUILayout.Button("Apply to camera"))
            {
                ApplyCamera();
            }

            EditorGUILayout.Space();

            int selectedPythonScript = GetPythonScriptIndex(param.python_script);

            selectedPythonScript = EditorGUILayout.Popup("Python script", selectedPythonScript, pythonFiles);
            param.python_script  = pythonFiles[selectedPythonScript];
            if (GUILayout.Button("Launch the script"))
            {
                if (Application.isPlaying)
                {
                    PythonExecutor.getInstance().ExecutePython(param.python_script);
                }
                else
                {
                    VirtualCVLog.LogE("It's able to run only when Unity is playing.");
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Execute python script
        /// </summary>
        /// <param name="scriptFile">python script file name, default is opencv.py</param>
        public void ExecutePython(string pythonScriptFile)
        {
            VirtualCVLog.Log("Execute python script : " + pythonScriptFile);

            string useStereo = VirtualCVSettings.GetParam().useStereoCamera ? "stereo" : "";

            pythonProcess.StartInfo.FileName               = pythonExe;
            pythonProcess.StartInfo.WorkingDirectory       = pythonScriptPath;
            pythonProcess.StartInfo.Arguments              = $"{pythonScriptFile} {useStereo}";
            pythonProcess.StartInfo.UseShellExecute        = false;
            pythonProcess.StartInfo.RedirectStandardOutput = true;
            pythonProcess.StartInfo.CreateNoWindow         = true;

            pythonProcess.Start();
        }
Esempio n. 8
0
        /// <summary>
        /// Attach virtualCV camera to Unity main camera
        /// </summary>
        private void ApplyCamera()
        {
            foreach (Transform child in Camera.main.transform)
            {
                if (child.name == "VirtualCVRig")
                {
                    VirtualCVLog.LogW("VirtualCVRig has already been applied");
                    return;
                }
            }

            GameObject prefabVirtualCV = Resources.Load("VirtualCVRig") as GameObject;
            GameObject virtualCVRig    = Instantiate(prefabVirtualCV);

            virtualCVRig.transform.SetParent(Camera.main.transform);
            virtualCVRig.transform.localPosition = Vector3.zero;
            virtualCVRig.transform.localRotation = Quaternion.identity;
            virtualCVRig.transform.localScale    = Vector3.one;
            virtualCVRig.name = "VirtualCVRig";
        }
Esempio n. 9
0
        void Start()
        {
            VirtualCVLog.Log("VirtualCVCamera starts");

            bool useStereo = VirtualCVSettings.GetParam().useStereoCamera;

            isRightCamera = (name == "VirtualCVCameraRight");
            if (isRightCamera && !useStereo)
            {
                return;
            }

            SetCamera();
            SetTexture();

            screenshotPath = Path.Combine(Application.dataPath, "..", "Screenshot");
            Directory.CreateDirectory(screenshotPath);

            int port = isRightCamera ? rightCameraPort : leftCameraPort;

            ffmpegExecutor = new FFMPEGExecutor(port);
            ffmpegExecutor.Initialze();
            ffmpegExecutor.ExecuteFFMPEG();
        }
Esempio n. 10
0
        public static VirtualCVCameraParams LoadSettings()
        {
            try
            {
                string line;
                using (var sr = new StreamReader(settingFilePath))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Contains("usePhysicalCamera"))
                        {
                            param.usePhysicalCamera = GetValue <bool>(line);
                        }
                        if (line.Contains("useDepthCameara"))
                        {
                            param.useDepthCameara = GetValue <bool>(line);
                        }
                        if (line.Contains("useStereoCamera"))
                        {
                            param.useStereoCamera = GetValue <bool>(line);
                        }

                        if (line.Contains("textureWidth"))
                        {
                            param.textureWidth = GetValue <int>(line);
                        }
                        if (line.Contains("textureHeight"))
                        {
                            param.textureHeight = GetValue <int>(line);
                        }

                        if (line.Contains("fov"))
                        {
                            param.fov = GetValue <float>(line);
                        }
                        if (line.Contains("fps"))
                        {
                            param.fps = GetValue <int>(line);
                        }
                        if (line.Contains("focal_length"))
                        {
                            param.focal_length = GetValue <float>(line);
                        }
                        if (line.Contains("ipd"))
                        {
                            param.ipd = GetValue <float>(line);
                        }

                        if (line.Contains("python_script"))
                        {
                            param.python_script = GetValue <string>(line);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                VirtualCVLog.LogE($"fail to read settings file : {e}");

                // return default parameters
                param.usePhysicalCamera = false;
                param.useDepthCameara   = false;
                param.useStereoCamera   = false;
                param.textureWidth      = 640;
                param.textureHeight     = 480;
                param.fov           = 60;
                param.fps           = 20;
                param.focal_length  = 50;
                param.ipd           = 1.0f;
                param.python_script = "opencv.py";
            }

            return(param);
        }
Esempio n. 11
0
 public void Initialze()
 {
     VirtualCVLog.Log("ffmpeg path : " + ffmpegPath);
 }