Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/stereolabs/zed-unity
Browse files Browse the repository at this point in the history
  • Loading branch information
Bvallon-sl committed Oct 2, 2024
2 parents 5aa0ebd + 5737dff commit e28af0b
Show file tree
Hide file tree
Showing 25 changed files with 514 additions and 843 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Material:
m_PrefabAsset: {fileID: 0}
m_Name: Mat_ZED_PointCloud
m_Shader: {fileID: 4800000, guid: e559735f38c1ab04bb202a47093cfd4f, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords:
- _EMISSION
Expand All @@ -18,6 +20,7 @@ Material:
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============

using System.Threading;
using UnityEngine;

/// <summary>
Expand Down Expand Up @@ -82,22 +83,23 @@ void Start()
}

if (zedManager != null)
{
zed = zedManager.zedCamera;

if (_pointMaterial == null)
{
_pointMaterial = new Material(Resources.Load("Materials/PointCloud/Mat_ZED_FusedPC_Point") as Material);
}
if (_pointMaterial == null)
{
_pointMaterial = new Material(Resources.Load("Materials/PointCloud/Mat_ZED_FusedPC_Point") as Material);
}

if (_diskMaterial == null)
{
_diskMaterial = new Material(Resources.Load("Materials/PointCloud/Mat_ZED_FusedPC_Disk") as Material);
}

_diskMaterial.hideFlags = HideFlags.DontSave;
_pointMaterial.hideFlags = HideFlags.DontSave;
zedManager.OnGrab += startMap;
if (_diskMaterial == null)
{
_diskMaterial = new Material(Resources.Load("Materials/PointCloud/Mat_ZED_FusedPC_Disk") as Material);
}

_diskMaterial.hideFlags = HideFlags.DontSave;
_pointMaterial.hideFlags = HideFlags.DontSave;
zedManager.OnGrab += startMap;
}
}

/// <summary>
Expand All @@ -107,9 +109,13 @@ private void startMap()
{
if (zed != null && notStarted)
{
zed.EnableSpatialMapping(sl.SPATIAL_MAP_TYPE.FUSED_POINT_CLOUD, resolution, range);
notStarted = false;
canUpdate = true;
sl.ERROR_CODE err = zed.EnableSpatialMapping(sl.SPATIAL_MAP_TYPE.FUSED_POINT_CLOUD, resolution, range);

if (err == sl.ERROR_CODE.SUCCESS)
{
notStarted = false;
canUpdate = true;
}
}

}
Expand Down Expand Up @@ -139,7 +145,7 @@ void OnDestroy()
/// </summary>
void Update()
{
if (zed.IsCameraReady && canUpdate) //Don't do anything unless the ZED has been initialized.
if (zed != null && zed.IsCameraReady && canUpdate) //Don't do anything unless the ZED has been initialized.
{
updateTime += Time.deltaTime;
if (updateTime >= 1)
Expand All @@ -148,6 +154,10 @@ void Update()
updateTime = 0;
}
}
else
{
Thread.Sleep(1);
}
}

/// <summary>
Expand Down
15 changes: 14 additions & 1 deletion ZEDCamera/Assets/SDK/Helpers/Scripts/ZEDManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ public enum shaderType

private sl.ObjectDetectionRuntimeParameters objectDetectionRuntimeParameters = new sl.ObjectDetectionRuntimeParameters();

public sl.CustomObjectDetectionRuntimeParameters customObjectDetectionRuntimeParameters = new sl.CustomObjectDetectionRuntimeParameters();

[HideInInspector]
public List<CustomBoxObjectData> customObjects = new List<CustomBoxObjectData>();

Expand Down Expand Up @@ -3236,7 +3238,18 @@ private void RetrieveObjectDetectionFrame()
{
sl.Objects objsbuffer = new sl.Objects();

sl.ERROR_CODE res = zedCamera.RetrieveObjects(ref objectDetectionRuntimeParameters, ref objsbuffer, objectDetectionInstanceID);
sl.ERROR_CODE res = sl.ERROR_CODE.FAILURE;

if (objectDetectionModel == sl.OBJECT_DETECTION_MODEL.CUSTOM_BOX_OBJECTS)
{
customObjectDetectionRuntimeParameters.objectClassDetectionProperties = new List<CustomObjectDetectionProperties>();
customObjectDetectionRuntimeParameters.objectDetectionProperties = new CustomObjectDetectionProperties();
res = zedCamera.RetrieveObjects(ref customObjectDetectionRuntimeParameters, ref objsbuffer, objectDetectionInstanceID);
}
else
{
res = zedCamera.RetrieveObjects(ref objectDetectionRuntimeParameters, ref objsbuffer, objectDetectionInstanceID);
}

if (res == sl.ERROR_CODE.SUCCESS && objsbuffer.isNew != 0)
{
Expand Down
168 changes: 166 additions & 2 deletions ZEDCamera/Assets/SDK/NativeInterface/ZEDCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public int TagInvisibleToZED
/// <summary>
/// Current Plugin Version.
/// </summary>
public static readonly System.Version PluginVersion = new System.Version(4, 1, 0);
public static readonly System.Version PluginVersion = new System.Version(4, 2, 0);

/******** DLL members ***********/
[DllImport(nameDll, EntryPoint = "GetRenderEventFunc")]
Expand Down Expand Up @@ -735,6 +735,9 @@ private static extern int dllz_enable_tracking(int cameraID, ref Quaternion quat
[DllImport(nameDll, EntryPoint = "sl_retrieve_objects")]
private static extern int dllz_retrieve_objects_data(int cameraID, ref ObjectDetectionRuntimeParameters od_params, ref Objects objs, uint instanceID);

[DllImport(nameDll, EntryPoint = "sl_retrieve_custom_objects")]
private static extern int dllz_retrieve_custom_objects(int cameraID, ref dll_customObjectDetectionRuntimeParameters od_params, ref Objects objs, uint instanceID);

[DllImport(nameDll, EntryPoint = "sl_enable_body_tracking")]
private static extern int dllz_enable_body_tracking(int cameraID, ref BodyTrackingParameters bt_params);

Expand Down Expand Up @@ -2436,6 +2439,22 @@ public float GetDepthValue(Vector3 pixel)
return d;
}

/// <summary>
/// Gets the current depth value of a pixel in the UNITS specified when the camera was started with Init().
/// <param name="position">The pixel's coordinates of the ZED Image as a Vector2.
/// <returns>Depth value as a float.</returns>
/// </summary>
public float GetDepthValue(Vector2 pixel)
{
if (!cameraReady)
{
return -1;
}

float d = dllz_get_depth_value(CameraID, (uint)pixel.x, (uint)pixel.y);
return d;
}

/// <summary>
/// Gets the current Euclidean distance (sqrt(x²+y²+z²)) of the targeted pixel of the screen to the camera.
/// May result in errors if the ZED image does not fill the whole screen.
Expand Down Expand Up @@ -3104,13 +3123,157 @@ public sl.ERROR_CODE IngestCustomBoxObjects(List<CustomBoxObjectData> objects_in
/// Retrieve object detection data
/// </summary>
/// <param name="od_params"> Object detection runtime parameters</param>
/// <param name="objFrame"> ObjectsFrameSDK that contains all the detection data</param>
/// <param name="objFrame"> Objects that contains all the detection data</param>
/// <param name="instanceID"> Id of the object detection instance. Used when multiple instances of the object detection module are enabled at the same time. </param>
/// <returns></returns>
public sl.ERROR_CODE RetrieveObjects(ref ObjectDetectionRuntimeParameters od_params, ref Objects objFrame, uint instanceID = 0)
{
return (sl.ERROR_CODE)dllz_retrieve_objects_data(CameraID, ref od_params, ref objFrame, instanceID);
}

[StructLayout(LayoutKind.Sequential)]
public struct dll_customObjectDetectionProperties
{
/// <summary>
/// Index of the class represented by this set of properties.
/// </summary>
public int classID;

/// <summary>
/// Whether the object object is kept or not.
/// </summary>
[MarshalAs(UnmanagedType.U1)]
public bool enabled;
/// <summary>
/// Confidence threshold.
/// From 1 to 100, with 1 meaning a low threshold, more uncertain objects and 99 very few but very precise objects.
/// If the scene contains a lot of objects, increasing the confidence can slightly speed up the process, since every object instance is tracked.
/// Default: 20.f
/// </summary>
public float detectionConfidenceThreshold;

/// <summary>
/// Provide hypothesis about the object movements(degrees of freedom or DoF) to improve the object tracking.
/// - true: 2 DoF projected alongside the floor plane. Case for object standing on the ground such as person, vehicle, etc.
/// The projection implies that the objects cannot be superposed on multiple horizontal levels.
/// - false: 6 DoF (full 3D movements are allowed).
/// This parameter cannot be changed for a given object tracking id.
/// It is advised to set it by labels to avoid issues.
/// </summary>
[MarshalAs(UnmanagedType.U1)]
public bool isGrounded;

/// <summary>
/// Provide hypothesis about the object staticity to improve the object tracking.
/// - true: the object will be assumed to never move nor being moved.
/// - false: the object will be assumed to be able to move or being moved.
/// </summary>
[MarshalAs(UnmanagedType.U1)]
public bool isStatic;

/// <summary>
/// Maximum tracking time threshold (in seconds) before dropping the tracked object when unseen for this amount of time.
/// By default, let the tracker decide internally based on the internal sub class of the tracked object.
/// Only valid for static object.
/// </summary>
public float trackingTimeout;

/// <summary>
/// Maximum tracking distance threshold (in meters) before dropping the tracked object when unseen for this amount of meters.
/// By default, do not discard tracked object based on distance.
/// Only valid for static object.
/// </summary>
public float trackingMaxDist;

/// <summary>
/// Maximum allowed width normalized to the image size.
/// Any prediction bigger than that will be filtered out.
/// Default: -1 (no filtering)
/// </summary>
public float maxBoxWidthNormalized;

/// <summary>
/// Minimum allowed width normalized to the image size.
/// Any prediction smaller than that will be filtered out.
/// Default: -1 (no filtering)
/// </summary>
public float minBoxWidthNormalized;

/// <summary>
/// Maximum allowed height normalized to the image size.
/// Any prediction bigger than that will be filtered out.
/// Default: -1 (no filtering)
/// </summary>
public float maxBoxHeightNormalized;

/// <summary>
/// Minimum allowed Height normalized to the image size.
/// Any prediction smaller than that will be filtered out.
/// Default: -1 (no filtering)
/// </summary>
public float minBoxHeightNormalized;

public dll_customObjectDetectionProperties(CustomObjectDetectionProperties customObjectDetectionProperties)
{
classID = customObjectDetectionProperties.classID;
enabled = customObjectDetectionProperties.enabled;
detectionConfidenceThreshold = customObjectDetectionProperties.detectionConfidenceThreshold;
isGrounded = customObjectDetectionProperties.isGrounded;
isStatic = customObjectDetectionProperties.isStatic;
trackingTimeout = customObjectDetectionProperties.trackingTimeout;
trackingMaxDist = customObjectDetectionProperties.trackingMaxDist;
maxBoxWidthNormalized = customObjectDetectionProperties.maxBoxWidthNormalized;
minBoxWidthNormalized = customObjectDetectionProperties.minBoxWidthNormalized;
maxBoxHeightNormalized = customObjectDetectionProperties.maxBoxHeightNormalized;
minBoxHeightNormalized = customObjectDetectionProperties.minBoxHeightNormalized;
}
};

/// <summary>
/// DLL-friendly version of CustomObjectDetectionRuntimeParameters (found in ZEDCommon.cs).
/// </summary>
[StructLayout(LayoutKind.Sequential)]
struct dll_customObjectDetectionRuntimeParameters
{
/// <summary>
/// Global object detection properties.
/// objectDetectionProperties is used as a fallback when CustomObjectDetectionRuntimeParameters.objectClassDetectionProperties is partially set.
/// </summary>
public dll_customObjectDetectionProperties objectDetectionProperties;

/// <summary>
/// Per class object detection properties.
/// </summary>
public IntPtr objectClassDetectionProperties;

/// <summary>
/// Size of the \ref objectClassDetectionProperties array.
/// </summary>
public uint numberCustomDetectionProperties;

public dll_customObjectDetectionRuntimeParameters(CustomObjectDetectionRuntimeParameters customObjectDetectionRuntimeParameters)
{
objectDetectionProperties = new dll_customObjectDetectionProperties(customObjectDetectionRuntimeParameters.objectDetectionProperties);
numberCustomDetectionProperties = (uint)customObjectDetectionRuntimeParameters.objectClassDetectionProperties.Count;
objectClassDetectionProperties = Marshal.AllocHGlobal(customObjectDetectionRuntimeParameters.objectClassDetectionProperties.Count * Marshal.SizeOf(typeof(dll_customObjectDetectionProperties)));
}
};

/// <summary>
/// Retrieve object detection data from custom object detection
/// </summary>
/// <param name="objectDetectionRuntimeParameters"> Custim object detection runtime parameters</param>
/// <param name="objFrame">Objects that contains all the detection data</param>
/// <param name="instanceID"> Id of the object detection instance. Used when multiple instances of the object detection module are enabled at the same time. </param>
/// <returns></returns>
public sl.ERROR_CODE RetrieveObjects(ref CustomObjectDetectionRuntimeParameters objectDetectionRuntimeParameters, ref Objects objFrame, uint instanceID = 0)
{
dll_customObjectDetectionRuntimeParameters dll_CustomObjectDetectionRuntime = new dll_customObjectDetectionRuntimeParameters(objectDetectionRuntimeParameters);
var e = (sl.ERROR_CODE)dllz_retrieve_custom_objects(CameraID, ref dll_CustomObjectDetectionRuntime, ref objFrame, instanceID);
Marshal.FreeHGlobal(dll_CustomObjectDetectionRuntime.objectClassDetectionProperties);
return e;
}

/// <summary>
/// Update the batch trajectories and retrieve the number of batches.
/// </summary>
Expand Down Expand Up @@ -3209,6 +3372,7 @@ public void DisableBodyTracking(uint bodyTrackingInstanceID = 1)
/// </summary>
/// <param name="body_params"> Body Tracking runtime parameters</param>
/// <param name="bodies"> Bodies that contains all the detection data</param>
/// <param name="instanceID"> Id of the object detection instance. Used when multiple instances of the object detection module are enabled at the same time. </param>
/// <returns></returns>
public sl.ERROR_CODE RetrieveBodies(ref BodyTrackingRuntimeParameters bt_params, ref Bodies bodies, uint instanceID = 0)
{
Expand Down
Loading

0 comments on commit e28af0b

Please sign in to comment.