using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using sl;
///
/// Holds all objects detected by the ZED Object Detection module from a single ZED camera during a single frame.
/// Holds metadata about the frame and camera, and provides helper functions for filtering out the detected objects.
/// This is provided when subscribed to the ZEDManager.OnObjectDetection event.
///
/// This is a higher level version of sl.ObjectsFrame, which comes directly from the ZED SDK and doesn't follow Unity conventions.
///
public class DetectionFrame
{
private ObjectsFrameSDK objectsFrame;
///
/// The raw ObjectsFrame object that this object is an abstraction of - ObjectsFrame comes
/// directly from the SDk and doesn't follow Unity conventions.
///
public ObjectsFrameSDK rawObjectsFrame
{
get
{
return objectsFrame;
}
}
///
/// Timestamp of when the object detection module finished detecting the frame. (TODO: Verify.)
///
public ulong timestamp
{
get
{
return objectsFrame.timestamp;
}
}
private int frameDetected = -1;
///
/// Value of Time.frameCount when this object was created. Assumes the constructor was called in the same frame
/// that the object was detected by the SDK.
///
public int frameCountAtDetection
{
get
{
return frameDetected;
}
}
///
/// How many objects were detected in total.
/// Note this does not include any filtering, so it includes objects in the SEARCHING and OFF tracking states.
///
public int objectCount
{
get
{
return objectsFrame.numObject;
}
}
///
/// The manager class responsible for the ZED camera that detected the objects in this frame.
///
public ZEDManager detectingZEDManager;
private List detObjects = new List();
///
/// All objects detected within this frame. Use GetFilteredObjectList to filter them by category or confidence.
///
public List detectedObjects
{
get
{
return detObjects;
}
}
///
/// Constructor that sets up this frame and spawns DetectedObject objects for each raw ObjectData object in the frame.
///
/// Raw sl.ObjectsFrame object from the SDK, that this object is an abstraction of.
/// ZEDManager that represents the camera that detected this frame.
public DetectionFrame(ObjectsFrameSDK oframe, ZEDManager detectingmanager)
{
objectsFrame = oframe;
detectingZEDManager = detectingmanager;
frameDetected = Time.frameCount;
Vector3 campos = detectingmanager.GetLeftCameraTransform().position;
Quaternion camrot = detectingmanager.GetLeftCameraTransform().rotation;
for (int i = 0; i < oframe.numObject; i++)
{
DetectedObject dobj = new DetectedObject(oframe.objectData[i], detectingmanager, campos, camrot);
detObjects.Add(dobj);
}
}
///
/// Returns the list of detected objects from this frame, but with only objects of the desired tracking state and
/// minimum confidence included.
/// Note: The object detection module itself already filters confidence, adjustable with ZEDManager.objectDetectionConfidenceThreshold.
/// It's simpler to set this value instead (via the Inspector) unless you want multiple filters.
///
/// True to include objects where tracking data is known.
/// True to include objects where the SDK is currently searching for its position.
/// True to include objects that have no tracking data at all.
/// Minimum confidence threshold.
public List GetFilteredObjectList(bool tracking_ok, bool tracking_searching, bool tracking_off, float confidencemin = 0)
{
List filteredobjects = new List();
foreach(DetectedObject dobj in detObjects)
{
if (dobj.confidence < confidencemin && dobj.confidence != -1) continue;
switch (dobj.trackingState)
{
case OBJECT_TRACK_STATE.OK:
if (tracking_ok) filteredobjects.Add(dobj);
break;
case OBJECT_TRACK_STATE.SEARCHING:
if (tracking_searching) filteredobjects.Add(dobj);
break;
case OBJECT_TRACK_STATE.OFF:
if (tracking_off) filteredobjects.Add(dobj);
break;
}
}
return filteredobjects;
}
///
/// Releases all textures and ZEDMats in all detected objects.
/// Call when the frame won't be used anymore to avoid memory leaks.
///
public void CleanUpAllObjects()
{
foreach(DetectedObject dobj in detObjects)
{
dobj.CleanUpTextures();
}
}
}