2023-11-15 08:10:56 +00:00
|
|
|
|
using OpenCVForUnity.CoreModule;
|
|
|
|
|
using OpenCVForUnity.ImgprocModule;
|
|
|
|
|
using OpenCVForUnity.UnityUtils;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Yoga;
|
|
|
|
|
|
|
|
|
|
public class CVEstimator : Singleton<CVEstimator>
|
|
|
|
|
{
|
|
|
|
|
private bool isInited = false;
|
|
|
|
|
|
|
|
|
|
public bool IsInited { get => isInited; private set => isInited = value; }
|
|
|
|
|
public bool IsRunning { get; private set; }
|
|
|
|
|
|
|
|
|
|
private Thread _mainThread;
|
|
|
|
|
private Thread _estimateThread;
|
|
|
|
|
|
|
|
|
|
private static readonly TimeSpan _refreshRate = TimeSpan.FromSeconds(1 / 30);
|
|
|
|
|
|
|
|
|
|
public void Init(params object[] args)
|
|
|
|
|
{
|
|
|
|
|
if (IsInited)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_mainThread != null)
|
|
|
|
|
{
|
|
|
|
|
_mainThread.Abort();
|
|
|
|
|
_mainThread = null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 06:44:27 +00:00
|
|
|
|
//加载模型
|
|
|
|
|
switch (YogaManager.Instance.Action.ModelType)
|
|
|
|
|
{
|
|
|
|
|
case ModelType.OpenPose:
|
|
|
|
|
YogaManager.Instance.CurrentEstimator = new OpenPoseEsimater();
|
|
|
|
|
break;
|
|
|
|
|
case ModelType.MediapipePose:
|
|
|
|
|
YogaManager.Instance.CurrentEstimator = new MediaPipeEstimator();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
YogaManager.Instance.CurrentEstimator.Init();
|
2023-11-15 08:10:56 +00:00
|
|
|
|
|
|
|
|
|
_mainThread = new Thread(new ThreadStart(MainThread));
|
|
|
|
|
_mainThread.Start();
|
|
|
|
|
|
|
|
|
|
IsInited = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StartEstimation()
|
|
|
|
|
{
|
|
|
|
|
if (!IsInited)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("CVEstimator is not inited. ");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_estimateThread.Start();
|
|
|
|
|
if (IsRunning)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
IsRunning = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!IsInited)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
IsRunning = false;
|
|
|
|
|
_estimateThread.Abort();
|
|
|
|
|
_mainThread.Abort();
|
|
|
|
|
IsInited = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MainThread()
|
|
|
|
|
{
|
|
|
|
|
//加载模型
|
|
|
|
|
Utils.setDebugMode(true); //打印日志
|
|
|
|
|
//解析数据
|
|
|
|
|
_estimateThread = new Thread(new ThreadStart(Estimation));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Estimation()
|
|
|
|
|
{
|
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
|
Mat bgrMat = new Mat();
|
|
|
|
|
while (IsRunning)
|
|
|
|
|
{
|
|
|
|
|
Mat rgbaMat = MotionCaptureManager.Instance.RgbaMat;
|
|
|
|
|
if (rgbaMat == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("WebCamTexture is null. ");
|
|
|
|
|
Debug.Log("Re-Estimation.");
|
|
|
|
|
continue; //重新检测
|
|
|
|
|
}
|
|
|
|
|
Imgproc.cvtColor(rgbaMat, bgrMat, Imgproc.COLOR_RGBA2BGR);
|
|
|
|
|
|
2023-11-17 06:44:27 +00:00
|
|
|
|
if (!YogaManager.Instance.CurrentEstimator.Esitmate(bgrMat, rgbaMat, out List<Point> points))
|
2023-11-15 08:10:56 +00:00
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("Pose estimation failed. Re-Estimating.");
|
|
|
|
|
continue; //重新检测
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//更新关键点位检测结果
|
|
|
|
|
MotionCaptureManager.Instance.CurrPersonPoints = points;
|
|
|
|
|
|
|
|
|
|
//等待到下一个更新周期
|
|
|
|
|
if (DateTime.Now - startTime > _refreshRate)
|
|
|
|
|
{
|
|
|
|
|
startTime = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(_refreshRate - (DateTime.Now - startTime));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bgrMat.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|