浏览代码

Migrating labelers to CameraLabeler

/labeler_mock_mb
Jon Hogins 4 年前
当前提交
20a475b5
共有 8 个文件被更改,包括 156 次插入100 次删除
  1. 21
      com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBoxLabeler.cs
  2. 80
      com.unity.perception/Runtime/GroundTruth/Labelers/InstanceSegmentationLabeler.cs
  3. 39
      com.unity.perception/Runtime/GroundTruth/Labelers/RenderedObjectInfoLabeler.cs
  4. 9
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelingConfiguration.cs
  5. 22
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  6. 1
      com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs
  7. 81
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs
  8. 3
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs.meta

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


using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Unity.Collections;
using Unity.Profiling;

public class BoundingBoxLabeler : CameraLabeler
public class BoundingBox2DLabeler : CameraLabeler
public LabelingConfiguration labelingConfiguration;
PerceptionCamera m_PerceptionCamera;
Dictionary<int, AsyncAnnotation> m_AsyncAnnotations = new Dictionary<int, AsyncAnnotation>();
AnnotationDefinition m_BoundingBoxAnnotationDefinition;
BoundingBoxValue[] m_BoundingBoxValues;

public float height;
}
void Start()
public override void Setup()
m_PerceptionCamera = GetComponent<PerceptionCamera>();
m_RenderedObjectInfoLabeler = GetComponent<RenderedObjectInfoLabeler>();
m_RenderedObjectInfoLabeler = (RenderedObjectInfoLabeler)PerceptionCamera.labelers.First(l => l is RenderedObjectInfoLabeler && ((RenderedObjectInfoLabeler)l).labelingConfiguration == labelingConfiguration);
if (m_RenderedObjectInfoLabeler == null)
{
PerceptionCamera.labelers
}
m_RenderedObjectInfoLabeler.renderedObjectInfosCalculated += OnRenderedObjectInfosCalculated;
m_PerceptionCamera.BeginRendering += ReportAsyncMetrics;
m_RenderedObjectInfoLabeler.renderedObjectInfosCalculated += OnRenderedObjectInfosCalculated;
void ReportAsyncMetrics()
public override void OnBeginRendering()
m_AsyncAnnotations[Time.frameCount] = m_PerceptionCamera.SensorHandle.ReportAnnotationAsync(m_BoundingBoxAnnotationDefinition);
m_AsyncAnnotations[Time.frameCount] = PerceptionCamera.SensorHandle.ReportAnnotationAsync(m_BoundingBoxAnnotationDefinition);
}
void OnRenderedObjectInfosCalculated(int frameCount, NativeArray<RenderedObjectInfo> renderedObjectInfos)

80
com.unity.perception/Runtime/GroundTruth/Labelers/InstanceSegmentationLabeler.cs


#endif
namespace UnityEngine.Perception.GroundTruth {
public class InstanceSegmentationLabeler : CameraLabeler
{
//Uncomment when we support saving instance segmentation labels
//public bool saveImages = false;
//public string annotationId = "E657461D-B950-42E1-8141-BEC9B4810241";
RenderTexture m_SegmentationTexture;
RenderTextureReader<uint> m_SegmentationReader;
#if HDRP_PRESENT
InstanceSegmentationPass m_SegmentationPass;
#endif
#if URP_PRESENT
[NonSerialized]
InstanceSegmentationUrpPass m_InstanceSegmentationUrpPass;
#endif
public RenderTexture InstanceSegmentationRenderTexture => m_SegmentationTexture;
public event Action<int, NativeArray<uint>, RenderTexture> InstanceSegmentationImageReadback;
public void Start()
{
var myCamera = GetComponent<Camera>();
var perceptionCamera = GetComponent<PerceptionCamera>();
var width = myCamera.pixelWidth;
var height = myCamera.pixelHeight;
m_SegmentationTexture = new RenderTexture(new RenderTextureDescriptor(width, height, GraphicsFormat.R8G8B8A8_UNorm, 8));
m_SegmentationTexture.name = "Segmentation";
#if HDRP_PRESENT
var customPassVolume = this.GetComponent<CustomPassVolume>() ?? gameObject.AddComponent<CustomPassVolume>();
customPassVolume.injectionPoint = CustomPassInjectionPoint.BeforeRendering;
customPassVolume.isGlobal = true;
m_SegmentationPass = new InstanceSegmentationPass()
{
name = "Segmentation Pass",
targetCamera = myCamera,
targetTexture = m_SegmentationTexture
};
m_SegmentationPass.EnsureInit();
customPassVolume.customPasses.Add(m_SegmentationPass);
#endif
#if URP_PRESENT
perceptionCamera.AddScriptableRenderPass(new InstanceSegmentationUrpPass(myCamera, m_SegmentationTexture));
#endif
m_SegmentationReader = new RenderTextureReader<uint>(m_SegmentationTexture, myCamera, (frameCount, data, tex) =>
{
InstanceSegmentationImageReadback?.Invoke(frameCount, data, tex);
});
}
public void OnDisable()
{
if (m_SegmentationTexture != null)
m_SegmentationTexture.Release();
m_SegmentationTexture = null;
m_SegmentationReader?.WaitForAllImages();
m_SegmentationReader?.Dispose();
m_SegmentationReader = null;
}
}
// public class InstanceSegmentationLabeler : CameraLabeler
// {
// //Uncomment when we support saving instance segmentation labels
// //public bool saveImages = false;
// //public string annotationId = "E657461D-B950-42E1-8141-BEC9B4810241";
//
// public override void Setup()
// {
// PerceptionCamera.InstanceSegmentationImageReadback += OnInstanceSegmentationImageReadback;
// }
//
// void OnInstanceSegmentationImageReadback(int frameCount, NativeArray<uint> imageData, RenderTexture renderTexture)
// {
// throw new NotImplementedException();
// }
// }
}

39
com.unity.perception/Runtime/GroundTruth/Labelers/RenderedObjectInfoLabeler.cs


static ProfilerMarker s_ClassCountCallback = new ProfilerMarker("OnClassLabelsReceived");
static ProfilerMarker s_ProduceRenderedObjectInfoMetric = new ProfilerMarker("ProduceRenderedObjectInfoMetric");
RenderedObjectInfoGenerator m_RenderedObjectInfoGenerator;
[SuppressMessage("ReSharper", "InconsistentNaming")]
struct ObjectCountSpec
{

Dictionary<int, AsyncMetric> m_ObjectInfoAsyncMetrics = new Dictionary<int, AsyncMetric>();
MetricDefinition m_ObjectCountMetricDefinition;
MetricDefinition m_RenderedObjectInfoMetricDefinition;
PerceptionCamera m_PerceptionCamera;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> classCountsReceived;
/// <summary>
/// Invoked when RenderedObjectInfos are calculated. The first parameter is the Time.frameCount at which the objects were rendered. This may be called many frames after the frame in which the objects were rendered.
/// </summary>
public event Action<int, NativeArray<RenderedObjectInfo>> renderedObjectInfosCalculated;
public string boundingBoxAnnotationId = "F9F22E05-443F-4602-A422-EBE4EA9B55CB";
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> classCountsReceived;
Dictionary<int, AsyncAnnotation> m_AsyncAnnotations = new Dictionary<int, AsyncAnnotation>();
AnnotationDefinition m_BoundingBoxAnnotationDefinition;
BoundingBoxValue[] m_BoundingBoxValues;
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "NotAccessedField.Local")]
struct BoundingBoxValue
{
public int label_id;
public string label_name;
public int instance_id;
public float x;
public float y;
public float width;
public float height;
}
public void Start()
public void Setup()
{
if (labelingConfiguration == null)
{

}
m_PerceptionCamera = GetComponent<PerceptionCamera>();
var instanceSegmentationLabeler = GetComponent<InstanceSegmentationLabeler>();
m_RenderedObjectInfoGenerator = new RenderedObjectInfoGenerator(labelingConfiguration);
World.DefaultGameObjectInjectionWorld.GetExistingSystem<GroundTruthLabelSetupSystem>().Activate(m_RenderedObjectInfoGenerator);
instanceSegmentationLabeler.InstanceSegmentationImageReadback += (frameCount, data, tex) =>
PerceptionCamera.InstanceSegmentationImageReadback += (frameCount, data, tex) =>
m_RenderedObjectInfoGenerator.Compute(data, tex.width, BoundingBoxOrigin.TopLeft, out var renderedObjectInfos, out var classCounts, Allocator.Temp);
using (s_RenderedObjectInfosCalculatedEvent.Auto())
renderedObjectInfosCalculated?.Invoke(frameCount, renderedObjectInfos);

if (produceObjectInfoMetrics)
ProduceRenderedObjectInfoMetric(renderedObjectInfos, frameCount);
};
m_PerceptionCamera.BeginRendering += ReportAsyncMetrics;
void ReportAsyncMetrics()
public override void OnBeginRendering()
{
if (produceObjectCountMetrics)
{

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


[CreateAssetMenu(fileName = "LabelingConfiguration", menuName = "Perception/Labeling Configuration", order = 1)]
public class LabelingConfiguration : ScriptableObject
{
[FormerlySerializedAs("LabelingConfigurations")]
[FormerlySerializedAs("LabelEntries")]
[SerializeField]
List<LabelEntry> m_LabelEntries = new List<LabelEntry>();
/// <summary>
/// Whether the inspector will auto-assign ids based on the id of the first element.
/// </summary>

/// <summary>
/// A sequence of <see cref="LabelEntry"/> which defines the labels relevant for this configuration and their values.
/// </summary>
[FormerlySerializedAs("LabelingConfigurations")]
[SerializeField]
public List<LabelEntry> LabelEntries = new List<LabelEntry>();
public IReadOnlyList<LabelEntry> LabelEntries => m_LabelEntries;
/// <summary>
/// Attempts to find the matching index in <see cref="LabelEntries"/> for the given <see cref="Labeling"/>.

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


/// Captures ground truth from the associated Camera.
/// </summary>
[RequireComponent(typeof(Camera))]
public class PerceptionCamera : MonoBehaviour
public partial class PerceptionCamera : MonoBehaviour
{
//TODO: Remove the Guid path when we have proper dataset merging in USim/Thea
internal static string RgbDirectory { get; } = $"RGB{Guid.NewGuid()}";

var ego = m_EgoMarker == null ? SimulationManager.RegisterEgo("") : m_EgoMarker.EgoHandle;
SensorHandle = SimulationManager.RegisterSensor(ego, "camera", description, period, startTime);
var myCamera = GetComponent<Camera>();
var width = myCamera.pixelWidth;
var height = myCamera.pixelHeight;
SetupInstanceSegmentation();
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
SimulationManager.SimulationEnding += OnSimulationEnding;

if (UnityEditor.EditorApplication.isPaused)
return;
#endif
BeginRendering?.Invoke();
CaptureRgbData(cam);
}

SensorHandle.Dispose();
SensorHandle = default;
CleanUpInstanceSegmentation();
}
[Serializable]

public bool foldout;
protected PerceptionCamera PerceptionCamera { get; private set; }
protected SensorHandle SensorHandle { get; private set; }
public abstract void Setup();
public virtual void OnUpdate() {}
public virtual void OnBeginRendering() {}
internal void Init(PerceptionCamera perceptionCamera)
{
PerceptionCamera = perceptionCamera;
SensorHandle = perceptionCamera.SensorHandle;
}
}
}

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


{
m_InstanceIdToLabelEntryIndexLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
}
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = index;
}
}

81
com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs


using System;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
#if HDRP_PRESENT
using UnityEngine.Rendering.HighDefinition;
#endif
#if URP_PRESENT
using UnityEngine.Rendering.Universal;
#endif
namespace UnityEngine.Perception.GroundTruth
{
partial class PerceptionCamera
{
public event Action<int, NativeArray<uint>, RenderTexture> InstanceSegmentationImageReadback;
/// <summary>
/// Invoked when RenderedObjectInfos are calculated. The first parameter is the Time.frameCount at which the objects were rendered. This may be called many frames after the frame in which the objects were rendered.
/// </summary>
public event Action<int, NativeArray<RenderedObjectInfo>> RenderedObjectInfosCalculated;
RenderedObjectInfoGenerator m_RenderedObjectInfoGenerator;
RenderTexture m_InstanceSegmentationTexture;
RenderTextureReader<uint> m_InstanceSegmentationReader;
#if HDRP_PRESENT
InstanceSegmentationPass m_InstanceSegmentationPass;
#endif
#if URP_PRESENT
[NonSerialized]
InstanceSegmentationUrpPass m_InstanceSegmentationUrpPass;
#endif
void SetupInstanceSegmentation()
{
var camera = PerceptionCamera.GetComponent<Camera>();
var width = camera.pixelWidth;
var height = camera.pixelHeight;
m_InstanceSegmentationTexture = new RenderTexture(new RenderTextureDescriptor(width, height, GraphicsFormat.R8G8B8A8_UNorm, 8));
m_InstanceSegmentationTexture.name = "InstanceSegmentation";
m_RenderedObjectInfoGenerator = new RenderedObjectInfoGenerator();
World.DefaultGameObjectInjectionWorld.GetExistingSystem<GroundTruthLabelSetupSystem>().Activate(m_RenderedObjectInfoGenerator);
#if HDRP_PRESENT
var customPassVolume = this.GetComponent<CustomPassVolume>() ?? gameObject.AddComponent<CustomPassVolume>();
customPassVolume.injectionPoint = CustomPassInjectionPoint.BeforeRendering;
customPassVolume.isGlobal = true;
m_SegmentationPass = new InstanceSegmentationPass()
{
name = "Instance segmentation pass",
targetCamera = myCamera,
targetTexture = m_SegmentationTexture
};
m_SegmentationPass.EnsureInit();
customPassVolume.customPasses.Add(m_SegmentationPass);
#endif
#if URP_PRESENT
AddScriptableRenderPass(new InstanceSegmentationUrpPass(camera, m_InstanceSegmentationTexture));
#endif
m_InstanceSegmentationReader = new RenderTextureReader<uint>(m_InstanceSegmentationTexture, camera, (frameCount, data, tex) =>
{
InstanceSegmentationImageReadback?.Invoke(frameCount, data, tex);
});
}
void CleanUpInstanceSegmentation()
{
if (m_InstanceSegmentationTexture != null)
m_InstanceSegmentationTexture.Release();
m_InstanceSegmentationTexture = null;
m_InstanceSegmentationReader?.WaitForAllImages();
m_InstanceSegmentationReader?.Dispose();
m_InstanceSegmentationReader = null;
}
}
}

3
com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs.meta


fileFormatVersion: 2
guid: ee9eab40cf054cd6ad5da120c84881d7
timeCreated: 1593126137
正在加载...
取消
保存