Health/Assets/Scripts/PoseCheck/EstimateModel/CVEstimator.cs

141 lines
3.9 KiB
C#
Raw Normal View History

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.LevelData.ModelType)
2023-11-17 06:44:27 +00:00
{
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
2023-11-25 20:52:27 +00:00
IsRunning = false;
2023-11-15 08:10:56 +00:00
_mainThread = new Thread(new ThreadStart(MainThread));
_mainThread.Start();
IsInited = true;
}
2023-11-24 18:39:53 +00:00
private void MainThread()
{
//解析数据
_estimateThread = new Thread(new ThreadStart(Estimation));
LogPrint.Log("MainThread Executed");
}
2023-11-15 08:10:56 +00:00
public void StartEstimation()
{
2023-11-24 18:39:53 +00:00
if (IsRunning)
2023-11-15 08:10:56 +00:00
{
2023-11-24 18:39:53 +00:00
LogPrint.Log("Estimation running");
2023-11-15 08:10:56 +00:00
return;
}
2023-11-24 18:39:53 +00:00
if (!IsInited)
{
Init();
LogPrint.Log("CVEstimator is not inited. ", PrintLevel.Normal);
}
if (_estimateThread == null)
2023-11-15 08:10:56 +00:00
{
2023-11-24 18:39:53 +00:00
LogPrint.Error("Estimate Thread fail to load!", PrintLevel.Important);
LogPrint.Error($"Main Thread: {_mainThread.ThreadState}");
2023-11-15 08:10:56 +00:00
return;
}
2023-11-24 18:39:53 +00:00
_estimateThread.Start();
LogPrint.Log("EstimateThread Start");
2023-11-15 08:10:56 +00:00
IsRunning = true;
}
public void Dispose()
{
if (!IsInited)
{
return;
}
IsRunning = false;
_estimateThread.Abort();
_mainThread.Abort();
IsInited = false;
}
private void Estimation()
{
DateTime startTime = DateTime.Now;
Mat bgrMat = new Mat();
2023-11-25 20:52:27 +00:00
LogPrint.Log("IsRunning: " + IsRunning);
2023-11-15 08:10:56 +00:00
while (IsRunning)
{
2023-11-25 20:52:27 +00:00
try
2023-11-15 08:10:56 +00:00
{
2023-11-25 20:52:27 +00:00
LogPrint.Log("Estimation Thread Running!IsRunning: " + IsRunning);
Mat rgbaMat = YogaManager.Instance.RgbaMat;
if (rgbaMat == null)
{
LogPrint.Log("WebCamTexture is null. ");
LogPrint.Log("Re-Estimation.");
continue; //重新检测
}
Imgproc.cvtColor(rgbaMat, bgrMat, Imgproc.COLOR_RGBA2BGR);
2023-11-15 08:10:56 +00:00
2023-11-25 20:52:27 +00:00
if (!YogaManager.Instance.CurrentEstimator.Esitmate(bgrMat, rgbaMat, out List<Point> points))
{
LogPrint.Log("Pose estimation failed. Re-Estimating.");
continue; //重新检测
}
2023-11-15 08:10:56 +00:00
2023-11-25 20:52:27 +00:00
//更新关键点位检测结果
YogaManager.Instance.AddCurrEstimateKeyPoints(points);
2023-11-15 08:10:56 +00:00
2023-11-25 20:52:27 +00:00
//等待到下一个更新周期
if (DateTime.Now - startTime > _refreshRate)
{
startTime = DateTime.Now;
}
else
{
Thread.Sleep(_refreshRate - (DateTime.Now - startTime));
}
2023-11-24 18:39:53 +00:00
}
2023-11-25 20:52:27 +00:00
catch (Exception e)
2023-11-24 18:39:53 +00:00
{
2023-11-25 20:52:27 +00:00
LogPrint.Exception(e);
2023-11-24 18:39:53 +00:00
}
2023-11-25 20:52:27 +00:00
2023-11-15 08:10:56 +00:00
}
bgrMat.Dispose();
}
}