2023-11-28 19:05:48 +00:00
|
|
|
|
using OpenCVCompact;
|
|
|
|
|
using Serenegiant.UVC;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
[RequireComponent(typeof(OpenCVCompact.WebCamTextureToMatHelper))]
|
|
|
|
|
public class WebCamFaceDetectManager : FaceDetectManagerBase
|
|
|
|
|
{
|
|
|
|
|
public RawImage image;
|
|
|
|
|
public RawImage cutImage;
|
|
|
|
|
bool webCamReady = false;
|
|
|
|
|
private int[] _positionRect;
|
2023-11-29 07:59:38 +00:00
|
|
|
|
//private Texture2D _cutTexture;
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
|
|
|
|
//Image Capture
|
|
|
|
|
OpenCVCompact.WebCamTextureToMatHelper _webCamTextureToMatHelper;
|
|
|
|
|
protected override void InitMatHelper()
|
|
|
|
|
{
|
|
|
|
|
_webCamTextureToMatHelper = gameObject.GetComponent<OpenCVCompact.WebCamTextureToMatHelper>();
|
|
|
|
|
_webCamTextureToMatHelper.Initialize();
|
|
|
|
|
}
|
|
|
|
|
protected override Mat GetMat()
|
|
|
|
|
{
|
|
|
|
|
if (!_webCamTextureToMatHelper.IsPlaying() || !_webCamTextureToMatHelper.DidUpdateThisFrame())
|
|
|
|
|
return null;
|
|
|
|
|
return _webCamTextureToMatHelper.GetMat();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
protected override void Dispose()
|
2023-11-28 19:05:48 +00:00
|
|
|
|
{
|
|
|
|
|
_webCamTextureToMatHelper.Dispose();
|
|
|
|
|
}
|
|
|
|
|
protected override void DebugPint()
|
|
|
|
|
{
|
|
|
|
|
if (webCamReady == true)
|
|
|
|
|
{
|
2023-11-29 07:59:38 +00:00
|
|
|
|
if (mat4Display.rows() == _videoTexture.height)
|
2023-11-28 19:05:48 +00:00
|
|
|
|
{
|
|
|
|
|
mat4Display.copyTo(mat4DisplayTexture);
|
2023-11-29 07:59:38 +00:00
|
|
|
|
Utils.matToTexture2D(mat4DisplayTexture, _videoTexture);
|
2023-11-28 19:05:48 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (mat4Process.rows() == cutImage.texture.height)
|
|
|
|
|
{
|
|
|
|
|
Mat cutMat = mat4Process.clone();
|
|
|
|
|
Texture2D cutTexture = new Texture2D(cutMat.cols(), cutMat.rows(), TextureFormat.RGBA32, false);
|
|
|
|
|
Utils.matToTexture2D(cutMat, cutTexture);
|
|
|
|
|
cutImage.texture = cutTexture;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void OnWebCamTextureToMatHelperInitialized()
|
|
|
|
|
{
|
|
|
|
|
Mat webCamTextureMat = _webCamTextureToMatHelper.GetMat();
|
|
|
|
|
|
2023-11-29 07:59:38 +00:00
|
|
|
|
_videoTexture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false);
|
2023-11-28 19:05:48 +00:00
|
|
|
|
//gameObject.GetComponent<Renderer>().material.mainTexture = videoTexture;
|
2023-11-29 07:59:38 +00:00
|
|
|
|
image.texture = _videoTexture;
|
2023-11-28 19:05:48 +00:00
|
|
|
|
_positionRect = PictureUtility.GetPositionRect(webCamTextureMat.cols(), webCamTextureMat.rows());
|
|
|
|
|
_cutTexture = new Texture2D(_positionRect[2], _positionRect[3], TextureFormat.RGBA32, false);
|
|
|
|
|
cutImage.texture = _cutTexture;
|
|
|
|
|
|
|
|
|
|
mat4Display = new Mat(webCamTextureMat.rows(), webCamTextureMat.cols(), CvType.CV_8UC4);
|
|
|
|
|
mat4DisplayTexture = new Mat(webCamTextureMat.rows(), webCamTextureMat.cols(), CvType.CV_8UC4);
|
|
|
|
|
|
|
|
|
|
LogPrint.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
|
|
|
|
|
|
|
|
|
|
float width = webCamTextureMat.width();
|
|
|
|
|
float height = webCamTextureMat.height();
|
|
|
|
|
|
|
|
|
|
float widthScale = (float)Screen.width / width;
|
|
|
|
|
float heightScale = (float)Screen.height / height;
|
|
|
|
|
if (widthScale < heightScale)
|
|
|
|
|
{
|
|
|
|
|
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Camera.main.orthographicSize = height / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dnnUtils.InitHeadPoseEstimationCameraInfo(webCamTextureMat.cols(), webCamTextureMat.rows());
|
|
|
|
|
|
|
|
|
|
webCamReady = true;
|
|
|
|
|
LogPrint.Log("OnWebCamTextureToMatHelperInitialized");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void OnWebCamTextureToMatHelperDisposed()
|
|
|
|
|
{
|
|
|
|
|
LogPrint.Log("OnWebCamTextureToMatHelperDisposed");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnWebCamTextureToMatHelperErrorOccurred(OpenCVCompact.WebCamTextureToMatHelper.ErrorCode errorCode)
|
|
|
|
|
{
|
|
|
|
|
LogPrint.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
|
|
|
|
|
}
|
|
|
|
|
}
|