浏览代码

Merge pull request #9 from Unity-Technologies/1-based-labels

Custom label ids & LabelingConfiguration cleanup
/main
GitHub 4 年前
当前提交
d052596b
共有 17 个文件被更改,包括 266 次插入105 次删除
  1. 13
      TestProjects/PerceptionHDRP/Assets/LabelingConfiguration.asset
  2. 2
      TestProjects/PerceptionHDRP/Packages/manifest.json
  3. 4
      TestProjects/PerceptionHDRP/ProjectSettings/ProjectVersion.txt
  4. 13
      TestProjects/PerceptionURP/Assets/ExampleLabelingConfiguration.asset
  5. 2
      TestProjects/PerceptionURP/Packages/manifest.json
  6. 4
      TestProjects/PerceptionURP/ProjectSettings/ProjectVersion.txt
  7. 104
      com.unity.perception/Editor/GroundTruth/LabelingConfigurationEditor.cs
  8. 62
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelingConfiguration.cs
  9. 36
      com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs
  10. 20
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  11. 47
      com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs
  12. 6
      com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs
  13. 20
      com.unity.perception/Tests/Runtime/GroundTruthTests/BoundingBox2DTests.cs
  14. 9
      com.unity.perception/Tests/Runtime/GroundTruthTests/ObjectCountTests.cs
  15. 5
      com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs
  16. 21
      com.unity.perception/Runtime/GroundTruth/Labeling/StartingLabelId.cs
  17. 3
      com.unity.perception/Runtime/GroundTruth/Labeling/StartingLabelId.cs.meta

13
TestProjects/PerceptionHDRP/Assets/LabelingConfiguration.asset


m_Script: {fileID: 11500000, guid: bad10bec3eccd8e49a9d725b2c30f74c, type: 3}
m_Name: LabelingConfiguration
m_EditorClassIdentifier:
LabelingConfigurations:
- label: Box
AutoAssignIds: 1
StartingLabelId: 1
LabelEntries:
- id: 1
label: Box
- label: Crate
- id: 2
label: Crate
- label: Cube
- id: 3
label: Cube
value: 30000

2
TestProjects/PerceptionHDRP/Packages/manifest.json


"com.unity.collab-proxy": "1.2.16",
"com.unity.ext.nunit": "1.0.0",
"com.unity.ide.rider": "1.1.4",
"com.unity.ide.vscode": "1.1.4",
"com.unity.ide.vscode": "1.2.0",
"com.unity.package-validation-suite": "0.9.1-preview",
"com.unity.perception": "file:../../../com.unity.perception",
"com.unity.render-pipelines.core": "7.3.1",

4
TestProjects/PerceptionHDRP/ProjectSettings/ProjectVersion.txt


m_EditorVersion: 2019.3.9f1
m_EditorVersionWithRevision: 2019.3.9f1 (e6e740a1c473)
m_EditorVersion: 2019.3.13f1
m_EditorVersionWithRevision: 2019.3.13f1 (d4ddf0d95db9)

13
TestProjects/PerceptionURP/Assets/ExampleLabelingConfiguration.asset


m_Script: {fileID: 11500000, guid: bad10bec3eccd8e49a9d725b2c30f74c, type: 3}
m_Name: ExampleLabelingConfiguration
m_EditorClassIdentifier:
LabelingConfigurations:
- label: Box
AutoAssignIds: 1
StartingLabelId: 1
LabelEntries:
- id: 1
label: Box
- label: Cube
- id: 2
label: Cube
- label: Crate
- id: 3
label: Crate
value: 30000

2
TestProjects/PerceptionURP/Packages/manifest.json


"dependencies": {
"com.unity.collab-proxy": "1.2.16",
"com.unity.ide.rider": "1.1.4",
"com.unity.ide.vscode": "1.1.4",
"com.unity.ide.vscode": "1.2.0",
"com.unity.package-validation-suite": "0.9.1-preview",
"com.unity.perception": "file:../../../com.unity.perception",
"com.unity.render-pipelines.universal": "7.3.1",

4
TestProjects/PerceptionURP/ProjectSettings/ProjectVersion.txt


m_EditorVersion: 2019.3.9f1
m_EditorVersionWithRevision: 2019.3.9f1 (e6e740a1c473)
m_EditorVersion: 2019.3.13f1
m_EditorVersionWithRevision: 2019.3.13f1 (d4ddf0d95db9)

104
com.unity.perception/Editor/GroundTruth/LabelingConfigurationEditor.cs


using System;
using Unity.Mathematics;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;

class LabelingConfigurationEditor : Editor
{
ReorderableList m_LabelsList;
const float k_Margin = 5f;
m_LabelsList = new ReorderableList(this.serializedObject, this.serializedObject.FindProperty(nameof(LabelingConfiguration.LabelingConfigurations)), true, false, true, true);
m_LabelsList.elementHeight = EditorGUIUtility.singleLineHeight * 2;
m_LabelsList = new ReorderableList(this.serializedObject, this.serializedObject.FindProperty(nameof(LabelingConfiguration.LabelEntries)), true, false, true, true);
m_LabelsList.elementHeight = EditorGUIUtility.singleLineHeight * 3 + k_Margin;
m_LabelsList.onReorderCallbackWithDetails += OnReorder;
}
void OnReorder(ReorderableList list, int oldIndex, int newIndex)
{
if (!autoAssign)
return;
AutoAssignIds();
}
void OnRemove(ReorderableList list)

if (autoAssign)
AutoAssignIds();
this.serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target);
}

int maxLabel = Int32.MinValue;
if (list.serializedProperty.arraySize == 0)
maxLabel = -1;
for (int i = 0; i < list.serializedProperty.arraySize; i++)
{
var item = list.serializedProperty.GetArrayElementAtIndex(i);
maxLabel = math.max(maxLabel, item.FindPropertyRelative(nameof(LabelEntry.id)).intValue);
}
var labelProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.label));
var idProperty = element.FindPropertyRelative(nameof(LabelEntry.id));
idProperty.intValue = maxLabel + 1;
var labelProperty = element.FindPropertyRelative(nameof(LabelEntry.label));
var valueProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.value));
var valueProperty = element.FindPropertyRelative(nameof(LabelEntry.value));
if (autoAssign)
AutoAssignIds();
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target);
}

var element = m_LabelsList.serializedProperty.GetArrayElementAtIndex(index);
var labelProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.label));
var valueProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.value));
var idProperty = element.FindPropertyRelative(nameof(LabelEntry.id));
var labelProperty = element.FindPropertyRelative(nameof(LabelEntry.label));
var valueProperty = element.FindPropertyRelative(nameof(LabelEntry.value));
var newLabel = EditorGUI.TextField(contentRect, nameof(LabelingConfigurationEntry.label), labelProperty.stringValue);
using (new EditorGUI.DisabledScope(autoAssign))
{
var newLabel = EditorGUI.IntField(contentRect, nameof(LabelEntry.id), idProperty.intValue);
if (change.changed)
{
idProperty.intValue = newLabel;
if (autoAssign)
AutoAssignIds();
}
}
}
using (var change = new EditorGUI.ChangeCheckScope())
{
var contentRect = new Rect(rect.position + new Vector2(0, EditorGUIUtility.singleLineHeight), new Vector2(rect.width, EditorGUIUtility.singleLineHeight));
var newLabel = EditorGUI.TextField(contentRect, nameof(LabelEntry.label), labelProperty.stringValue);
{
}
var contentRect = new Rect(rect.position + new Vector2(0, EditorGUIUtility.singleLineHeight), new Vector2(rect.width, EditorGUIUtility.singleLineHeight));
var newValue = EditorGUI.IntField(contentRect, nameof(LabelingConfigurationEntry.value), valueProperty.intValue);
var contentRect = new Rect(rect.position + new Vector2(0, EditorGUIUtility.singleLineHeight * 2), new Vector2(rect.width, EditorGUIUtility.singleLineHeight));
var newValue = EditorGUI.IntField(contentRect, nameof(LabelEntry.value), valueProperty.intValue);
bool autoAssign => serializedObject.FindProperty(nameof(LabelingConfiguration.AutoAssignIds)).boolValue;
serializedObject.Update();
var autoAssignIdsProperty = serializedObject.FindProperty(nameof(LabelingConfiguration.AutoAssignIds));
using (var change = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(autoAssignIdsProperty, new GUIContent("Auto Assign IDs"));
if (change.changed && autoAssignIdsProperty.boolValue)
AutoAssignIds();
}
if (autoAssignIdsProperty.boolValue)
{
using (var change = new EditorGUI.ChangeCheckScope())
{
var startingLabelIdProperty = serializedObject.FindProperty(nameof(LabelingConfiguration.StartingLabelId));
EditorGUILayout.PropertyField(startingLabelIdProperty, new GUIContent("Starting Label ID"));
if (change.changed)
AutoAssignIds();
}
}
}
void AutoAssignIds()
{
var serializedProperty = serializedObject.FindProperty(nameof(LabelingConfiguration.LabelEntries));
var size = serializedProperty.arraySize;
if (size == 0)
return;
var startingLabelId = (StartingLabelId)serializedObject.FindProperty(nameof(LabelingConfiguration.StartingLabelId)).enumValueIndex;
var nextId = startingLabelId == StartingLabelId.One ? 1 : 0;
for (int i = 0; i < size; i++)
{
serializedProperty.GetArrayElementAtIndex(i).FindPropertyRelative(nameof(LabelEntry.id)).intValue = nextId;
nextId++;
}
}
}
}

62
com.unity.perception/Runtime/GroundTruth/Labeling/LabelingConfiguration.cs


using System;
using System.Collections.Generic;
using UnityEngine.Serialization;
namespace UnityEngine.Perception.GroundTruth
{

public class LabelingConfiguration : ScriptableObject
{
/// <summary>
/// A sequence of <see cref="LabelingConfigurationEntry"/> which defines the labels relevant for this configuration and their values.
/// Whether the inspector will auto-assign ids based on the id of the first element.
/// </summary>
public bool AutoAssignIds = true;
/// <summary>
/// Whether the inspector will start label ids at zero or one when <see cref="AutoAssignIds"/> is enabled.
public StartingLabelId StartingLabelId = StartingLabelId.One;
/// <summary>
/// A sequence of <see cref="LabelEntry"/> which defines the labels relevant for this configuration and their values.
/// </summary>
[FormerlySerializedAs("LabelingConfigurations")]
public List<LabelingConfigurationEntry> LabelingConfigurations = new List<LabelingConfigurationEntry>();
public List<LabelEntry> LabelEntries = new List<LabelEntry>();
/// Attempts to find the matching index in <see cref="LabelingConfigurations"/> for the given <see cref="Labeling"/>.
/// Attempts to find the matching index in <see cref="LabelEntries"/> for the given <see cref="Labeling"/>.
/// The matching index is the first class name in the given Labeling which matches an entry in <see cref="LabelingConfigurations"/>.
/// The matching index is the first class name in the given Labeling which matches an entry in <see cref="LabelEntries"/>.
/// <param name="index">When this method returns, contains the index of the matching <see cref="LabelingConfigurationEntry"/>, or -1 if no match was found.</param>
/// <param name="labelEntry">When this method returns, contains the matching <see cref="LabelEntry"/>, or <code>default</code> if no match was found.</param>
public bool TryGetMatchingConfigurationIndex(Labeling labeling, out int index)
public bool TryGetMatchingConfigurationEntry(Labeling labeling, out LabelEntry labelEntry)
{
return TryGetMatchingConfigurationEntry(labeling, out labelEntry, out int _);
}
/// <summary>
/// Attempts to find the matching index in <see cref="LabelEntries"/> for the given <see cref="Labeling"/>.
/// </summary>
/// <remarks>
/// The matching index is the first class name in the given Labeling which matches an entry in <see cref="LabelEntries"/>.
/// </remarks>
/// <param name="labeling">The <see cref="Labeling"/> to match </param>
/// <param name="labelEntry">When this method returns, contains the matching <see cref="LabelEntry"/>, or <code>default</code> if no match was found.</param>
/// <param name="labelEntryIndex">When this method returns, contains the index of the matching <see cref="LabelEntry"/>, or <code>-1</code> if no match was found.</param>
/// <returns>Returns true if a match was found. False if not.</returns>
public bool TryGetMatchingConfigurationEntry(Labeling labeling, out LabelEntry labelEntry, out int labelEntryIndex)
for (var i = 0; i < LabelingConfigurations.Count; i++)
for (var i = 0; i < LabelEntries.Count; i++)
var configuration = LabelingConfigurations[i];
if (string.Equals(configuration.label, labelingClass))
var entry = LabelEntries[i];
if (string.Equals(entry.label, labelingClass))
index = i;
labelEntry = entry;
labelEntryIndex = i;
index = -1;
labelEntryIndex = -1;
labelEntry = default;
return false;
}
}

/// </summary>
[Serializable]
public struct LabelingConfigurationEntry
public struct LabelEntry
/// The id associated with the label. Used to associate objects with labels in various forms of ground truth.
/// </summary>
public int id;
/// <summary>
/// The label string
/// </summary>
public string label;

public int value;
/// <param name="id">The id associated with the label. Used to associate objects with labels in various forms of ground truth.</param>
public LabelingConfigurationEntry(string label, int value)
public LabelEntry(int id, string label, int value)
this.id = id;
this.label = label;
this.value = value;
}

36
com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs


ComputeBuffer m_InstanceIdPresenceMask;
ComputeBuffer m_InstanceIdToClassId;
ComputeBuffer m_ClassCounts;
NativeList<int> m_InstanceIdToClassIdLookup;
NativeList<int> m_InstanceIdToLabelIndexLookup;
HashSet<Camera> m_CamerasRendered = new HashSet<Camera>();
bool m_IdBuffersNeedUpdating;
bool m_DidComputeLastFrame;

public override void SetupMaterialProperties(MaterialPropertyBlock mpb, MeshRenderer meshRenderer, Labeling labeling, uint instanceId)
{
if (!m_InstanceIdToClassIdLookup.IsCreated)
if (!m_InstanceIdToLabelIndexLookup.IsCreated)
m_InstanceIdToClassIdLookup = new NativeList<int>(k_StartingObjectCount, Allocator.Persistent);
m_InstanceIdToLabelIndexLookup = new NativeList<int>(k_StartingObjectCount, Allocator.Persistent);
if (LabelingConfiguration.TryGetMatchingConfigurationIndex(labeling, out var index))
if (LabelingConfiguration.TryGetMatchingConfigurationEntry(labeling, out LabelEntry labelEntry, out var index))
if (m_InstanceIdToClassIdLookup.Length <= instanceId)
if (m_InstanceIdToLabelIndexLookup.Length <= instanceId)
m_InstanceIdToClassIdLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
m_InstanceIdToLabelIndexLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
m_InstanceIdToClassIdLookup[(int)instanceId] = index + 1;
m_InstanceIdToLabelIndexLookup[(int)instanceId] = index + 1;
}
}

var objectCount = k_StartingObjectCount;
UpdateIdBufferSizes(objectCount);
m_ClassCounts = new ComputeBuffer(LabelingConfiguration.LabelingConfigurations.Count + 1, UnsafeUtility.SizeOf<uint>(), ComputeBufferType.Structured);
m_ClassCounts = new ComputeBuffer(LabelingConfiguration.LabelEntries.Count + 1, UnsafeUtility.SizeOf<uint>(), ComputeBufferType.Structured);
RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
}

protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
//If there are no objects to label, skip the pass
if (!m_InstanceIdToClassIdLookup.IsCreated || m_InstanceIdToClassIdLookup.Length == 0)
if (!m_InstanceIdToLabelIndexLookup.IsCreated || m_InstanceIdToLabelIndexLookup.Length == 0)
var counts = new NativeArray<uint>(LabelingConfiguration.LabelingConfigurations.Count + 1, Allocator.Temp);
var counts = new NativeArray<uint>(LabelingConfiguration.LabelEntries.Count + 1, Allocator.Temp);
OnClassCountReadback(Time.frameCount, counts);
counts.Dispose();
return;

if (m_IdBuffersNeedUpdating)
{
UpdateIdBufferSizes(m_InstanceIdToClassIdLookup.Capacity);
m_InstanceIdToClassId.SetData(m_InstanceIdToClassIdLookup.AsArray());
UpdateIdBufferSizes(m_InstanceIdToLabelIndexLookup.Capacity);
m_InstanceIdToClassId.SetData(m_InstanceIdToLabelIndexLookup.AsArray());
}
//The following section kicks off the four kernels in LabeledObjectHistogram.compute

cmd.SetComputeBufferParam(m_ComputeShader, 3, "InstanceIdPresenceMask", m_InstanceIdPresenceMask);
cmd.SetComputeBufferParam(m_ComputeShader, 3, "InstanceIdToClassId", m_InstanceIdToClassId);
cmd.SetComputeBufferParam(m_ComputeShader, 3, "ClassCounts", m_ClassCounts);
cmd.DispatchCompute(m_ComputeShader, 3, m_InstanceIdToClassIdLookup.Length, 1, 1);
cmd.DispatchCompute(m_ComputeShader, 3, m_InstanceIdToLabelIndexLookup.Length, 1, 1);
var requestFrameCount = Time.frameCount;
cmd.RequestAsyncReadback(m_ClassCounts, request => OnClassCountReadback(requestFrameCount, request.GetData<uint>()));

m_ClassCounts = null;
WaitForAllRequests();
if (m_InstanceIdToClassIdLookup.IsCreated)
if (m_InstanceIdToLabelIndexLookup.IsCreated)
m_InstanceIdToClassIdLookup.Dispose();
m_InstanceIdToClassIdLookup = default;
m_InstanceIdToLabelIndexLookup.Dispose();
m_InstanceIdToLabelIndexLookup = default;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelingConfigurationEntry>, int> ClassCountsReceived;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> ClassCountsReceived;
void OnClassCountReadback(int requestFrameCount, NativeArray<uint> counts)
{

Debug.Log(sb);
#endif
ClassCountsReceived?.Invoke(new NativeSlice<uint>(counts, 1), LabelingConfiguration.LabelingConfigurations, requestFrameCount);
ClassCountsReceived?.Invoke(new NativeSlice<uint>(counts, 1), LabelingConfiguration.LabelEntries, requestFrameCount);
}
public void WaitForAllRequests()

20
com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs


internal event Action<int, NativeArray<uint>> segmentationImageReceived;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelingConfigurationEntry>, int> classCountsReceived;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> classCountsReceived;
[NonSerialized]
internal RenderTexture labelingTexture;

if (produceSegmentationImages)
{
var specs = LabelingConfiguration.LabelingConfigurations.Select((l, index) => new SemanticSegmentationSpec()
var specs = LabelingConfiguration.LabelEntries.Select((l) => new SemanticSegmentationSpec()
label_id = index,
label_id = l.id,
label_name = l.label,
pixel_value = l.value
}).ToArray();

if (produceObjectCountAnnotations || produceBoundingBoxAnnotations || produceRenderedObjectInfoMetric)
{
var labelingMetricSpec = LabelingConfiguration.LabelingConfigurations.Select((l, index) => new ObjectCountSpec()
var labelingMetricSpec = LabelingConfiguration.LabelEntries.Select((l) => new ObjectCountSpec()
label_id = index,
label_id = l.id,
label_name = l.label,
}).ToArray();

renderedObjectInfosCalculated?.Invoke(frameCount, renderedObjectInfos);
if (produceObjectCountAnnotations)
OnObjectCountsReceived(classCounts, LabelingConfiguration.LabelingConfigurations, frameCount);
OnObjectCountsReceived(classCounts, LabelingConfiguration.LabelEntries, frameCount);
ProduceBoundingBoxesAnnotation(renderedObjectInfos, LabelingConfiguration.LabelingConfigurations, frameCount);
ProduceBoundingBoxesAnnotation(renderedObjectInfos, LabelingConfiguration.LabelEntries, frameCount);
if (produceRenderedObjectInfoMetric)
ProduceRenderedObjectInfoMetric(renderedObjectInfos, frameCount);

#endif
void ProduceBoundingBoxesAnnotation(NativeArray<RenderedObjectInfo> renderedObjectInfos, List<LabelingConfigurationEntry> labelingConfigurations, int frameCount)
void ProduceBoundingBoxesAnnotation(NativeArray<RenderedObjectInfo> renderedObjectInfos, List<LabelEntry> labelingConfigurations, int frameCount)
{
using (s_BoundingBoxCallback.Auto())
{

return m_RenderedObjectInfoGenerator.TryGetLabelIdFromInstanceId(instanceId, out labelId);
}
void OnObjectCountsReceived(NativeSlice<uint> counts, IReadOnlyList<LabelingConfigurationEntry> entries, int frameCount)
void OnObjectCountsReceived(NativeSlice<uint> counts, IReadOnlyList<LabelEntry> entries, int frameCount)
{
using (s_ClassCountCallback.Auto())
{

{
m_ClassCountValues[i] = new ClassCountValue()
{
label_id = i,
label_id = entries[i].id,
label_name = entries[i].label,
count = counts[i]
};

47
com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs


}
}
NativeList<int> m_InstanceIdToClassIdLookup;
NativeList<int> m_InstanceIdToLabelEntryIndexLookup;
LabelingConfiguration m_LabelingConfiguration;
// ReSharper disable once InvalidXmlDocComment

public RenderedObjectInfoGenerator(LabelingConfiguration labelingConfiguration)
{
m_LabelingConfiguration = labelingConfiguration;
m_InstanceIdToClassIdLookup = new NativeList<int>(k_StartingObjectCount, Allocator.Persistent);
m_InstanceIdToLabelEntryIndexLookup = new NativeList<int>(k_StartingObjectCount, Allocator.Persistent);
if (m_LabelingConfiguration.TryGetMatchingConfigurationIndex(labeling, out var index))
if (m_LabelingConfiguration.TryGetMatchingConfigurationEntry(labeling, out var entry, out var index))
if (m_InstanceIdToClassIdLookup.Length <= instanceId)
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
m_InstanceIdToClassIdLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
m_InstanceIdToLabelEntryIndexLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
m_InstanceIdToClassIdLookup[(int)instanceId] = index;
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = index;
}
}

/// InstanceSegmentationRawData should be the raw data from a texture filled by <see cref="InstanceSegmentationUrpPass"/> or <see cref="InstanceSegmentationPass"/>
/// using the same LabelingConfiguration that was passed into this object.
/// </summary>
/// <param name="instanceSegmentationRawData"></param>
/// <param name="stride"></param>
/// <param name="boundingBoxOrigin"></param>
/// <param name="boundingBoxes"></param>
/// <param name="classCounts"></param>
/// <param name="allocator"></param>
public void Compute(NativeArray<uint> instanceSegmentationRawData, int stride, BoundingBoxOrigin boundingBoxOrigin, out NativeArray<RenderedObjectInfo> boundingBoxes, out NativeArray<uint> classCounts, Allocator allocator)
/// <param name="instanceSegmentationRawData">The raw instance segmentation image.</param>
/// <param name="stride">Stride of the image data. Should be equal to the width of the image.</param>
/// <param name="boundingBoxOrigin">Whether bounding boxes should be top-left or bottom-right-based.</param>
/// <param name="renderedObjectInfos">When this method returns, filled with RenderedObjectInfo entries for each object visible in the frame.</param>
/// <param name="perLabelEntryObjectCount">When the method returns, filled with a NativeArray with the count of objects for each entry in <see cref="LabelingConfiguration.LabelEntries"/> in the LabelingConfiguration passed into the constructor.</param>
/// <param name="allocator">The allocator to use for allocating renderedObjectInfos and perLabelEntryObjectCount.</param>
public void Compute(NativeArray<uint> instanceSegmentationRawData, int stride, BoundingBoxOrigin boundingBoxOrigin, out NativeArray<RenderedObjectInfo> renderedObjectInfos, out NativeArray<uint> perLabelEntryObjectCount, Allocator allocator)
{
const int jobCount = 24;
var height = instanceSegmentationRawData.Length / stride;

JobHandle.CompleteAll(handles);
}
classCounts = new NativeArray<uint>(m_LabelingConfiguration.LabelingConfigurations.Count, allocator);
perLabelEntryObjectCount = new NativeArray<uint>(m_LabelingConfiguration.LabelEntries.Count, allocator);
var boundingBoxMap = new NativeHashMap<int, RenderedObjectInfo>(100, Allocator.Temp);
using (s_LabelMerge.Auto())
{

}
var keyValueArrays = boundingBoxMap.GetKeyValueArrays(Allocator.Temp);
boundingBoxes = new NativeArray<RenderedObjectInfo>(keyValueArrays.Keys.Length, allocator);
renderedObjectInfos = new NativeArray<RenderedObjectInfo>(keyValueArrays.Keys.Length, allocator);
if (m_InstanceIdToClassIdLookup.Length <= instanceId)
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
var classId = m_InstanceIdToClassIdLookup[instanceId];
classCounts[classId]++;
var labelIndex = m_InstanceIdToLabelEntryIndexLookup[instanceId];
var labelId = m_LabelingConfiguration.LabelEntries[labelIndex].id;
perLabelEntryObjectCount[labelIndex]++;
var renderedObjectInfo = keyValueArrays.Values[i];
var boundingBox = renderedObjectInfo.boundingBox;
if (boundingBoxOrigin == BoundingBoxOrigin.TopLeft)

}
boundingBoxes[i] = new RenderedObjectInfo
renderedObjectInfos[i] = new RenderedObjectInfo
labelId = classId,
labelId = labelId,
boundingBox = boundingBox,
pixelCount = renderedObjectInfo.pixelCount
};

public bool TryGetLabelIdFromInstanceId(int instanceId, out int labelId)
{
labelId = -1;
if (m_InstanceIdToClassIdLookup.Length <= instanceId)
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
labelId = m_InstanceIdToClassIdLookup[instanceId];
labelId = m_InstanceIdToLabelEntryIndexLookup[instanceId];
return true;
}

m_InstanceIdToClassIdLookup.Dispose();
m_InstanceIdToLabelEntryIndexLookup.Dispose();
}
}
}

6
com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs


using System;
using System;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;

public override void SetupMaterialProperties(MaterialPropertyBlock mpb, MeshRenderer meshRenderer, Labeling labeling, uint instanceId)
{
var entry = new LabelingConfigurationEntry();
foreach (var l in m_LabelingConfiguration.LabelingConfigurations)
var entry = new LabelEntry();
foreach (var l in m_LabelingConfiguration.LabelEntries)
{
if (labeling.labels.Contains(l.label))
{

20
com.unity.perception/Tests/Runtime/GroundTruthTests/BoundingBox2DTests.cs


{
boundingBox = new Rect(0, 0, 2, 2),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 4
}
}, new uint[]

{
boundingBox = new Rect(0, 0, 1, 2),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 2
},
new RenderedObjectInfo()

labelId = 1,
labelId = 2,
pixelCount = 1
}
}, new uint[]

{
boundingBox = new Rect(0, 0, 3, 2),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 4
},
new RenderedObjectInfo()

labelId = 1,
labelId = 2,
pixelCount = 2
}
}, new uint[]

{
boundingBox = new Rect(1, 0, 1, 1),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 1
},
}, new uint[]

var label2 = "label2";
var labelingConfiguration = ScriptableObject.CreateInstance<LabelingConfiguration>();
labelingConfiguration.LabelingConfigurations = new List<LabelingConfigurationEntry>
labelingConfiguration.LabelEntries = new List<LabelEntry>
new LabelingConfigurationEntry
new LabelEntry
id = 1,
new LabelingConfigurationEntry
new LabelEntry
id = 2,
label = label2,
value = 500
}

9
com.unity.perception/Tests/Runtime/GroundTruthTests/ObjectCountTests.cs


var label = "label";
var labelingConfiguration = ScriptableObject.CreateInstance<LabelingConfiguration>();
labelingConfiguration.LabelingConfigurations = new List<LabelingConfigurationEntry>
labelingConfiguration.LabelEntries = new List<LabelEntry>
new LabelingConfigurationEntry
new LabelEntry
id = 1,
var receivedResults = new List<(uint[] counts, LabelingConfigurationEntry[] labels, int frameCount)>();
var receivedResults = new List<(uint[] counts, LabelEntry[] labels, int frameCount)>();
var cameraObject = SetupCamera(labelingConfiguration, (counts, labels, frameCount) =>
{

}
static GameObject SetupCamera(LabelingConfiguration labelingConfiguration,
Action<NativeSlice<uint>, IReadOnlyList<LabelingConfigurationEntry>, int> onClassCountsReceived)
Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> onClassCountsReceived)
{
var cameraObject = new GameObject();
cameraObject.SetActive(false);

5
com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs


var label = "label";
var labelingConfiguration = ScriptableObject.CreateInstance<LabelingConfiguration>();
labelingConfiguration.LabelingConfigurations = new List<LabelingConfigurationEntry>
labelingConfiguration.LabelEntries = new List<LabelEntry>
new LabelingConfigurationEntry
new LabelEntry
id = 1,
label = label,
value = 500
}

21
com.unity.perception/Runtime/GroundTruth/Labeling/StartingLabelId.cs


using System;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// Selector for whether label ids should start at zero or one. <seealso cref="LabelingConfiguration.StartingLabelId"/>.
/// </summary>
public enum StartingLabelId
{
/// <summary>
/// Start label id assignment at 0
/// </summary>
[InspectorName("0")]
Zero,
/// <summary>
/// Start label id assignment at 1
/// </summary>
[InspectorName("1")]
One
}
}

3
com.unity.perception/Runtime/GroundTruth/Labeling/StartingLabelId.cs.meta


fileFormatVersion: 2
guid: 2f8f1d8ca6304fab94bf1c45c9ad0873
timeCreated: 1589210485
正在加载...
取消
保存