|
@@ -0,0 +1,416 @@
|
|
|
+using System;
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using Assets.Logging;
|
|
|
+using Assets.StreetLight.Poco;
|
|
|
+using Assets.ZED.SDK.Helpers.Scripts;
|
|
|
+using UnityEngine;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public class MyZED3DObjectVisualizer : MonoBehaviour
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Space(5)]
|
|
|
+ [Header("Box Appearance")]
|
|
|
+ [Tooltip("Prefab object that's instantiated to represent detected objects. " +
|
|
|
+ "This class expects the object to have the default Unity cube as a mesh - otherwise, it may be scaled incorrectly.\r\n" +
|
|
|
+ "It also expects a BBox3DHandler component in the root object, but you won't have errors if it lacks one. ")]
|
|
|
+ public GameObject boundingBoxPrefab;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Tooltip("The colors that will be cycled through when assigning colors to new bounding boxes. ")]
|
|
|
+
|
|
|
+ public List<Color> boxColors = new List<Color>()
|
|
|
+ {
|
|
|
+ new Color(.231f, .909f, .69f, 1),
|
|
|
+ new Color(.098f, .686f, .816f, 1),
|
|
|
+ new Color(.412f, .4f, .804f, 1),
|
|
|
+ new Color(1, .725f, 0f, 1),
|
|
|
+ new Color(.989f, .388f, .419f, 1)
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Space(5)]
|
|
|
+ [Header("Box Transform")]
|
|
|
+ [Tooltip("If true, bounding boxes are rotated to face the camera that detected them. If false, the boxes are calculated from known bounds to face Z = 1. " +
|
|
|
+ "'False' has more parity with the SDK and will generally result in more accurate boxes.")]
|
|
|
+ public bool boxesFaceCamera = false;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Tooltip("If true, transforms the localScale of the root bounding box transform to match the detected 3D bounding box. ")]
|
|
|
+ public bool transformBoxScale = true;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Tooltip("If true, and transformBoxScale is also true, modifies the center and Y bounds of each detected bounding box so that its " +
|
|
|
+ "bottom is at floor level (Y = 0) while keeping the other corners at the same place. WARNING: Estimate Initial position must be set to true.")]
|
|
|
+ public bool transformBoxToTouchFloor = true;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Tooltip("If true, sets the Y value of the center of the bounding box to 0. Use for bounding box prefabs meant to be centered at the user's feet. ")]
|
|
|
+ [LabelOverride("Box Center Always On Floor")]
|
|
|
+ public bool floorBBoxPosition = false;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Space(5)]
|
|
|
+ [Header("Filters")]
|
|
|
+ [Tooltip("Display bounding boxes of objects that are actively being tracked by object tracking, where valid positions are known. ")]
|
|
|
+ public bool showONTracked = true;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Tooltip("Display bounding boxes of objects that were actively being tracked by object tracking, but that were lost very recently.")]
|
|
|
+ public bool showSEARCHINGTracked = false;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Tooltip("Display bounding boxes of objects that are visible but not actively being tracked by object tracking (usually because object tracking is disabled in ZEDManager).")]
|
|
|
+ public bool showOFFTracked = false;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ [Tooltip("How wide a bounding box has to be in order to be displayed. Use this to remove tiny bounding boxes from partially-occluded objects.\r\n" +
|
|
|
+ "(If you have this issue, it can also be helpful to set showSEARCHINGTracked to OFF.)")]
|
|
|
+ public float minimumWidthToDisplay = 0.3f;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Dictionary<int, Color> idColorDict = new Dictionary<int, Color>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Stack<GameObject> bboxPool = new Stack<GameObject>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Dictionary<int, GameObject> liveBBoxes = new Dictionary<int, GameObject>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private int nextColorIndex = 0;
|
|
|
+
|
|
|
+ PersonManager PersonManager => personManagerLazy.Value;
|
|
|
+ Lazy<PersonManager> personManagerLazy;
|
|
|
+
|
|
|
+ private void Awake()
|
|
|
+ {
|
|
|
+ personManagerLazy = new(FindObjectOfType<PersonManager>());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Update()
|
|
|
+ {
|
|
|
+ Visualize3DBoundingBoxes(PersonManager.Persons);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ZedManager_OnObjectDetection(DetectionFrame objFrame)
|
|
|
+ {
|
|
|
+
|
|
|
+ UpdateMinMaxCoordinates(objFrame);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void UpdateMinMaxCoordinates(DetectionFrame objFrame)
|
|
|
+ {
|
|
|
+ List<DetectedObject> newobjects = objFrame.GetFilteredObjectList(showONTracked, showSEARCHINGTracked, showOFFTracked);
|
|
|
+ foreach (var detectedObject in newobjects)
|
|
|
+ {
|
|
|
+ Bounds objbounds = detectedObject.Get3DWorldBounds();
|
|
|
+ if (objbounds.size.x < minimumWidthToDisplay || objbounds.size == Vector3.zero) { }
|
|
|
+ {
|
|
|
+ Vector3 obj_position = detectedObject.Get3DWorldPosition();
|
|
|
+ minX = Math.Min(minX, obj_position.x);
|
|
|
+ maxX = Math.Max(maxX, obj_position.x);
|
|
|
+ minY = Math.Min(minY, obj_position.y);
|
|
|
+ maxY = Math.Max(maxY, obj_position.y);
|
|
|
+ minZ = Math.Min(minZ, obj_position.z);
|
|
|
+ maxZ = Math.Max(maxZ, obj_position.z);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ float maxX = 0.9971107f;
|
|
|
+ float minX = -0.7968294f;
|
|
|
+ float maxY = int.MinValue;
|
|
|
+ float minY = int.MaxValue;
|
|
|
+ float maxZ = 2.373606f;
|
|
|
+ float minZ = 0.4380694f;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void Visualize3DBoundingBoxes(IEnumerable<Person> persons)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ List<int> activeids = liveBBoxes.Keys.ToList();
|
|
|
+
|
|
|
+ foreach (var person in persons)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (activeids.Contains(person.Id)) activeids.Remove(person.Id);
|
|
|
+
|
|
|
+
|
|
|
+ GameObject bbox = GetBBoxForObject(person);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ bbox.transform.position = new Vector3(person.GroundPosition.x, 0, person.GroundPosition.y);
|
|
|
+
|
|
|
+ if (floorBBoxPosition)
|
|
|
+ {
|
|
|
+ bbox.transform.position = new Vector3(bbox.transform.position.x, 0, bbox.transform.position.z);
|
|
|
+ }
|
|
|
+
|
|
|
+ var x = bbox.transform.position.x;
|
|
|
+
|
|
|
+ var totalDistance = maxX - minX;
|
|
|
+ var distanceFromStart = x - minX;
|
|
|
+ var ratio = distanceFromStart / totalDistance;
|
|
|
+ var newX = -12 + ratio * 24;
|
|
|
+
|
|
|
+ var z = bbox.transform.position.z;
|
|
|
+
|
|
|
+ totalDistance = maxZ - minZ;
|
|
|
+ distanceFromStart = z - minZ;
|
|
|
+ ratio = distanceFromStart / totalDistance;
|
|
|
+ var newZ = -6 + ratio * 12;
|
|
|
+
|
|
|
+ bbox.transform.position = new Vector3(newX, 0, newZ);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ BBox3DHandler boxhandler = bbox.GetComponent<BBox3DHandler>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ foreach (int id in activeids)
|
|
|
+ {
|
|
|
+ ReturnBoxToPool(id, liveBBoxes[id]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private GameObject GetBBoxForObject(Person dobj)
|
|
|
+ {
|
|
|
+ if (!liveBBoxes.ContainsKey(dobj.Id))
|
|
|
+ {
|
|
|
+ GameObject newbox = GetAvailableBBox();
|
|
|
+ newbox.name = "Object #" + dobj.Id;
|
|
|
+
|
|
|
+ BBox3DHandler boxhandler = newbox.GetComponent<BBox3DHandler>();
|
|
|
+
|
|
|
+ Color col;
|
|
|
+ if (idColorDict.ContainsKey(dobj.Id))
|
|
|
+ {
|
|
|
+ col = idColorDict[dobj.Id];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ col = GetNextColor();
|
|
|
+ idColorDict.Add(dobj.Id, col);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (boxhandler)
|
|
|
+ {
|
|
|
+ boxhandler.SetColor(col);
|
|
|
+ boxhandler.SetID(dobj.Id.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ liveBBoxes[dobj.Id] = newbox;
|
|
|
+ return newbox;
|
|
|
+ }
|
|
|
+ else return liveBBoxes[dobj.Id];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private GameObject GetAvailableBBox()
|
|
|
+ {
|
|
|
+ if (bboxPool.Count == 0)
|
|
|
+ {
|
|
|
+ GameObject newbbox = Instantiate(boundingBoxPrefab);
|
|
|
+ newbbox.transform.SetParent(transform, false);
|
|
|
+ bboxPool.Push(newbbox);
|
|
|
+ }
|
|
|
+
|
|
|
+ GameObject bbox = bboxPool.Pop();
|
|
|
+ bbox.SetActive(true);
|
|
|
+
|
|
|
+ return bbox;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void ReturnBoxToPool(int id, GameObject bbox)
|
|
|
+ {
|
|
|
+ bbox.SetActive(false);
|
|
|
+ bbox.name = "Unused";
|
|
|
+
|
|
|
+ bboxPool.Push(bbox);
|
|
|
+
|
|
|
+ if (liveBBoxes.ContainsKey(id))
|
|
|
+ {
|
|
|
+ liveBBoxes.Remove(id);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Debug.LogError("Tried to remove object ID " + id + " from active bboxes, but it wasn't in the dictionary.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Color GetNextColor()
|
|
|
+ {
|
|
|
+ if (boxColors.Count == 0)
|
|
|
+ {
|
|
|
+ return new Color(.043f, .808f, .435f, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (nextColorIndex >= boxColors.Count)
|
|
|
+ {
|
|
|
+ nextColorIndex = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ Color returncol = boxColors[nextColorIndex];
|
|
|
+
|
|
|
+ nextColorIndex++;
|
|
|
+
|
|
|
+
|
|
|
+ return returncol;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnDestroy()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void DrawDebugBox(DetectedObject dobj)
|
|
|
+ {
|
|
|
+
|
|
|
+ Transform camtrans = dobj.detectingZEDManager.GetLeftCameraTransform();
|
|
|
+ Vector3[] corners = dobj.rawObjectData.worldBoundingBox;
|
|
|
+ Vector3[] rotcorners = new Vector3[8];
|
|
|
+
|
|
|
+ Vector3[] corners3d = dobj.Get3DWorldCorners();
|
|
|
+ for (int i = 0; i < 8; i++)
|
|
|
+ {
|
|
|
+ Vector3 fixrot = camtrans.InverseTransformPoint(corners[i]);
|
|
|
+ rotcorners[i] = fixrot;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Debug.DrawLine(corners3d[0], corners3d[1], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[1], corners3d[2], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[2], corners3d[3], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[3], corners3d[0], Color.red);
|
|
|
+
|
|
|
+ Debug.DrawLine(corners3d[4], corners3d[5], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[5], corners3d[6], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[6], corners3d[7], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[7], corners3d[4], Color.red);
|
|
|
+
|
|
|
+ Debug.DrawLine(corners3d[0], corners3d[4], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[1], corners3d[5], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[2], corners3d[6], Color.red);
|
|
|
+ Debug.DrawLine(corners3d[3], corners3d[7], Color.red);
|
|
|
+ }
|
|
|
+}
|