浏览代码

Starting to migrate BoundingBoxLabeler

/labeler_mock_on_camera
Jon Hogins 5 年前
当前提交
ab3dae9a
共有 5 个文件被更改,包括 92 次插入51 次删除
  1. 17
      com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBoxLabeler.cs
  2. 27
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelingConfiguration.cs
  3. 91
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  4. 4
      com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs
  5. 4
      com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs

17
com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBoxLabeler.cs


using System;
using System.Linq;
namespace UnityEngine.Perception.GroundTruth {
[Serializable]

public LabelingConfiguration labelingConfiguration;
AnnotationDefinition m_BoundingBoxAnnotationDefinition;
public override void Setup()
{
var labelingMetricSpec = labelingConfiguration.GetAnnotationSpecification();
m_BoundingBoxAnnotationDefinition = SimulationManager.RegisterAnnotationDefinition("bounding box", labelingMetricSpec, "Bounding box for each labeled object visible to the sensor", id: new Guid(annotationId));
}
public override void Update()
{
//BoundingBoxAsyncMetric = SensorHandle.ReportAnnotationAsync(m_BoundingBoxAnnotationDefinition);
}
public override void OnInstanceSegmentationRead(int frameCount)
{
}
}
}

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


using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine.Serialization;
namespace UnityEngine.Perception.GroundTruth

labelEntryIndex = -1;
labelEntry = default;
return false;
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
internal struct LabelEntrySpec
{
/// <summary>
/// The label id prepared for reporting in the annotation
/// </summary>
[UsedImplicitly]
public int label_id;
/// <summary>
/// The label name prepared for reporting in the annotation
/// </summary>
[UsedImplicitly]
public string label_name;
}
internal LabelEntrySpec[] GetAnnotationSpecification()
{
return LabelEntries.Select((l) => new LabelEntrySpec()
{
label_id = l.id,
label_name = l.label,
}).ToArray();
}
}

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


/// <summary>
/// Whether semantic segmentation images should be generated
/// </summary>
public bool produceSegmentationImages = true;
[FormerlySerializedAs("produceSegmentationImages")]
public bool produceSemanticSegmentationImages = true;
/// <summary>
/// Whether object counts should be computed
/// </summary>

/// </summary>
public event Action<int, NativeArray<RenderedObjectInfo>> renderedObjectInfosCalculated;
internal event Action<int, NativeArray<uint>> segmentationImageReceived;
internal event Action<int, NativeArray<uint>> instanceSegmentationImageReceived;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> classCountsReceived;

internal RenderTexture segmentationTexture;
internal RenderTexture instanceSegmentationTexture;
RenderTextureReader<short> m_ClassLabelingTextureReader;
RenderTextureReader<uint> m_SegmentationReader;
RenderTextureReader<short> m_SemanticSegmentationTextureReader;
RenderTextureReader<uint> m_InstanceSegmentationReader;
RenderedObjectInfoGenerator m_RenderedObjectInfoGenerator;
Dictionary<string, object> m_PersistentSensorData = new Dictionary<string, object>();

public int pixel_value;
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
struct ObjectCountSpec
{
[UsedImplicitly]
public int label_id;
[UsedImplicitly]
public string label_name;
}
/// <summary>
/// Add a data object which will be added to the dataset with each capture. Overrides existing sensor data associated with the given key.
/// </summary>

var width = myCamera.pixelWidth;
var height = myCamera.pixelHeight;
if ((produceSegmentationImages || produceObjectCountAnnotations || produceBoundingBoxAnnotations) && LabelingConfiguration == null)
if ((produceSemanticSegmentationImages || produceObjectCountAnnotations || produceBoundingBoxAnnotations) && LabelingConfiguration == null)
produceSegmentationImages = false;
produceSemanticSegmentationImages = false;
segmentationTexture = new RenderTexture(new RenderTextureDescriptor(width, height, GraphicsFormat.R8G8B8A8_UNorm, 8));
segmentationTexture.name = "Segmentation";
instanceSegmentationTexture = new RenderTexture(new RenderTextureDescriptor(width, height, GraphicsFormat.R8G8B8A8_UNorm, 8));
instanceSegmentationTexture.name = "Segmentation";
labelingTexture = new RenderTexture(new RenderTextureDescriptor(width, height, GraphicsFormat.R8G8B8A8_UNorm, 8));
labelingTexture.name = "Labeling";

SetupPasses(customPassVolume);
#endif
#if URP_PRESENT
instanceSegmentationUrpPass = new InstanceSegmentationUrpPass(myCamera, segmentationTexture);
instanceSegmentationUrpPass = new InstanceSegmentationUrpPass(myCamera, instanceSegmentationTexture);
if (produceSegmentationImages)
if (produceSemanticSegmentationImages)
{
var specs = LabelingConfiguration.LabelEntries.Select((l) => new SemanticSegmentationSpec()
{

m_SegmentationAnnotationDefinition = SimulationManager.RegisterAnnotationDefinition("semantic segmentation", specs, "pixel-wise semantic segmentation label", "PNG");
m_ClassLabelingTextureReader = new RenderTextureReader<short>(labelingTexture, myCamera,
m_SemanticSegmentationTextureReader = new RenderTextureReader<short>(labelingTexture, myCamera,
var labelingMetricSpec = LabelingConfiguration.LabelEntries.Select((l) => new ObjectCountSpec()
{
label_id = l.id,
label_name = l.label,
}).ToArray();
var labelingMetricSpec = LabelingConfiguration.GetAnnotationSpecification();
if (produceObjectCountAnnotations)
{

if (produceBoundingBoxAnnotations)
{
m_BoundingBoxAnnotationDefinition = SimulationManager.RegisterAnnotationDefinition("bounding box", labelingMetricSpec, "Bounding box for each labeled object visible to the sensor", id: new Guid(boundingBoxId));
}
if (produceRenderedObjectInfoMetric)
m_RenderedObjectInfoMetricDefinition = SimulationManager.RegisterMetricDefinition("rendered object info", labelingMetricSpec, "Information about each labeled object visible to the sensor", id: new Guid(renderedObjectInfoId));

m_SegmentationReader = new RenderTextureReader<uint>(segmentationTexture, myCamera, (frameCount, data, tex) =>
m_InstanceSegmentationReader = new RenderTextureReader<uint>(instanceSegmentationTexture, myCamera, (frameCount, data, tex) =>
if (segmentationImageReceived != null)
segmentationImageReceived(frameCount, data);
instanceSegmentationImageReceived?.Invoke(frameCount, data);
m_RenderedObjectInfoGenerator.Compute(data, tex.width, boundingBoxOrigin, out var renderedObjectInfos, out var classCounts, Allocator.Temp);

RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
SimulationManager.SimulationEnding += OnSimulationEnding;
}
// ReSharper disable InconsistentNaming
struct RenderedObjectInfoValue

void ReportAsyncAnnotations()
{
if (produceSegmentationImages || produceObjectCountAnnotations || produceBoundingBoxAnnotations || produceRenderedObjectInfoMetric)
if (produceSemanticSegmentationImages || produceObjectCountAnnotations || produceBoundingBoxAnnotations || produceRenderedObjectInfoMetric)
if (produceSegmentationImages)
if (produceSemanticSegmentationImages)
captureInfo.SegmentationAsyncAnnotation = SensorHandle.ReportAnnotationAsync(m_SegmentationAnnotationDefinition);
if (produceObjectCountAnnotations)

void OnSimulationEnding()
{
m_ClassLabelingTextureReader?.WaitForAllImages();
m_ClassLabelingTextureReader?.Dispose();
m_ClassLabelingTextureReader = null;
m_SemanticSegmentationTextureReader?.WaitForAllImages();
m_SemanticSegmentationTextureReader?.Dispose();
m_SemanticSegmentationTextureReader = null;
m_SegmentationReader?.WaitForAllImages();
m_SegmentationReader?.Dispose();
m_SegmentationReader = null;
m_InstanceSegmentationReader?.WaitForAllImages();
m_InstanceSegmentationReader?.Dispose();
m_InstanceSegmentationReader = null;
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
}

OnSimulationEnding();
m_ClassLabelingTextureReader?.Dispose();
m_ClassLabelingTextureReader = null;
if (segmentationTexture != null)
segmentationTexture.Release();
m_SemanticSegmentationTextureReader?.Dispose();
m_SemanticSegmentationTextureReader = null;
if (instanceSegmentationTexture != null)
instanceSegmentationTexture.Release();
segmentationTexture = null;
instanceSegmentationTexture = null;
if (labelingTexture != null)
labelingTexture.Release();

{
public bool enabled;
public bool foldout;
protected PerceptionCamera PerceptionCamera { get; private set; }
protected SensorHandle SensorHandle { get; private set; }
public abstract void Setup();
public virtual void Update() { }
public virtual void OnInstanceSegmentationRead(int frameCount) { }
internal void Init(PerceptionCamera perceptionCamera)
{
PerceptionCamera = perceptionCamera;
SensorHandle = perceptionCamera.SensorHandle;
}
}
}

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


public IEnumerator EnableSemanticSegmentation_GeneratesCorrectDataset()
{
var labelingConfiguration = CreateLabelingConfiguration();
SetupCamera(labelingConfiguration, pc => pc.produceSegmentationImages = true);
SetupCamera(labelingConfiguration, pc => pc.produceSemanticSegmentationImages = true);
string expectedImageFilename = $"segmentation_{Time.frameCount}.png";

camera.orthographicSize = 1;
var perceptionCamera = cameraObject.AddComponent<PerceptionCamera>();
perceptionCamera.produceSegmentationImages = false;
perceptionCamera.produceSemanticSegmentationImages = false;
perceptionCamera.produceRenderedObjectInfoMetric = false;
perceptionCamera.produceBoundingBoxAnnotations = false;
perceptionCamera.produceObjectCountAnnotations = false;

4
com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs


var skinnedMeshRenderer = planeObject.AddComponent<SkinnedMeshRenderer>();
skinnedMeshRenderer.sharedMesh = meshFilter.sharedMesh;
skinnedMeshRenderer.material = meshRenderer.material;
Object.DestroyImmediate(oldObject);
}
planeObject.transform.SetPositionAndRotation(new Vector3(0, 0, 10), Quaternion.Euler(90, 0, 0));

perceptionCamera.captureRgbImages = false;
perceptionCamera.produceBoundingBoxAnnotations = false;
perceptionCamera.produceObjectCountAnnotations = true;
perceptionCamera.segmentationImageReceived += onSegmentationImageReceived;
perceptionCamera.instanceSegmentationImageReceived += onSegmentationImageReceived;
#endif
AddTestObjectForCleanup(cameraObject);
cameraObject.SetActive(true);

正在加载...
取消
保存