浏览代码

Merge pull request #100 from Unity-Technologies/labelcache-bugfix

Fixed erratic behaviour where labeled objects that did not match any tracked label were still detected as foreground objects
/main
GitHub 4 年前
当前提交
1631cefd
共有 2 个文件被更改,包括 46 次插入2 次删除
  1. 7
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs
  2. 41
      com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs

7
com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs


const int k_StartingObjectCount = 1 << 8;
NativeList<ushort> m_InstanceIdToLabelEntryIndexLookup;
IdLabelConfig m_IdLabelConfig;
ushort m_DefaultValue;
ushort m_DefaultValue = ushort.MaxValue;
public LabelEntryMatchCache(IdLabelConfig idLabelConfig)
{

{
if (m_IdLabelConfig.TryGetMatchingConfigurationEntry(labeling, out _, out var index))
{
m_DefaultValue = ushort.MaxValue;
Debug.Assert(index < m_DefaultValue, "Too many entries in the label config");
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
{

m_InstanceIdToLabelEntryIndexLookup[i] = m_DefaultValue;
}
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = (ushort)index;
}
else if (m_InstanceIdToLabelEntryIndexLookup.Length > (int)instanceId)
{
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = m_DefaultValue;
}
}

41
com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs


using System.Collections;
using NUnit.Framework;
using Unity.Entities;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.TestTools;

//allow label to be registered
yield return null;
Assert.IsFalse(cache.TryGetLabelEntryFromInstanceId(labeledPlane.GetComponent<Labeling>().instanceId, out var labelEntry, out var index));
Assert.AreEqual(-1, index);
Assert.AreEqual(default(IdLabelEntry), labelEntry);
}
}
[UnityTest]
public IEnumerator TryGet_ReturnsFalse_ForNonMatchingLabel_WhenAllObjectsAreDestroyedAndNewOnesAreCreated()
{
//only way to guarantee registration order is to run frames.
var labeledPlane = TestHelper.CreateLabeledPlane(label: "foo");
var config = ScriptableObject.CreateInstance<IdLabelConfig>();
config.Init(new[]
{
new IdLabelEntry()
{
id = 1,
label = "foo"
},
});
using (var cache = new LabelEntryMatchCache(config))
{
//allow label to be registered
yield return null;
//delete all labeled objects and run a frame so that instance ids of labeled entities reset
DestroyTestObject(labeledPlane);
yield return null;
//this new object has a label that is not included in our label config
var labeledPlane2 = TestHelper.CreateLabeledPlane(label: "bar");
AddTestObjectForCleanup(labeledPlane2);
//let labeledPlane2 be assigned a recycled instance id (1) previously belonging to labeledPlane
yield return null;
Assert.IsFalse(cache.TryGetLabelEntryFromInstanceId(labeledPlane2.GetComponent<Labeling>().instanceId, out var labelEntry, out var index));
Assert.AreEqual(-1, index);
Assert.AreEqual(default(IdLabelEntry), labelEntry);
}

正在加载...
取消
保存