您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

182 行
6.9 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.GroundTruth.DataModel;
using UnityEngine.TestTools;
#if MOQ_PRESENT
using Moq;
#endif
namespace GroundTruthTests
{
#if HDRP_PRESENT
[Ignore("Ignoring in HDRP because of a rendering issue in the first frame. See issue AISV-455.")]
#endif
public class PerceptionCameraIntegrationTests : GroundTruthTestBase
{
[UnityTest]
[UnityPlatform(RuntimePlatform.LinuxPlayer, RuntimePlatform.WindowsPlayer)]
public IEnumerator EnableBoundingBoxes_GeneratesCorrectDataset()
{
//set resolution to ensure we don't have rounding in rendering leading to bounding boxes to change height/width
Screen.SetResolution(400, 400, false);
//give the screen a chance to resize
yield return null;
// var jsonExpected = $@"[
// {{
// ""label_id"": 100,
// ""label_name"": ""label"",
// ""instance_id"": 1,
// ""x"": 0.0,
// ""y"": {Screen.height / 4:F1},
// ""width"": {Screen.width:F1},
// ""height"": {Screen.height / 2:F1}
// }}
// ]";
var labelingConfiguration = CreateLabelingConfiguration();
SetupCamera(pc =>
{
pc.AddLabeler(new BoundingBox2DLabeler(labelingConfiguration));
});
var plane = TestHelper.CreateLabeledPlane();
AddTestObjectForCleanup(plane);
//a plane is 10x10 by default, so scale it down to be 10x1 to cover the center half of the image
plane.transform.localScale = new Vector3(10f, -1f, .1f);
plane.transform.localPosition = new Vector3(0, 0, 10);
var plane2 = TestHelper.CreateLabeledPlane(label: "nonmatching");
AddTestObjectForCleanup(plane2);
//place a smaller plane in front to test non-matching objects
plane2.transform.localScale = new Vector3(.1f, -1f, .1f);
plane2.transform.localPosition = new Vector3(0, 0, 5);
yield return null;
var collector = ScriptableObject.CreateInstance<CollectEndpoint>();
DatasetCapture.Instance.OverrideEndpoint(collector);
DatasetCapture.Instance.ResetSimulation();
var dcWatcher = new DatasetCapture.WaitUntilComplete();
yield return dcWatcher;
// TODO what do I check here
Assert.NotZero(0);
// var capturesPath = Path.Combine(DatasetCapture.OutputDirectory, "captures_000.json");
// var capturesJson = File.ReadAllText(capturesPath);
// StringAssert.Contains(TestHelper.NormalizeJson(jsonExpected, true), TestHelper.NormalizeJson(capturesJson, true));
}
[UnityTest]
public IEnumerator EnableSemanticSegmentation_GeneratesCorrectDataset([Values(true, false)] bool enabled)
{
SimulationState.TimeOutFrameCount = 50;
var collector = ScriptableObject.CreateInstance<CollectEndpoint>();
DatasetCapture.Instance.OverrideEndpoint(collector);
SemanticSegmentationLabeler semanticSegmentationLabeler = null;
SetupCamera(pc =>
{
semanticSegmentationLabeler = new SemanticSegmentationLabeler(CreateSemanticSegmentationLabelConfig());
pc.AddLabeler(semanticSegmentationLabeler);
}, enabled);
AddTestObjectForCleanup(TestHelper.CreateLabeledPlane());
yield return null;
DatasetCapture.Instance.ResetSimulation();
var dcWatcher = new DatasetCapture.WaitUntilComplete();
yield return dcWatcher;
if (enabled)
{
Assert.NotNull(collector.currentRun);
Assert.AreEqual(1, collector.currentRun.frames.Count);
Assert.AreEqual(1, collector.currentRun.frames[0].sensors.Count());
var rgb = collector.currentRun.frames[0].sensors.First() as RgbSensor;
Assert.NotNull(rgb);
Assert.AreEqual(1, rgb.annotations.Count());
var ann = rgb.annotations.First() as SemanticSegmentationLabeler.SemanticSegmentation;
Assert.NotNull(ann);
Assert.NotZero(ann.buffer.Length);
}
else
{
Assert.Null(collector.currentRun.frames);
}
}
[UnityTest]
public IEnumerator Disabled_GeneratesCorrectDataset()
{
var collector = ScriptableObject.CreateInstance<CollectEndpoint>();
DatasetCapture.Instance.OverrideEndpoint(collector);
SemanticSegmentationLabeler semanticSegmentationLabeler = null;
SetupCamera(pc =>
{
semanticSegmentationLabeler = new SemanticSegmentationLabeler(CreateSemanticSegmentationLabelConfig());
pc.AddLabeler(semanticSegmentationLabeler);
});
AddTestObjectForCleanup(TestHelper.CreateLabeledPlane());
yield return null;
DatasetCapture.Instance.ResetSimulation();
var dcWatcher = new DatasetCapture.WaitUntilComplete();
yield return dcWatcher;
Assert.NotNull(collector.currentRun);
Assert.AreEqual(1, collector.currentRun.frames.Count);
Assert.AreEqual(1, collector.currentRun.frames[0].sensors.Count());
var rgb = collector.currentRun.frames[0].sensors.First() as RgbSensor;
Assert.NotNull(rgb);
Assert.AreEqual(1, rgb.annotations.Count());
var ann = rgb.annotations.First() as SemanticSegmentationLabeler.SemanticSegmentation;
Assert.NotNull(ann);
Assert.NotZero(ann.buffer.Length);
}
static IdLabelConfig CreateLabelingConfiguration()
{
var label = "label";
var labelConfig = ScriptableObject.CreateInstance<IdLabelConfig>();
labelConfig.Init(new List<IdLabelEntry>
{
new IdLabelEntry
{
id = 100,
label = label
}
});
return labelConfig;
}
static SemanticSegmentationLabelConfig CreateSemanticSegmentationLabelConfig()
{
var label = "label";
var labelingConfiguration = ScriptableObject.CreateInstance<SemanticSegmentationLabelConfig>();
labelingConfiguration.Init(new List<SemanticSegmentationLabelEntry>
{
new SemanticSegmentationLabelEntry()
{
label = label,
color = Color.blue
}
});
return labelingConfiguration;
}
}
}