This commit is contained in:
terric 2023-11-26 13:14:22 +08:00
parent 4b32e29d93
commit fe78e3cbe8
2 changed files with 55 additions and 12 deletions

View File

@ -55,6 +55,8 @@ namespace Yoga
if (YogaManager.Instance.CurrentEstimator.Check(ref img))//检测模型,将错误信息打印在图片上 if (YogaManager.Instance.CurrentEstimator.Check(ref img))//检测模型,将错误信息打印在图片上
{ {
if (YogaManager.Instance.RgbaMat != null)
YogaManager.Instance.RgbaMat.Dispose();
YogaManager.Instance.RgbaMat = img.clone(); YogaManager.Instance.RgbaMat = img.clone();
//打印 //打印

View File

@ -3,6 +3,7 @@ using OpenCVForUnity.UnityUtils;
using Serenegiant.UVC; using Serenegiant.UVC;
using System; using System;
using System.Collections;
using UnityEngine; using UnityEngine;
@ -10,10 +11,29 @@ namespace Yoga
{ {
public class MotionUSBCameraCaptureManager : CaptureManagerBase, IUVCDrawer public class MotionUSBCameraCaptureManager : CaptureManagerBase, IUVCDrawer
{ {
private Material _flipImg;
private void Awake()
{
_flipImg = Resources.Load<Material>("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() protected override Mat GetMat()
{ {
Mat img = null; Mat img = null;
LogPrint.Log("USB Camera!", PrintLevel.Normal);
if (UVCManager.Instance != null && if (UVCManager.Instance != null &&
UVCManager.Instance.GetAttachedDevices() != null && UVCManager.Instance.GetAttachedDevices().Count > 0) UVCManager.Instance.GetAttachedDevices() != null && UVCManager.Instance.GetAttachedDevices().Count > 0)
{ {
@ -21,20 +41,19 @@ namespace Yoga
for (int i = 0; i < devices.Count; i++) for (int i = 0; i < devices.Count; i++)
{ {
UVCManager.CameraInfo device = devices[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");
var index = PlayerPrefs.GetInt("USBCameraIndex"); //if (index >= devices.Count || index < 0) //数量超出时候改为默认第一个
if (index >= devices.Count) //数量超出时候改为默认第一个 // index = 0;
index = 0;
picCaptured = devices[index].previewTexture; var device1 = devices.Find(d => d.DeviceName.Equals("/dev/bus/usb/002/008"));
if (picCaptured == null)
if (device1.previewTexture == null)
throw new Exception("No preview Texture found!"); throw new Exception("No preview Texture found!");
Texture2D tmpTex = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight); var tmpTex = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight);
Utils.textureToTexture2D(picCaptured, tmpTex); textureToFlipTexture2D(device1.previewTexture, tmpTex);
tmpTex = PictureUtility.HorizontalFlipTexture(tmpTex);//picCaptured texture水平反转
img = new Mat(UVCManager.Instance.DefaultHeight, UVCManager.Instance.DefaultWidth, CvType.CV_8UC3); img = new Mat(UVCManager.Instance.DefaultHeight, UVCManager.Instance.DefaultWidth, CvType.CV_8UC3);
Utils.texture2DToMat(tmpTex, img); Utils.texture2DToMat(tmpTex, img);
} }
@ -42,12 +61,34 @@ namespace Yoga
return img; 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() protected override void MatInitial()
{ {
if (UVCManager.Instance == null) if (UVCManager.Instance == null)
throw new Exception("没有连接USB摄像头"); throw new Exception("没有连接USB摄像头");
Debug.LogWarning("USB Camera!");
} }
#region UVC #region UVC