using OpenCVForUnity.CoreModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.UnityUtils; using OpenCVForUnity.UnityUtils.Helper; using System; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; namespace OpenCVForUnityExample { /// /// WebCamTextureToMatHelper Example /// [RequireComponent(typeof(WebCamTextureToMatHelper))] public class WebCamTextureToMatHelperExample : MonoBehaviour { /// /// The requested resolution dropdown. /// public Dropdown requestedResolutionDropdown; /// /// The requested resolution. /// public ResolutionPreset requestedResolution = ResolutionPreset._640x480; /// /// The requestedFPS dropdown. /// public Dropdown requestedFPSDropdown; /// /// The requestedFPS. /// public FPSPreset requestedFPS = FPSPreset._30; /// /// The rotate 90 degree toggle. /// public Toggle rotate90DegreeToggle; /// /// The flip vertical toggle. /// public Toggle flipVerticalToggle; /// /// The flip horizontal toggle. /// public Toggle flipHorizontalToggle; /// /// The texture. /// Texture2D texture; /// /// The webcam texture to mat helper. /// WebCamTextureToMatHelper webCamTextureToMatHelper; /// /// The FPS monitor. /// FpsMonitor fpsMonitor; // Use this for initialization void Start() { fpsMonitor = GetComponent(); // Get the WebCamTextureToMatHelper component attached to the current game object webCamTextureToMatHelper = gameObject.GetComponent(); // Set the requested width, height, FPS and ColorFormat int width, height; Dimensions(requestedResolution, out width, out height); webCamTextureToMatHelper.requestedWidth = width; webCamTextureToMatHelper.requestedHeight = height; webCamTextureToMatHelper.requestedFPS = (int)requestedFPS; webCamTextureToMatHelper.outputColorFormat = WebCamTextureToMatHelper.ColorFormat.RGBA; // Initialize the webcam texture to Mat helper, which starts the webcam and prepares the conversion webCamTextureToMatHelper.Initialize(); // Update GUI state requestedResolutionDropdown.value = (int)requestedResolution; string[] enumNames = System.Enum.GetNames(typeof(FPSPreset)); int index = Array.IndexOf(enumNames, requestedFPS.ToString()); requestedFPSDropdown.value = index; rotate90DegreeToggle.isOn = webCamTextureToMatHelper.rotate90Degree; flipVerticalToggle.isOn = webCamTextureToMatHelper.flipVertical; flipHorizontalToggle.isOn = webCamTextureToMatHelper.flipHorizontal; } /// /// Raises the webcam texture to mat helper initialized event. /// public void OnWebCamTextureToMatHelperInitialized() { Debug.Log("OnWebCamTextureToMatHelperInitialized"); // Retrieve the current frame from the WebCamTextureToMatHelper as a Mat object Mat webCamTextureMat = webCamTextureToMatHelper.GetMat(); // Create a new Texture2D with the same dimensions as the Mat and RGBA32 color format texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false); // Convert the Mat to a Texture2D, effectively transferring the image data Utils.matToTexture2D(webCamTextureMat, texture); // Set the Texture2D as the main texture of the Renderer component attached to the game object gameObject.GetComponent().material.mainTexture = texture; // Adjust the scale of the game object to match the dimensions of the texture gameObject.transform.localScale = new Vector3(webCamTextureMat.cols(), webCamTextureMat.rows(), 1); Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); if (fpsMonitor != null) { fpsMonitor.Add("deviceName", webCamTextureToMatHelper.GetDeviceName().ToString()); fpsMonitor.Add("width", webCamTextureToMatHelper.GetWidth().ToString()); fpsMonitor.Add("height", webCamTextureToMatHelper.GetHeight().ToString()); fpsMonitor.Add("videoRotationAngle", webCamTextureToMatHelper.GetWebCamTexture().videoRotationAngle.ToString()); fpsMonitor.Add("videoVerticallyMirrored", webCamTextureToMatHelper.GetWebCamTexture().videoVerticallyMirrored.ToString()); fpsMonitor.Add("camera fps", webCamTextureToMatHelper.GetFPS().ToString()); fpsMonitor.Add("isFrontFacing", webCamTextureToMatHelper.IsFrontFacing().ToString()); fpsMonitor.Add("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString()); fpsMonitor.Add("flipVertical", webCamTextureToMatHelper.flipVertical.ToString()); fpsMonitor.Add("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString()); fpsMonitor.Add("orientation", Screen.orientation.ToString()); } // Get the width and height of the webCamTextureMat float width = webCamTextureMat.width(); float height = webCamTextureMat.height(); // Calculate the scale factors for width and height based on the screen dimensions float widthScale = (float)Screen.width / width; float heightScale = (float)Screen.height / height; // Adjust the orthographic size of the main Camera to fit the aspect ratio of the image if (widthScale < heightScale) { // If the width scale is smaller, adjust the orthographic size based on width and screen height Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2; } else { // If the height scale is smaller or equal, adjust the orthographic size based on height Camera.main.orthographicSize = height / 2; } } /// /// Raises the webcam texture to mat helper disposed event. /// public void OnWebCamTextureToMatHelperDisposed() { Debug.Log("OnWebCamTextureToMatHelperDisposed"); // Destroy the texture and set it to null if (texture != null) { Texture2D.Destroy(texture); texture = null; } } /// /// Raises the webcam texture to mat helper error occurred event. /// /// Error code. public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode) { Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode); if (fpsMonitor != null) { fpsMonitor.consoleText = "ErrorCode: " + errorCode; } } // Update is called once per frame void Update() { // Check if the web camera is playing and if a new frame was updated if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) { // Retrieve the current frame as a Mat object Mat rgbaMat = webCamTextureToMatHelper.GetMat(); // Add text overlay on the frame //Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false); // Convert the Mat to a Texture2D to display it on a texture Utils.matToTexture2D(rgbaMat, texture); } } /// /// Raises the destroy event. /// void OnDestroy() { // Dispose of the webCamTextureToMatHelper object and release any resources held by it. webCamTextureToMatHelper.Dispose(); } /// /// Raises the back button click event. /// public void OnBackButtonClick() { // Load the specified scene when the back button is clicked SceneManager.LoadScene("OpenCVForUnityExample"); } /// /// Raises the play button click event. /// public void OnPlayButtonClick() { webCamTextureToMatHelper.Play(); } /// /// Raises the pause button click event. /// public void OnPauseButtonClick() { webCamTextureToMatHelper.Pause(); } /// /// Raises the stop button click event. /// public void OnStopButtonClick() { webCamTextureToMatHelper.Stop(); } /// /// Raises the change camera button click event. /// public void OnChangeCameraButtonClick() { webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.requestedIsFrontFacing; } /// /// Raises the requested resolution dropdown value changed event. /// public void OnRequestedResolutionDropdownValueChanged(int result) { if ((int)requestedResolution != result) { requestedResolution = (ResolutionPreset)result; int width, height; Dimensions(requestedResolution, out width, out height); webCamTextureToMatHelper.Initialize(width, height); } } /// /// Raises the requestedFPS dropdown value changed event. /// public void OnRequestedFPSDropdownValueChanged(int result) { string[] enumNames = Enum.GetNames(typeof(FPSPreset)); int value = (int)System.Enum.Parse(typeof(FPSPreset), enumNames[result], true); if ((int)requestedFPS != value) { requestedFPS = (FPSPreset)value; webCamTextureToMatHelper.requestedFPS = (int)requestedFPS; } } /// /// Raises the rotate 90 degree toggle value changed event. /// public void OnRotate90DegreeToggleValueChanged() { if (rotate90DegreeToggle.isOn != webCamTextureToMatHelper.rotate90Degree) { webCamTextureToMatHelper.rotate90Degree = rotate90DegreeToggle.isOn; if (fpsMonitor != null) fpsMonitor.Add("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString()); } } /// /// Raises the flip vertical toggle value changed event. /// public void OnFlipVerticalToggleValueChanged() { if (flipVerticalToggle.isOn != webCamTextureToMatHelper.flipVertical) { webCamTextureToMatHelper.flipVertical = flipVerticalToggle.isOn; if (fpsMonitor != null) fpsMonitor.Add("flipVertical", webCamTextureToMatHelper.flipVertical.ToString()); } } /// /// Raises the flip horizontal toggle value changed event. /// public void OnFlipHorizontalToggleValueChanged() { if (flipHorizontalToggle.isOn != webCamTextureToMatHelper.flipHorizontal) { webCamTextureToMatHelper.flipHorizontal = flipHorizontalToggle.isOn; if (fpsMonitor != null) fpsMonitor.Add("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString()); } } public enum FPSPreset : int { _0 = 0, _1 = 1, _5 = 5, _10 = 10, _15 = 15, _30 = 30, _60 = 60, } public enum ResolutionPreset : byte { _50x50 = 0, _640x480, _1280x720, _1920x1080, _9999x9999, } private void Dimensions(ResolutionPreset preset, out int width, out int height) { switch (preset) { case ResolutionPreset._50x50: width = 50; height = 50; break; case ResolutionPreset._640x480: width = 640; height = 480; break; case ResolutionPreset._1280x720: width = 1280; height = 720; break; case ResolutionPreset._1920x1080: width = 1920; height = 1080; break; case ResolutionPreset._9999x9999: width = 9999; height = 9999; break; default: width = height = 0; break; } } } }