新增主副驾区别功能,新增选取最大区块功能(openpose)

This commit is contained in:
terric 2023-11-21 18:28:27 +08:00
parent 687ddbd876
commit cefaba5dea
8 changed files with 73 additions and 22 deletions

View File

@ -449,7 +449,8 @@ MonoBehaviour:
m_RequiresDepthTextureOption: 2 m_RequiresDepthTextureOption: 2
m_RequiresOpaqueTextureOption: 2 m_RequiresOpaqueTextureOption: 2
m_CameraType: 0 m_CameraType: 0
m_Cameras: [] m_Cameras:
- {fileID: 1969623179}
m_RendererIndex: -1 m_RendererIndex: -1
m_VolumeLayerMask: m_VolumeLayerMask:
serializedVersion: 2 serializedVersion: 2
@ -784,7 +785,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!114 &1969623177 --- !u!114 &1969623177
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -0,0 +1,6 @@
public class GlobalData : MonoSingleton<GlobalData>
{
private PositionType position = PositionType.None;
public PositionType Position { get => position; internal set => position = value; }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a40cd440bbfea884b978d45f27249385
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -29,7 +29,7 @@ public abstract class Estimator
protected bool IsObjectValid(float[] box, float[] confidence, float[] classID, Mat rgbaMat) protected bool IsObjectValid(float[] box, float[] confidence, float[] classID, Mat rgbaMat)
{ {
if ((int)classID[0] != 0 || confidence[0] < 0.8f) //只检测人体且置信度大于80% if ((int)classID[0] != 0 || confidence[0] < 0.6f) //只检测人体且置信度大于60%
return false; return false;
//是否满足主驾/副驾的位置条件 //是否满足主驾/副驾的位置条件
@ -37,6 +37,18 @@ public abstract class Estimator
//获取矩形的中点 //获取矩形的中点
float centerX = (box[0] + box[2]) / 2; float centerX = (box[0] + box[2]) / 2;
//左边副驾驶,右边主驾驶 //左边副驾驶,右边主驾驶
if (centerX < width / 2) //主驾驶
{
//选择为副驾驶返回false
if (GlobalData.Instance.Position == PositionType.CoDriver)
return false;
}
else //副驾驶
{
//选择为主驾驶返回false
if (GlobalData.Instance.Position == PositionType.Driver)
return false;
}
return true; return true;
} }

View File

@ -52,9 +52,7 @@ public class MediaPipeEstimator : Estimator
Mat result = _personDetector.infer(bgrMat); Mat result = _personDetector.infer(bgrMat);
if (result.rows() == 0) if (result.rows() == 0)
{
return false; return false;
}
for (int i = 0; i < result.rows(); ++i) for (int i = 0; i < result.rows(); ++i)
{ {
@ -101,13 +99,10 @@ public class MediaPipeEstimator : Estimator
float[] landmarks_presence = new float[(39 - auxiliary_points_num)]; float[] landmarks_presence = new float[(39 - auxiliary_points_num)];
results_col4_199_39x5.colRange(new OpenCVRange(4, 5)).get(0, 0, landmarks_presence); results_col4_199_39x5.colRange(new OpenCVRange(4, 5)).get(0, 0, landmarks_presence);
Mat results_col199_316_39x3 = result.rowRange(new OpenCVRange(199, 316 - (3 * auxiliary_points_num))).reshape(1, 39 - auxiliary_points_num); Mat results_col199_316_39x3 = result.rowRange(new OpenCVRange(199, 316 - (3 * auxiliary_points_num))).reshape(1, 39 - auxiliary_points_num);
float[] landmarks_world = new float[(39 - auxiliary_points_num) * 3]; float[] landmarks_world = new float[(39 - auxiliary_points_num) * 3];
results_col199_316_39x3.get(0, 0, landmarks_world); results_col199_316_39x3.get(0, 0, landmarks_world);
//将关键点映射到现有的open pose关键点上 //将关键点映射到现有的open pose关键点上
retVal.Add(GetPointData(landmarks_screen_xy, landmarks_presence, 0)); //Nose retVal.Add(GetPointData(landmarks_screen_xy, landmarks_presence, 0)); //Nose
retVal.Add(new Point(-1, -1)); //Neck retVal.Add(new Point(-1, -1)); //Neck

View File

@ -5,6 +5,7 @@ using OpenCVForUnity.UnityUtils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements;
using Yoga; using Yoga;
public class OpenPoseEsimater : Estimator public class OpenPoseEsimater : Estimator
@ -62,6 +63,7 @@ public class OpenPoseEsimater : Estimator
var voloResultBox = new List<float[]>(); var voloResultBox = new List<float[]>();
bool hasValidObject = false; bool hasValidObject = false;
List<List<float[]>> boxList = new();
for (int i = results.rows() - 1; i >= 0; --i) for (int i = results.rows() - 1; i >= 0; --i)
{ {
@ -72,18 +74,33 @@ public class OpenPoseEsimater : Estimator
float[] cls = new float[1]; float[] cls = new float[1];
results.get(i, 5, cls); //类别 results.get(i, 5, cls); //类别
if (!IsObjectValid(box, conf, cls, rgbaMat)) if (!IsObjectValid(box, conf, cls, rgbaMat)) //不符合规则的,跳过
{ continue;
return false;
}
hasValidObject = true; hasValidObject = true;
voloResultBox.Clear(); List<float[]> item= new List<float[]>();
voloResultBox.Add(box); item.Add(box);
voloResultBox.Add(conf); item.Add(conf);
voloResultBox.Add(cls); item.Add(cls);
break; boxList.Add(item);
} }
float area = 0;
List<float[]> maxSizeBox = new List<float[]>();
//选择最大的一个
foreach(var box in boxList)
{
var rect = box[0];
var tmp = (rect[2] - rect[0]) * (rect[3] - rect[1]);
area = Math.Max(area, tmp);
if (area == tmp)
{
maxSizeBox = box;
}
}
voloResultBox.Clear();
voloResultBox = maxSizeBox;
if (!hasValidObject) //没有检测到人体 if (!hasValidObject) //没有检测到人体
{ {
Debug.Log("No person block found. Re-Estimation."); Debug.Log("No person block found. Re-Estimation.");

View File

@ -33,11 +33,20 @@ public class MainUIPanel : UIPanelBase
public void OnDriverBtnClicked() public void OnDriverBtnClicked()
{ {
GlobalData.Instance.Position = PositionType.Driver;
UIManager.Instance.ShowPanel<ActionListUIPanel>(true); UIManager.Instance.ShowPanel<ActionListUIPanel>(true);
} }
public void OnCoDriverBtnClicked() public void OnCoDriverBtnClicked()
{ {
GlobalData.Instance.Position = PositionType.CoDriver;
UIManager.Instance.ShowPanel<ActionListUIPanel>(true); UIManager.Instance.ShowPanel<ActionListUIPanel>(true);
} }
} }
public enum PositionType
{
None,
Driver,
CoDriver
}

View File

@ -14,11 +14,11 @@ public class YogaDataLoader
data[-1] = new YogaData() data[-1] = new YogaData()
{ {
VideoPath = "Video/Action3", VideoPath = "Video/Action3",
Action = AvatarAction.HandsUp, Action = AvatarAction.HeadShake,
ModelType = ModelType.MediapipePose, ModelType = ModelType.OpenPose,
MaxActionCount = 1000, MaxActionCount = 1000,
TotalSeconds = 20.0f, TotalSeconds = 20.0f,
MustPoints = new List<string>() { "Nose", "RShoulder", "LShoulder", "RElbow", "LElbow" }, MustPoints = new List<string>() { "Nose", "REye", "LEye", "Neck" },
//AnyPoints = new List<string>() { "RWrist", "LWrist" } //AnyPoints = new List<string>() { "RWrist", "LWrist" }
}; };
#endif #endif