diff --git a/Assets/Scripts/UI/Component/CaptureManagerBase.cs b/Assets/Scripts/UI/Component/CaptureManagerBase.cs index 59b73c3..e82917b 100644 --- a/Assets/Scripts/UI/Component/CaptureManagerBase.cs +++ b/Assets/Scripts/UI/Component/CaptureManagerBase.cs @@ -55,6 +55,8 @@ namespace Yoga if (YogaManager.Instance.CurrentEstimator.Check(ref img))//检测模型,将错误信息打印在图片上 { + if (YogaManager.Instance.RgbaMat != null) + YogaManager.Instance.RgbaMat.Dispose(); YogaManager.Instance.RgbaMat = img.clone(); //打印 diff --git a/Assets/Scripts/UI/Component/MotionUSBCameraCaptureManager.cs b/Assets/Scripts/UI/Component/MotionUSBCameraCaptureManager.cs index fd0471a..9292459 100644 --- a/Assets/Scripts/UI/Component/MotionUSBCameraCaptureManager.cs +++ b/Assets/Scripts/UI/Component/MotionUSBCameraCaptureManager.cs @@ -3,6 +3,7 @@ using OpenCVForUnity.UnityUtils; using Serenegiant.UVC; using System; +using System.Collections; using UnityEngine; @@ -10,10 +11,29 @@ namespace Yoga { public class MotionUSBCameraCaptureManager : CaptureManagerBase, IUVCDrawer { + private Material _flipImg; + private void Awake() + { + _flipImg = Resources.Load("Materials/Unlit_FlipHorizontal"); + InvokeRepeating("GCCollection", 0, 1f); + } + + private void GCCollection() + { + StartCoroutine(CleanUp()); + } + private IEnumerator CleanUp() + { + GC.Collect(); + yield return Resources.UnloadUnusedAssets(); + GC.Collect(); + } + protected override Mat GetMat() { Mat img = null; + LogPrint.Log("USB Camera!", PrintLevel.Normal); if (UVCManager.Instance != null && UVCManager.Instance.GetAttachedDevices() != null && UVCManager.Instance.GetAttachedDevices().Count > 0) { @@ -21,20 +41,19 @@ namespace Yoga for (int i = 0; i < devices.Count; i++) { UVCManager.CameraInfo device = devices[i]; - LogPrint.Log($"id:{i},pid:{device.Pid},vid:{device.Vid},deviceName:{device.DeviceName}"); + LogPrint.Log($"id:{i},pid:{device.Pid},vid:{device.Vid},deviceName:{device.DeviceName}", PrintLevel.Normal); } - Texture picCaptured = null; - var index = PlayerPrefs.GetInt("USBCameraIndex"); - if (index >= devices.Count) //数量超出时候改为默认第一个 - index = 0; + //var index = PlayerPrefs.GetInt("USBCameraIndex"); + //if (index >= devices.Count || index < 0) //数量超出时候改为默认第一个 + // index = 0; - picCaptured = devices[index].previewTexture; - if (picCaptured == null) + var device1 = devices.Find(d => d.DeviceName.Equals("/dev/bus/usb/002/008")); + + if (device1.previewTexture == null) throw new Exception("No preview Texture found!"); - Texture2D tmpTex = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight); - Utils.textureToTexture2D(picCaptured, tmpTex); - tmpTex = PictureUtility.HorizontalFlipTexture(tmpTex);//picCaptured texture水平反转 + var tmpTex = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight); + textureToFlipTexture2D(device1.previewTexture, tmpTex); img = new Mat(UVCManager.Instance.DefaultHeight, UVCManager.Instance.DefaultWidth, CvType.CV_8UC3); Utils.texture2DToMat(tmpTex, img); } @@ -42,12 +61,34 @@ namespace Yoga return img; } + private void textureToFlipTexture2D(Texture texture, Texture2D texture2D) + { + if (texture == null) + throw new ArgumentNullException("texture"); + + if (texture2D == null) + throw new ArgumentNullException("texture2D"); + + if (texture.width != texture2D.width || texture.height != texture2D.height) + throw new ArgumentException("texture and texture2D need to be the same size."); + + RenderTexture prevRT = RenderTexture.active; + + RenderTexture tempRT = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32); + Graphics.Blit(texture, tempRT, _flipImg); + + RenderTexture.active = tempRT; + texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false); + texture2D.Apply(false, false); + RenderTexture.ReleaseTemporary(tempRT); + + RenderTexture.active = prevRT; + } + protected override void MatInitial() { if (UVCManager.Instance == null) throw new Exception("没有连接USB摄像头"); - - Debug.LogWarning("USB Camera!"); } #region UVC