예제 #1
0
    void OnVideoCaptureCreated(HoloLensCameraStream.VideoCapture videoCapture)
    {
        if (videoCapture == null)
        {
            Debug.LogError("Did not find a video capture object. You may not be using the HoloLens.");
            return;
        }

        this._videoCapture = videoCapture;

        //Request the spatial coordinate ptr if you want fetch the camera and set it if you need to
        CameraStreamHelper.Instance.SetNativeISpatialCoordinateSystemPtr(_spatialCoordinateSystemPtr);

        _resolution = CameraStreamHelper.Instance.GetLowestResolution();
        float frameRate = CameraStreamHelper.Instance.GetHighestFrameRate(_resolution);

        videoCapture.FrameSampleAcquired += OnFrameSampleAcquired;

        //You don't need to set all of these params.
        //I'm just adding them to show you that they exist.
        HoloLensCameraStream.CameraParameters cameraParams = new HoloLensCameraStream.CameraParameters();
        cameraParams.cameraResolutionHeight = _resolution.height;
        cameraParams.cameraResolutionWidth  = _resolution.width;
        cameraParams.frameRate             = Mathf.RoundToInt(frameRate);
        cameraParams.pixelFormat           = HoloLensCameraStream.CapturePixelFormat.BGRA32;
        cameraParams.rotateImage180Degrees = true; //If your image is upside down, remove this line.
        cameraParams.enableHolograms       = false;

        UnityEngine.WSA.Application.InvokeOnAppThread(() => {
            _videoPanelUI.SetResolution(_resolution.width, _resolution.height);
        }, false);

        videoCapture.StartVideoModeAsync(cameraParams, OnVideoModeStarted);
    }
    private void OnVideoCaptureCreated(VideoCapture videoCapture)
    {
        if (videoCapture == null)
        {
            Debug.LogError("Did not find a video capture object. You may not be using the HoloLens.");
            return;
        }

        _videoCapture = videoCapture;

        //Request the spatial coordinate ptr if you want fetch the camera and set it if you need to
        CameraStreamHelper.Instance.SetNativeISpatialCoordinateSystemPtr(_spatialCoordinateSystemPtr);

        _resolution = CameraStreamHelper.Instance.GetLowestResolution();
        float frameRate = CameraStreamHelper.Instance.GetHighestFrameRate(_resolution);

        Debug.Log("Frame rate: " + frameRate);

        videoCapture.FrameSampleAcquired += OnFrameSampleAcquired;

        CameraParameters cameraParams = new CameraParameters();

        cameraParams.cameraResolutionHeight = _resolution.height;
        cameraParams.cameraResolutionWidth  = _resolution.width;
        cameraParams.frameRate             = Mathf.RoundToInt(frameRate);
        cameraParams.pixelFormat           = CapturePixelFormat.BGRA32;
        cameraParams.rotateImage180Degrees = false;
        cameraParams.enableHolograms       = false;

        videoCapture.StartVideoModeAsync(cameraParams, OnVideoModeStarted);
    }
예제 #3
0
    private void OnVideoCaptureCreated(HoloLensCameraStream.VideoCapture v)
    {
        if (v == null)
        {
            Debug.LogError("No VideoCapture found");
            return;
        }

        _videoCapture = v;

        //Request the spatial coordinate ptr if you want fetch the camera and set it if you need to
        CameraStreamHelper.Instance.SetNativeISpatialCoordinateSystemPtr(_spatialCoordinateSystemPtr);

        _resolution = CameraStreamHelper.Instance.GetLowestResolution();
        float frameRate = CameraStreamHelper.Instance.GetHighestFrameRate(_resolution);

        _videoCapture.FrameSampleAcquired += OnFrameSampleAcquired;

        HoloLensCameraStream.CameraParameters cameraParams = new HoloLensCameraStream.CameraParameters();
        cameraParams.cameraResolutionHeight = _resolution.height;
        cameraParams.cameraResolutionWidth  = _resolution.width;
        cameraParams.frameRate   = Mathf.RoundToInt(frameRate);
        cameraParams.pixelFormat = HoloLensCameraStream.CapturePixelFormat.BGRA32;

        UnityEngine.WSA.Application.InvokeOnAppThread(() => { _pictureTexture = new Texture2D(_resolution.width, _resolution.height, TextureFormat.BGRA32, false); }, false);

        _videoCapture.StartVideoModeAsync(cameraParams, OnVideoModeStarted);
    }
예제 #4
0
    private void OnVideoCaptureInstanceCreated(HoloLensCameraStream.VideoCapture videoCapture)
    {
        if (videoCapture == null)
        {
            Debug.LogError("Creating the VideoCapture object failed.");
            return;
        }

        CameraStreamHelper.videoCapture = videoCapture;
        if (VideoCaptureCreated != null)
        {
            VideoCaptureCreated(videoCapture);
        }
    }
예제 #5
0
        protected virtual IEnumerator _Dispose()
        {
            while (isChangeVideoModeWaiting)
            {
                yield return(null);
            }

            isChangeVideoModeWaiting = true;
            videoCapture.StopVideoModeAsync(result => {
                videoCapture.Dispose();
                videoCapture             = null;
                isChangeVideoModeWaiting = false;
            });
        }
        protected virtual HoloLensCameraStream.CameraParameters CreateCameraParams(HoloLensCameraStream.VideoCapture videoCapture)
        {
            int min1 = videoCapture.GetSupportedResolutions().Min(r => Mathf.Abs((r.width * r.height) - (_requestedWidth * _requestedHeight)));

            HoloLensCameraStream.Resolution resolution = videoCapture.GetSupportedResolutions().First(r => Mathf.Abs((r.width * r.height) - (_requestedWidth * _requestedHeight)) == min1);

            float min2      = videoCapture.GetSupportedFrameRatesForResolution(resolution).Min(f => Mathf.Abs(f - _requestedFPS));
            float frameRate = videoCapture.GetSupportedFrameRatesForResolution(resolution).First(f => Mathf.Abs(f - _requestedFPS) == min2);

            HoloLensCameraStream.CameraParameters cameraParams = new HoloLensCameraStream.CameraParameters();
            cameraParams.cameraResolutionHeight = resolution.height;
            cameraParams.cameraResolutionWidth  = resolution.width;
            cameraParams.frameRate       = Mathf.RoundToInt(frameRate);
            cameraParams.pixelFormat     = CapturePixelFormat.BGRA32;
            cameraParams.enableHolograms = false;

            return(cameraParams);
        }
예제 #7
0
        /// <summary>
        /// Raises the destroy event.
        /// </summary>
        protected override void OnDestroy()
        {
            Dispose();

            if (videoCapture != null)
            {
                if (videoCapture.IsStreaming)
                {
                    videoCapture.StopVideoModeAsync(result => {
                        videoCapture.Dispose();
                        videoCapture = null;
                    });
                }
                else
                {
                    videoCapture.Dispose();
                    videoCapture = null;
                }
            }
        }
        protected virtual HoloLensCameraStream.CameraParameters CreateCameraParams(HoloLensCameraStream.VideoCapture videoCapture)
        {
            int min1 = videoCapture.GetSupportedResolutions().Min(r => Mathf.Abs((r.width * r.height) - (_requestedWidth * _requestedHeight)));

            HoloLensCameraStream.Resolution resolution = videoCapture.GetSupportedResolutions().First(r => Mathf.Abs((r.width * r.height) - (_requestedWidth * _requestedHeight)) == min1);

            float min2      = videoCapture.GetSupportedFrameRatesForResolution(resolution).Min(f => Mathf.Abs(f - _requestedFPS));
            float frameRate = videoCapture.GetSupportedFrameRatesForResolution(resolution).First(f => Mathf.Abs(f - _requestedFPS) == min2);

            HoloLensCameraStream.CameraParameters cameraParams = new HoloLensCameraStream.CameraParameters();
            cameraParams.cameraResolutionHeight = resolution.height;
            cameraParams.cameraResolutionWidth  = resolution.width;
            cameraParams.frameRate                 = Mathf.RoundToInt(frameRate);
            cameraParams.pixelFormat               = (outputColorFormat == ColorFormat.GRAY) ? CapturePixelFormat.NV12 : CapturePixelFormat.BGRA32;
            cameraParams.rotateImage180Degrees     = false;
            cameraParams.enableHolograms           = false;
            cameraParams.enableVideoStabilization  = false;
            cameraParams.recordingIndicatorVisible = false;

            return(cameraParams);
        }
예제 #9
0
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected override IEnumerator _Initialize()
        {
            if (hasInitDone)
            {
                ReleaseResources();

                if (onDisposed != null)
                {
                    onDisposed.Invoke();
                }
            }

            isInitWaiting = true;

            while (isChangeVideoModeWaiting)
            {
                yield return(null);
            }

            isChangeVideoModeWaiting = true;
            if (videoCapture != null)
            {
                videoCapture.StopVideoModeAsync(result1 => {
                    cameraParams = CreateCameraParams(videoCapture);
                    videoCapture.StartVideoModeAsync(cameraParams, result2 => {
                        if (!result2.success)
                        {
                            isChangeVideoModeWaiting = false;
                            isInitWaiting            = false;
                            CancelInitCoroutine();

                            if (onErrorOccurred != null)
                            {
                                onErrorOccurred.Invoke(ErrorCode.UNKNOWN);
                            }
                        }
                        else
                        {
                            isChangeVideoModeWaiting = false;
                        }
                    });
                });
            }
            else
            {
                //Fetch a pointer to Unity's spatial coordinate system if you need pixel mapping
                spatialCoordinateSystemPtr = UnityEngine.VR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr();

                HoloLensCameraStream.VideoCapture.CreateAync(videoCapture => {
                    if (initCoroutine == null)
                    {
                        return;
                    }

                    if (videoCapture == null)
                    {
                        Debug.LogError("Did not find a video capture object. You may not be using the HoloLens.");

                        isChangeVideoModeWaiting = false;
                        isInitWaiting            = false;
                        CancelInitCoroutine();

                        if (onErrorOccurred != null)
                        {
                            onErrorOccurred.Invoke(ErrorCode.CAMERA_DEVICE_NOT_EXIST);
                        }

                        return;
                    }

                    this.videoCapture = videoCapture;

                    //Request the spatial coordinate ptr if you want fetch the camera and set it if you need to
                    videoCapture.WorldOriginPtr = spatialCoordinateSystemPtr;

                    cameraParams = CreateCameraParams(videoCapture);

                    videoCapture.FrameSampleAcquired -= OnFrameSampleAcquired;
                    videoCapture.FrameSampleAcquired += OnFrameSampleAcquired;
                    videoCapture.StartVideoModeAsync(cameraParams, result => {
                        if (!result.success)
                        {
                            isChangeVideoModeWaiting = false;
                            isInitWaiting            = false;
                            CancelInitCoroutine();

                            if (onErrorOccurred != null)
                            {
                                onErrorOccurred.Invoke(ErrorCode.UNKNOWN);
                            }
                        }
                        else
                        {
                            isChangeVideoModeWaiting = false;
                        }
                    });
                });
            }

            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > _timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                else if (didUpdateThisFrame)
                {
                    Debug.Log("HololensCameraStreamToMatHelper:: " + "name:" + "" + " width:" + cameraParams.cameraResolutionWidth + " height:" + cameraParams.cameraResolutionHeight + " fps:" + cameraParams.frameRate);

                    if (colors == null || colors.Length != cameraParams.cameraResolutionWidth * cameraParams.cameraResolutionHeight)
                    {
                        colors = new Color32[cameraParams.cameraResolutionWidth * cameraParams.cameraResolutionHeight];
                    }

                    frameMat          = new Mat(cameraParams.cameraResolutionHeight, cameraParams.cameraResolutionWidth, CvType.CV_8UC4);
                    screenOrientation = Screen.orientation;

                    if (_requestedRotate90Degree)
                    {
                        rotatedFrameMat = new Mat(cameraParams.cameraResolutionWidth, cameraParams.cameraResolutionHeight, CvType.CV_8UC4);
                    }

                    isInitWaiting = false;
                    initCoroutine = null;

                    if (onInitialized != null)
                    {
                        onInitialized.Invoke();
                    }

                    hasInitDone = true;

                    break;
                }
                else
                {
                    initFrameCount++;
                    yield return(null);
                }
            }

            if (isTimeout)
            {
                if (videoCapture != null)
                {
                    videoCapture.FrameSampleAcquired -= OnFrameSampleAcquired;

                    isChangeVideoModeWaiting = true;
                    videoCapture.StopVideoModeAsync(result => {
                        videoCapture.Dispose();
                        videoCapture             = null;
                        isChangeVideoModeWaiting = false;
                    });

                    isInitWaiting = false;
                    initCoroutine = null;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                    }
                }
                else
                {
                    isInitWaiting = false;
                    initCoroutine = null;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                    }
                }
            }
        }
        /// <summary>
        /// Initializes this instance by coroutine.
        /// </summary>
        protected override IEnumerator _Initialize()
        {
            if (hasInitDone)
            {
                StartCoroutine(_Stop());

                while (isChangeVideoModeWaiting)
                {
                    yield return(null);
                }

                ReleaseResources();

                if (onDisposed != null)
                {
                    onDisposed.Invoke();
                }
            }

            isInitWaiting = true;

            while (isChangeVideoModeWaiting)
            {
                yield return(null);
            }

            isChangeVideoModeWaiting = true;
            if (videoCapture != null)
            {
                videoCapture.StopVideoModeAsync(result1 =>
                {
                    cameraParams = CreateCameraParams(videoCapture);
                    videoCapture.StartVideoModeAsync(cameraParams, result2 =>
                    {
                        if (!result2.success)
                        {
                            isChangeVideoModeWaiting = false;
                            isInitWaiting            = false;
                            CancelInitCoroutine();

                            if (onErrorOccurred != null)
                            {
                                onErrorOccurred.Invoke(ErrorCode.UNKNOWN);
                            }
                        }
                        else
                        {
                            isChangeVideoModeWaiting = false;
                        }
                    });
                });
            }
            else
            {
                //Fetch a pointer to Unity's spatial coordinate system if you need pixel mapping
#if UNITY_2017_2_OR_NEWER
                spatialCoordinateSystemPtr = UnityEngine.XR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr();
#else
                spatialCoordinateSystemPtr = UnityEngine.VR.WSA.WorldManager.GetNativeISpatialCoordinateSystemPtr();
#endif

                HoloLensCameraStream.VideoCapture.CreateAync(videoCapture =>
                {
                    if (initCoroutine == null)
                    {
                        return;
                    }

                    if (videoCapture == null)
                    {
                        Debug.LogError("Did not find a video capture object. You may not be using the HoloLens.");

                        isChangeVideoModeWaiting = false;
                        isInitWaiting            = false;
                        CancelInitCoroutine();

                        if (onErrorOccurred != null)
                        {
                            onErrorOccurred.Invoke(ErrorCode.CAMERA_DEVICE_NOT_EXIST);
                        }

                        return;
                    }

                    this.videoCapture = videoCapture;

                    //Request the spatial coordinate ptr if you want fetch the camera and set it if you need to
                    videoCapture.WorldOriginPtr = spatialCoordinateSystemPtr;

                    cameraParams = CreateCameraParams(videoCapture);

                    videoCapture.FrameSampleAcquired -= OnFrameSampleAcquired;
                    videoCapture.FrameSampleAcquired += OnFrameSampleAcquired;
                    videoCapture.StartVideoModeAsync(cameraParams, result =>
                    {
                        if (!result.success)
                        {
                            isChangeVideoModeWaiting = false;
                            isInitWaiting            = false;
                            CancelInitCoroutine();

                            if (onErrorOccurred != null)
                            {
                                onErrorOccurred.Invoke(ErrorCode.UNKNOWN);
                            }
                        }
                        else
                        {
                            isChangeVideoModeWaiting = false;
                        }
                    });
                });
            }

            int  initFrameCount = 0;
            bool isTimeout      = false;

            while (true)
            {
                if (initFrameCount > _timeoutFrameCount)
                {
                    isTimeout = true;
                    break;
                }
                else if (didUpdateThisFrame)
                {
                    Debug.Log("HololensCameraStreamToMatHelper:: " + "name:" + "" + " width:" + cameraParams.cameraResolutionWidth + " height:" + cameraParams.cameraResolutionHeight + " fps:" + cameraParams.frameRate);

                    baseColorFormat = (outputColorFormat == ColorFormat.GRAY) ? ColorFormat.GRAY : ColorFormat.BGRA;

                    baseMat = new Mat(cameraParams.cameraResolutionHeight, cameraParams.cameraResolutionWidth, CvType.CV_8UC(Channels(baseColorFormat)));

                    if (baseColorFormat == outputColorFormat)
                    {
                        frameMat = baseMat;
                    }
                    else
                    {
                        frameMat = new Mat(baseMat.rows(), baseMat.cols(), CvType.CV_8UC(Channels(outputColorFormat)));
                    }

                    if (_rotate90Degree)
                    {
                        rotatedFrameMat = new Mat(frameMat.cols(), frameMat.rows(), CvType.CV_8UC(Channels(outputColorFormat)));
                    }

                    isInitWaiting = false;
                    hasInitDone   = true;
                    initCoroutine = null;

                    if (onInitialized != null)
                    {
                        onInitialized.Invoke();
                    }

                    hasInitEventCompleted = true;

                    break;
                }
                else
                {
                    initFrameCount++;
                    yield return(null);
                }
            }

            if (isTimeout)
            {
                if (videoCapture != null)
                {
                    videoCapture.FrameSampleAcquired -= OnFrameSampleAcquired;

                    isChangeVideoModeWaiting = true;
                    videoCapture.StopVideoModeAsync(result =>
                    {
                        videoCapture.Dispose();
                        videoCapture             = null;
                        isChangeVideoModeWaiting = false;
                    });

                    isInitWaiting = false;
                    initCoroutine = null;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                    }
                }
                else
                {
                    isInitWaiting = false;
                    initCoroutine = null;

                    if (onErrorOccurred != null)
                    {
                        onErrorOccurred.Invoke(ErrorCode.TIMEOUT);
                    }
                }
            }
        }