Health/Assets/Scripts/UI/Component/MotionVideoCaptureManager.cs

105 lines
3.5 KiB
C#

using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity.UnityUtils.Helper;
using UnityEngine;
namespace Yoga
{
public class MotionVideoCaptureManager : CaptureManagerBase
{
public bool FlipHorizontal = true;
private VideoCaptureToMatHelper _videoCaptureToMatHelper;
private readonly string VIDEO_FILENAME = "Video/WIN_20231124_20_02_38_Pro.mp4";
protected override Mat GetMat()
{
Mat img = null;
if (!_videoCaptureToMatHelper.IsPlaying())
_videoCaptureToMatHelper.Play();
if (_videoCaptureToMatHelper.IsPlaying() && _videoCaptureToMatHelper.DidUpdateThisFrame())
{
//img = PictureUtility.HorizontalFlipMat(_videoCaptureToMatHelper.GetMat());
//img = PictureUtility.Undistort(img);
}
return img;
}
protected override void MatInitial()
{
if (_videoCaptureToMatHelper == null)
{
_videoCaptureToMatHelper = transform.GetComponent<VideoCaptureToMatHelper>();
}
if (string.IsNullOrEmpty(_videoCaptureToMatHelper.requestedVideoFilePath))
_videoCaptureToMatHelper.requestedVideoFilePath = VIDEO_FILENAME;
_videoCaptureToMatHelper.outputColorFormat = VideoCaptureToMatHelper.ColorFormat.RGB;
_videoCaptureToMatHelper.Initialize();
}
protected override void OnRelease()
{
base.OnRelease();
if (_videoCaptureToMatHelper != null)
_videoCaptureToMatHelper.Dispose();
}
public void OnVideoCaptureToMatHelperInitialized()
{
Debug.Log("OnVideoCaptureToMatHelperInitialized");
Mat rgbMat = _videoCaptureToMatHelper.GetMat();
_displayTexture = new Texture2D(rgbMat.cols(), rgbMat.rows(), TextureFormat.RGB24, false);
Utils.matToTexture2D(rgbMat, _displayTexture);
TargetDisplayer.texture = _displayTexture;
gameObject.transform.localScale = new Vector3(rgbMat.cols(), rgbMat.rows(), 1);
Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
float width = rgbMat.width();
float height = rgbMat.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;
}
if (!CVEstimator.Instance.IsInited)
{
CVEstimator.Instance.Init();
}
_bgrMat = new Mat(rgbMat.rows(), rgbMat.cols(), CvType.CV_8UC3);
}
public void OnVideoCaptureToMatHelperDisposed()
{
Debug.Log("OnVideoCaptureToMatHelperDisposed");
if (_displayTexture != null)
{
Texture2D.Destroy(_displayTexture);
_displayTexture = null;
}
}
public void OnVideoCaptureToMatHelperErrorOccurred(VideoCaptureToMatHelper.ErrorCode errorCode)
{
Debug.Log("OnVideoCaptureToMatHelperErrorOccurred " + errorCode);
}
}
}