197 lines
6.3 KiB
C#
197 lines
6.3 KiB
C#
using OpenCVCompact;
|
|
using Serenegiant.UVC;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class USBFaceDetectManager : FaceDetectManagerBase, IUVCDrawer
|
|
{
|
|
public RawImage image;
|
|
public RawImage cutImage;
|
|
private Material _flipImg;
|
|
private int[] _positionRect;
|
|
private Texture2D _cutTexture;
|
|
protected override void InitMatHelper()
|
|
{
|
|
_flipImg = new Material(Shader.Find("Unlit/FlipHorizontal")); ;
|
|
InvokeRepeating("GCCollection", 0, 1f);
|
|
}
|
|
|
|
protected void GCCollection()
|
|
{
|
|
if (enabled == true && gameObject.activeInHierarchy == true)
|
|
StartCoroutine(CleanUp());
|
|
}
|
|
protected IEnumerator CleanUp()
|
|
{
|
|
GC.Collect();
|
|
yield return Resources.UnloadUnusedAssets();
|
|
GC.Collect();
|
|
}
|
|
|
|
protected override Mat GetMat()
|
|
{
|
|
Mat img = null;
|
|
if (UVCManager.Instance != null &&
|
|
UVCManager.Instance.GetAttachedDevices() != null && UVCManager.Instance.GetAttachedDevices().Count > 0)
|
|
{
|
|
var devices = UVCManager.Instance.GetAttachedDevices();
|
|
for (int i = 0; i < devices.Count; i++)
|
|
{
|
|
UVCManager.CameraInfo device = devices[i];
|
|
//LogPrint.Log($"device: {device}, device name:{device.DeviceName} texture: {device.previewTexture}");
|
|
}
|
|
//image.texture = devices.FirstOrDefault().previewTexture;
|
|
textureToFlipTexture2D(devices.FirstOrDefault().previewTexture, videoTexture);
|
|
|
|
img = new Mat(UVCManager.Instance.DefaultHeight, UVCManager.Instance.DefaultWidth, CvType.CV_8UC4);
|
|
Utils.texture2DToMat(videoTexture, 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 AnalyzingFace()
|
|
//{
|
|
// mat4Display = GetMat();
|
|
// if (mat4Display == null)
|
|
// return;
|
|
// //mat4Display.copyTo(mat4Process);
|
|
// var roiRect = new OpenCVCompact.Rect(
|
|
// _positionRect[0],
|
|
// _positionRect[1],
|
|
// Mathf.Abs(_positionRect[2] - _positionRect[0]),
|
|
// Mathf.Abs(_positionRect[3] - _positionRect[1]));
|
|
|
|
// mat4Process = new Mat(mat4Display, roiRect);
|
|
|
|
|
|
// //Face Detect
|
|
// panel.RefreshData(LandmarkDetect());
|
|
|
|
|
|
|
|
// Imgproc.rectangle(mat4Display, roiRect, new Scalar(255, 0, 0), 2);
|
|
// DebugPint();
|
|
//}
|
|
|
|
protected override void DebugPint()
|
|
{
|
|
//image.texture = videoTexture;
|
|
if (mat4Process.rows() == _cutTexture.height)
|
|
{
|
|
LogPrint.Log("mat4Process.rows() " + mat4Process.rows() + " mat4Process.cols() " + mat4Process.cols());
|
|
Mat cutMat = mat4Process.clone();
|
|
|
|
LogPrint.Log($"mat4Display type:{mat4Display.type()}.{CvType.CV_8UC3},{CvType.CV_8UC4}");
|
|
|
|
//Utils.matToTexture2D(cutMat, _cutTexture);
|
|
}
|
|
|
|
if (mat4Display.rows() == videoTexture.height)
|
|
{
|
|
//mat4Display.copyTo(mat4DisplayTexture);
|
|
Mat display = mat4Display.clone();
|
|
//Utils.matToTexture2D(display, videoTexture);
|
|
}
|
|
}
|
|
|
|
protected override void DestroyManager()
|
|
{
|
|
}
|
|
|
|
#region UVC
|
|
public UVCFilter[] UVCFilters;
|
|
private Texture SavedTexture;
|
|
public bool OnUVCAttachEvent(UVCManager manager, UVCDevice device)
|
|
{
|
|
var result = !device.IsRicoh || device.IsTHETA;
|
|
|
|
result &= UVCFilter.Match(device, UVCFilters);
|
|
|
|
return result;
|
|
}
|
|
|
|
public void OnUVCDetachEvent(UVCManager manager, UVCDevice device)
|
|
{
|
|
LogPrint.Log("OnUVCDetachEvent " + device);
|
|
}
|
|
|
|
public bool CanDraw(UVCManager manager, UVCDevice device)
|
|
{
|
|
return UVCFilter.Match(device, UVCFilters);
|
|
}
|
|
|
|
public void OnUVCStartEvent(UVCManager manager, UVCDevice device, Texture tex)
|
|
{
|
|
HandleOnStartPreview(tex);
|
|
}
|
|
|
|
public void OnUVCStopEvent(UVCManager manager, UVCDevice device)
|
|
{
|
|
HandleOnStopPreview();
|
|
}
|
|
|
|
private void HandleOnStartPreview(Texture tex)
|
|
{
|
|
SavedTexture = videoTexture;
|
|
|
|
videoTexture = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight, TextureFormat.RGBA32, false);
|
|
_positionRect = PictureUtility.GetPositionRect(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight);
|
|
_cutTexture = new Texture2D(_positionRect[2], _positionRect[3], TextureFormat.RGBA32, false);
|
|
image.texture = videoTexture;
|
|
cutImage.texture = _cutTexture;
|
|
|
|
//mat4Display = new Mat(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight, CvType.CV_8UC4);
|
|
mat4DisplayTexture = new Mat(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight, CvType.CV_8UC4);
|
|
|
|
LogPrint.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
|
|
|
|
dnnUtils.InitHeadPoseEstimationCameraInfo(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight);
|
|
}
|
|
private void HandleOnStopPreview()
|
|
{
|
|
RestoreTexture();
|
|
}
|
|
|
|
private void RestoreTexture()
|
|
{
|
|
try
|
|
{
|
|
transform.GetComponent<Renderer>().material.mainTexture = SavedTexture;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
}
|
|
SavedTexture = null;
|
|
}
|
|
#endregion
|
|
}
|