2023-11-28 19:05:48 +00:00
|
|
|
|
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;
|
2023-11-29 07:59:38 +00:00
|
|
|
|
public int[] PositionRect
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_positionRect == null)
|
|
|
|
|
_positionRect = PictureUtility.GetPositionRect(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight);
|
|
|
|
|
return _positionRect;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 19:05:48 +00:00
|
|
|
|
protected override void InitMatHelper()
|
|
|
|
|
{
|
|
|
|
|
_flipImg = new Material(Shader.Find("Unlit/FlipHorizontal")); ;
|
|
|
|
|
InvokeRepeating("GCCollection", 0, 1f);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
2023-11-29 09:59:42 +00:00
|
|
|
|
//摄像头问题 需要反转
|
|
|
|
|
GlobalData.Instance.IsFlip = true;
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
mat4Display = new Mat(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight, CvType.CV_8UC4);
|
|
|
|
|
mat4Process = new Mat(PositionRect[2] - PositionRect[0], PositionRect[3] - PositionRect[1], CvType.CV_8UC4);
|
|
|
|
|
mat4DisplayTexture = new Mat(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight, CvType.CV_8UC4);
|
|
|
|
|
|
|
|
|
|
_videoTexture = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight, TextureFormat.RGBA32, false);
|
|
|
|
|
_cutTexture = new Texture2D(PositionRect[2] - PositionRect[0], PositionRect[3] - PositionRect[1], TextureFormat.RGBA32, false);
|
|
|
|
|
|
|
|
|
|
image.texture = _videoTexture;
|
|
|
|
|
cutImage.texture = _cutTexture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
2023-11-29 09:59:42 +00:00
|
|
|
|
//摄像头问题,需要反转
|
|
|
|
|
GlobalData.Instance.IsFlip = false;
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
Destroy(_videoTexture);
|
|
|
|
|
Destroy(_cutTexture);
|
|
|
|
|
_videoTexture = null;
|
|
|
|
|
_cutTexture = null;
|
|
|
|
|
mat4Display.Dispose();
|
|
|
|
|
mat4Process.Dispose();
|
|
|
|
|
mat4DisplayTexture.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 19:05:48 +00:00
|
|
|
|
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()
|
|
|
|
|
{
|
2023-11-29 07:59:38 +00:00
|
|
|
|
return null;
|
2023-11-28 19:05:48 +00:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 09:59:42 +00:00
|
|
|
|
private void textureToTargetPosTexture2D(Texture texture, Texture2D texture2D, bool isFlip)
|
2023-11-29 07:59:38 +00:00
|
|
|
|
{
|
|
|
|
|
if (texture == null)
|
|
|
|
|
throw new ArgumentNullException("texture");
|
|
|
|
|
|
|
|
|
|
if (texture2D == null)
|
|
|
|
|
throw new ArgumentNullException("texture2D");
|
|
|
|
|
|
|
|
|
|
if (PositionRect[2] - PositionRect[0] > texture.width || PositionRect[3] - PositionRect[1] > texture.height)
|
|
|
|
|
{
|
|
|
|
|
LogPrint.Error($"{PositionRect[0]},{PositionRect[1]},{texture.width}||{PositionRect[2]},{PositionRect[3]},{texture.height}");
|
|
|
|
|
throw new ArgumentException("取值框超过目标像素最大范围");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RenderTexture prevRT = RenderTexture.active;
|
|
|
|
|
RenderTexture tempRT = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32);
|
2023-11-29 09:59:42 +00:00
|
|
|
|
if (isFlip)
|
|
|
|
|
Graphics.Blit(texture, tempRT, _flipImg);
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
Graphics.Blit(texture, tempRT);
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
|
|
|
|
|
RenderTexture.active = tempRT;
|
|
|
|
|
texture2D.ReadPixels(new UnityEngine.Rect(PositionRect[0], PositionRect[1], PositionRect[2] - PositionRect[0], PositionRect[3] - PositionRect[1]), 0, 0, false);
|
|
|
|
|
texture2D.Apply(false, false);
|
|
|
|
|
RenderTexture.ReleaseTemporary(tempRT);
|
|
|
|
|
|
|
|
|
|
RenderTexture.active = prevRT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void AnalyzingFace()
|
|
|
|
|
{
|
|
|
|
|
if (UVCManager.Instance == null || UVCManager.Instance.GetAttachedDevices() == null || UVCManager.Instance.GetAttachedDevices().Count <= 0)
|
|
|
|
|
return;
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
if (image.gameObject.activeInHierarchy != GlobalData.Instance.ShowFaceDetectCameraCaptureImage)
|
|
|
|
|
image.gameObject.SetActive(GlobalData.Instance.ShowFaceDetectCameraCaptureImage);
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
if (cutImage.gameObject.activeInHierarchy != GlobalData.Instance.ShowFaceDetectCameraCaptureImage)
|
|
|
|
|
cutImage.gameObject.SetActive(GlobalData.Instance.ShowFaceDetectCameraCaptureImage);
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
var devices = UVCManager.Instance.GetAttachedDevices();
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
2023-11-29 09:59:42 +00:00
|
|
|
|
if (GlobalData.Instance.IsFlip)
|
|
|
|
|
{
|
|
|
|
|
textureToFlipTexture2D(devices.FirstOrDefault().previewTexture, _videoTexture);
|
|
|
|
|
textureToTargetPosTexture2D(devices.FirstOrDefault().previewTexture, _cutTexture, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Utils.textureToTexture2D(devices.FirstOrDefault().previewTexture, _videoTexture);
|
|
|
|
|
textureToTargetPosTexture2D(devices.FirstOrDefault().previewTexture, _cutTexture, false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
Utils.texture2DToMat(_videoTexture, mat4Display);
|
|
|
|
|
Utils.texture2DToMat(_cutTexture, mat4Process);
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
panel.RefreshData(LandmarkDetect());//Face Detect
|
|
|
|
|
OpenCVCompact.Rect roiRect = new OpenCVCompact.Rect(PositionRect[0], PositionRect[1], PositionRect[2] - PositionRect[0], PositionRect[3] - PositionRect[1]);
|
|
|
|
|
Imgproc.rectangle(mat4Display, roiRect, new Scalar(255, 0, 0), 2);
|
|
|
|
|
//DebugPint();
|
|
|
|
|
}
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
|
|
|
|
protected override void DebugPint()
|
|
|
|
|
{
|
|
|
|
|
//image.texture = videoTexture;
|
|
|
|
|
if (mat4Process.rows() == _cutTexture.height)
|
|
|
|
|
{
|
|
|
|
|
Mat cutMat = mat4Process.clone();
|
2023-11-29 07:59:38 +00:00
|
|
|
|
Utils.matToTexture2D(cutMat, _cutTexture);
|
2023-11-28 19:05:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
if (mat4Display.rows() == _videoTexture.height)
|
2023-11-28 19:05:48 +00:00
|
|
|
|
{
|
|
|
|
|
Mat display = mat4Display.clone();
|
2023-11-29 07:59:38 +00:00
|
|
|
|
Utils.matToTexture2D(display, _videoTexture);
|
2023-11-28 19:05:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
protected override void Dispose()
|
2023-11-28 19:05:48 +00:00
|
|
|
|
{
|
2023-11-29 07:59:38 +00:00
|
|
|
|
|
2023-11-28 19:05:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region UVC
|
|
|
|
|
public UVCFilter[] UVCFilters;
|
|
|
|
|
private Texture SavedTexture;
|
2023-11-29 07:59:38 +00:00
|
|
|
|
|
|
|
|
|
|
2023-11-28 19:05:48 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogException(e);
|
|
|
|
|
}
|
|
|
|
|
SavedTexture = null;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|