Selaa lähdekoodia

Enable calibration when object detection is ready

Nick Steyer 1 vuosi sitten
vanhempi
commit
4dbbc78700

+ 22 - 0
Assets/CalibrationMarkerBehavior.cs

@@ -1,9 +1,13 @@
+using Assets.StreetLight.Scripts;
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
+using System.Drawing;
 using System.Net;
 using TMPro;
 using Unity.VisualScripting;
+using UnityEditor.VersionControl;
 using UnityEngine;
 using UnityEngine.Timeline;
 
@@ -11,6 +15,13 @@ public class CalibrationMarkerBehavior : MonoBehaviour
 {
     Vector3[] frustumCorners;
     List<Vector3> targetPositions;
+    PersonManager PersonManager => personManagerLazy.Value;
+    Lazy<PersonManager> personManagerLazy;
+
+    private void Awake()
+    {
+        personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
+    }
 
     // Start is called before the first frame update
     void Start()
@@ -28,6 +39,14 @@ public class CalibrationMarkerBehavior : MonoBehaviour
         };
 
         currentTarget = GameObject.Find("CalibrationMarker").transform.position;
+
+        enabled = false;
+        PersonManager.DetectionReady += PersonManager_DetectionReady;
+    }
+
+    private void PersonManager_DetectionReady(object sender, EventArgs e)
+    {
+        enabled = true;
     }
 
     int targetIndex = -1;
@@ -51,5 +70,8 @@ public class CalibrationMarkerBehavior : MonoBehaviour
             currentTarget = targetPositions[targetIndex];
             normalizedTranslationVector = (currentTarget - marker.transform.position).normalized;
         }
+
+        UnityEngine.Color color = UnityEngine.Color.Lerp(UnityEngine.Color.green, UnityEngine.Color.red, Time.deltaTime / 0.2f);
+        Debug.Log(string.Format("<color=#{0:X2}{1:X2}{2:X2}>{3}</color>", (byte)(color.r * 255f), (byte)(color.g * 255f), (byte)(color.b * 255f), $"deltaTime: {Time.deltaTime}"));
     }
 }

+ 8 - 0
Assets/CalibrationRecorderBehavior.cs

@@ -35,6 +35,14 @@ namespace Assets
             {
                 File.WriteAllLines(calibrationFileName, new string[] { "Time,WorldX,WorldY,WorldZ,UnityX,UnityY,UnityZ" });
             }
+
+            enabled = false;
+            PersonManager.DetectionReady += PersonManager_DetectionReady;
+        }
+
+        private void PersonManager_DetectionReady(object sender, EventArgs e)
+        {
+            enabled = true;
         }
 
         void Update()

+ 0 - 3
Assets/StreetLight/Adapters/ZedPersonDetector.cs

@@ -2,13 +2,10 @@
 using Assets.StreetLight.Poco;
 using Assets.StreetLight.Serialization;
 using Newtonsoft.Json;
-using Newtonsoft.Json.Serialization;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 using UnityEngine;
 
 namespace Assets.StreetLight.Adapters

+ 0 - 3
Assets/StreetLight/Interfaces/IPersonDetector.cs

@@ -1,9 +1,6 @@
 using Assets.StreetLight.Poco;
 using System;
 using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace Assets.StreetLight.Interfaces
 {

+ 9 - 0
Assets/StreetLight/Scripts/PersonManager.cs

@@ -19,6 +19,8 @@ namespace Assets.StreetLight.Scripts
 
         private IPersonDetector personDetector;
 
+        public event EventHandler DetectionReady;
+
         void Start()
         {
             var lines = File.ReadAllLines(Configuration.Instance.InputCalibrationFilePath);
@@ -43,9 +45,16 @@ namespace Assets.StreetLight.Scripts
 
             Persons = new ObservableCollection<Person>();
             personDetector = new ZedPersonDetector(FindObjectOfType<ZEDManager>());
+            personDetector.PersonsDetected += InvokeDetectionReady;
             personDetector.PersonsDetected += PersonDetector_PersonsDetected;
         }
 
+        private void InvokeDetectionReady(object sender, IEnumerable<Person> e)
+        {
+            DetectionReady?.Invoke(this, EventArgs.Empty);
+            personDetector.PersonsDetected -= InvokeDetectionReady;
+        }
+
         private void PersonDetector_PersonsDetected(object sender, IEnumerable<Person> e)
         {
             Persons.Clear();