From cefaba5deab2f9d1964d71fe9fc8913a8eb62c1b Mon Sep 17 00:00:00 2001 From: terric Date: Tue, 21 Nov 2023 18:28:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=BB=E5=89=AF=E9=A9=BE?= =?UTF-8?q?=E5=8C=BA=E5=88=AB=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E9=80=89=E5=8F=96=E6=9C=80=E5=A4=A7=E5=8C=BA=E5=9D=97=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=88openpose=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scenes/FaceDetect.unity | 5 +-- Assets/Scripts/GlobalData.cs | 6 ++++ Assets/Scripts/GlobalData.cs.meta | 11 ++++++ .../PoseCheck/EstimateModel/Estimator.cs | 14 +++++++- .../EstimateModel/MediaPipeEstimator.cs | 9 ++--- .../EstimateModel/OpenPoseEsimater.cs | 35 ++++++++++++++----- Assets/Scripts/UI/MainUIPanel.cs | 9 +++++ Assets/Scripts/YogaDataLoader.cs | 6 ++-- 8 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 Assets/Scripts/GlobalData.cs create mode 100644 Assets/Scripts/GlobalData.cs.meta diff --git a/Assets/Scenes/FaceDetect.unity b/Assets/Scenes/FaceDetect.unity index 6d60eab..04b51a0 100644 --- a/Assets/Scenes/FaceDetect.unity +++ b/Assets/Scenes/FaceDetect.unity @@ -449,7 +449,8 @@ MonoBehaviour: m_RequiresDepthTextureOption: 2 m_RequiresOpaqueTextureOption: 2 m_CameraType: 0 - m_Cameras: [] + m_Cameras: + - {fileID: 1969623179} m_RendererIndex: -1 m_VolumeLayerMask: serializedVersion: 2 @@ -784,7 +785,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!114 &1969623177 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/GlobalData.cs b/Assets/Scripts/GlobalData.cs new file mode 100644 index 0000000..fe96e71 --- /dev/null +++ b/Assets/Scripts/GlobalData.cs @@ -0,0 +1,6 @@ +public class GlobalData : MonoSingleton +{ + private PositionType position = PositionType.None; + + public PositionType Position { get => position; internal set => position = value; } +} \ No newline at end of file diff --git a/Assets/Scripts/GlobalData.cs.meta b/Assets/Scripts/GlobalData.cs.meta new file mode 100644 index 0000000..1e23d90 --- /dev/null +++ b/Assets/Scripts/GlobalData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a40cd440bbfea884b978d45f27249385 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PoseCheck/EstimateModel/Estimator.cs b/Assets/Scripts/PoseCheck/EstimateModel/Estimator.cs index be20dc9..b8d362e 100644 --- a/Assets/Scripts/PoseCheck/EstimateModel/Estimator.cs +++ b/Assets/Scripts/PoseCheck/EstimateModel/Estimator.cs @@ -29,7 +29,7 @@ public abstract class Estimator 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; //是否满足主驾/副驾的位置条件 @@ -37,6 +37,18 @@ public abstract class Estimator //获取矩形的中点 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; } diff --git a/Assets/Scripts/PoseCheck/EstimateModel/MediaPipeEstimator.cs b/Assets/Scripts/PoseCheck/EstimateModel/MediaPipeEstimator.cs index a7066dd..f00f892 100644 --- a/Assets/Scripts/PoseCheck/EstimateModel/MediaPipeEstimator.cs +++ b/Assets/Scripts/PoseCheck/EstimateModel/MediaPipeEstimator.cs @@ -51,10 +51,8 @@ public class MediaPipeEstimator : Estimator points = null; Mat result = _personDetector.infer(bgrMat); - if (result.rows() == 0) - { - return false; - } + if (result.rows() == 0) + return false; 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)]; 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); float[] landmarks_world = new float[(39 - auxiliary_points_num) * 3]; results_col199_316_39x3.get(0, 0, landmarks_world); - - //将关键点映射到现有的open pose关键点上 retVal.Add(GetPointData(landmarks_screen_xy, landmarks_presence, 0)); //Nose retVal.Add(new Point(-1, -1)); //Neck diff --git a/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs b/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs index d6aab54..7b1ba5c 100644 --- a/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs +++ b/Assets/Scripts/PoseCheck/EstimateModel/OpenPoseEsimater.cs @@ -5,6 +5,7 @@ using OpenCVForUnity.UnityUtils; using System; using System.Collections.Generic; using UnityEngine; +using UnityEngine.UIElements; using Yoga; public class OpenPoseEsimater : Estimator @@ -62,6 +63,7 @@ public class OpenPoseEsimater : Estimator var voloResultBox = new List(); bool hasValidObject = false; + List> boxList = new(); for (int i = results.rows() - 1; i >= 0; --i) { @@ -72,18 +74,33 @@ public class OpenPoseEsimater : Estimator float[] cls = new float[1]; results.get(i, 5, cls); //类别 - if (!IsObjectValid(box, conf, cls, rgbaMat)) - { - return false; - } + if (!IsObjectValid(box, conf, cls, rgbaMat)) //不符合规则的,跳过 + continue; + hasValidObject = true; - voloResultBox.Clear(); - voloResultBox.Add(box); - voloResultBox.Add(conf); - voloResultBox.Add(cls); - break; + List item= new List(); + item.Add(box); + item.Add(conf); + item.Add(cls); + boxList.Add(item); } + float area = 0; + List maxSizeBox = new List(); + //选择最大的一个 + 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) //没有检测到人体 { Debug.Log("No person block found. Re-Estimation."); diff --git a/Assets/Scripts/UI/MainUIPanel.cs b/Assets/Scripts/UI/MainUIPanel.cs index b925021..b18df62 100644 --- a/Assets/Scripts/UI/MainUIPanel.cs +++ b/Assets/Scripts/UI/MainUIPanel.cs @@ -33,11 +33,20 @@ public class MainUIPanel : UIPanelBase public void OnDriverBtnClicked() { + GlobalData.Instance.Position = PositionType.Driver; UIManager.Instance.ShowPanel(true); } public void OnCoDriverBtnClicked() { + GlobalData.Instance.Position = PositionType.CoDriver; UIManager.Instance.ShowPanel(true); } +} + +public enum PositionType +{ + None, + Driver, + CoDriver } \ No newline at end of file diff --git a/Assets/Scripts/YogaDataLoader.cs b/Assets/Scripts/YogaDataLoader.cs index 4029025..626c952 100644 --- a/Assets/Scripts/YogaDataLoader.cs +++ b/Assets/Scripts/YogaDataLoader.cs @@ -14,11 +14,11 @@ public class YogaDataLoader data[-1] = new YogaData() { VideoPath = "Video/Action3", - Action = AvatarAction.HandsUp, - ModelType = ModelType.MediapipePose, + Action = AvatarAction.HeadShake, + ModelType = ModelType.OpenPose, MaxActionCount = 1000, TotalSeconds = 20.0f, - MustPoints = new List() { "Nose", "RShoulder", "LShoulder", "RElbow", "LElbow" }, + MustPoints = new List() { "Nose", "REye", "LEye", "Neck" }, //AnyPoints = new List() { "RWrist", "LWrist" } }; #endif