DetectionFrame.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using sl;
  5. /// <summary>
  6. /// Holds all objects detected by the ZED Object Detection module from a single ZED camera during a single frame.
  7. /// Holds metadata about the frame and camera, and provides helper functions for filtering out the detected objects.
  8. /// <para>This is provided when subscribed to the ZEDManager.OnObjectDetection event.</para>
  9. /// </summary><remarks>
  10. /// This is a higher level version of sl.ObjectsFrame, which comes directly from the ZED SDK and doesn't follow Unity conventions.
  11. /// </remarks>
  12. public class DetectionFrame
  13. {
  14. private ObjectsFrameSDK objectsFrame;
  15. /// <summary>
  16. /// The raw ObjectsFrame object that this object is an abstraction of - ObjectsFrame comes
  17. /// directly from the SDk and doesn't follow Unity conventions.
  18. /// </summary>
  19. public ObjectsFrameSDK rawObjectsFrame
  20. {
  21. get
  22. {
  23. return objectsFrame;
  24. }
  25. }
  26. /// <summary>
  27. /// Timestamp of when the object detection module finished detecting the frame. (TODO: Verify.)
  28. /// </summary>
  29. public ulong timestamp
  30. {
  31. get
  32. {
  33. return objectsFrame.timestamp;
  34. }
  35. }
  36. private int frameDetected = -1;
  37. /// <summary>
  38. /// Value of Time.frameCount when this object was created. Assumes the constructor was called in the same frame
  39. /// that the object was detected by the SDK.
  40. /// </summary>
  41. public int frameCountAtDetection
  42. {
  43. get
  44. {
  45. return frameDetected;
  46. }
  47. }
  48. /// <summary>
  49. /// How many objects were detected in total.
  50. /// <para>Note this does not include any filtering, so it includes objects in the SEARCHING and OFF tracking states.</para>
  51. /// </summary>
  52. public int objectCount
  53. {
  54. get
  55. {
  56. return objectsFrame.numObject;
  57. }
  58. }
  59. /// <summary>
  60. /// The manager class responsible for the ZED camera that detected the objects in this frame.
  61. /// </summary>
  62. public ZEDManager detectingZEDManager;
  63. private List<DetectedObject> detObjects = new List<DetectedObject>();
  64. /// <summary>
  65. /// All objects detected within this frame. Use GetFilteredObjectList to filter them by category or confidence.
  66. /// </summary>
  67. public List<DetectedObject> detectedObjects
  68. {
  69. get
  70. {
  71. return detObjects;
  72. }
  73. }
  74. /// <summary>
  75. /// Constructor that sets up this frame and spawns DetectedObject objects for each raw ObjectData object in the frame.
  76. /// </summary>
  77. /// <param name="oframe">Raw sl.ObjectsFrame object from the SDK, that this object is an abstraction of.</param>
  78. /// <param name="detectingmanager">ZEDManager that represents the camera that detected this frame.</param>
  79. public DetectionFrame(ObjectsFrameSDK oframe, ZEDManager detectingmanager)
  80. {
  81. objectsFrame = oframe;
  82. detectingZEDManager = detectingmanager;
  83. frameDetected = Time.frameCount;
  84. Vector3 campos = detectingmanager.GetLeftCameraTransform().position;
  85. Quaternion camrot = detectingmanager.GetLeftCameraTransform().rotation;
  86. for (int i = 0; i < oframe.numObject; i++)
  87. {
  88. DetectedObject dobj = new DetectedObject(oframe.objectData[i], detectingmanager, campos, camrot);
  89. detObjects.Add(dobj);
  90. }
  91. }
  92. /// <summary>
  93. /// Returns the list of detected objects from this frame, but with only objects of the desired tracking state and
  94. /// minimum confidence included.
  95. /// <para>Note: The object detection module itself already filters confidence, adjustable with ZEDManager.objectDetectionConfidenceThreshold.
  96. /// It's simpler to set this value instead (via the Inspector) unless you want multiple filters.</para>
  97. /// </summary>
  98. /// <param name="tracking_ok">True to include objects where tracking data is known.</param>
  99. /// <param name="tracking_searching">True to include objects where the SDK is currently searching for its position.</param>
  100. /// <param name="tracking_off">True to include objects that have no tracking data at all.</param>
  101. /// <param name="confidencemin">Minimum confidence threshold.</param>
  102. public List<DetectedObject> GetFilteredObjectList(bool tracking_ok, bool tracking_searching, bool tracking_off, float confidencemin = 0)
  103. {
  104. List<DetectedObject> filteredobjects = new List<DetectedObject>();
  105. foreach(DetectedObject dobj in detObjects)
  106. {
  107. if (dobj.confidence < confidencemin && dobj.confidence != -1) continue;
  108. switch (dobj.trackingState)
  109. {
  110. case OBJECT_TRACK_STATE.OK:
  111. if (tracking_ok) filteredobjects.Add(dobj);
  112. break;
  113. case OBJECT_TRACK_STATE.SEARCHING:
  114. if (tracking_searching) filteredobjects.Add(dobj);
  115. break;
  116. case OBJECT_TRACK_STATE.OFF:
  117. if (tracking_off) filteredobjects.Add(dobj);
  118. break;
  119. }
  120. }
  121. return filteredobjects;
  122. }
  123. /// <summary>
  124. /// Releases all textures and ZEDMats in all detected objects.
  125. /// Call when the frame won't be used anymore to avoid memory leaks.
  126. /// </summary>
  127. public void CleanUpAllObjects()
  128. {
  129. foreach(DetectedObject dobj in detObjects)
  130. {
  131. dobj.CleanUpTextures();
  132. }
  133. }
  134. }