using System; using System.Collections.Generic; using JetBrains.Annotations; using Unity.Collections; using Unity.Profiling; using UnityEngine.Serialization; using UnityEngine.UI; namespace UnityEngine.Perception.GroundTruth { /// /// Labeler which produces label id, instance id, and visible pixel count in a single metric each frame for /// each object which takes up one or more pixels in the camera's frame. /// [Serializable] public sealed class RenderedObjectInfoLabeler : CameraLabeler { // ReSharper disable InconsistentNaming struct RenderedObjectInfoValue { [UsedImplicitly] public int label_id; [UsedImplicitly] public uint instance_id; [UsedImplicitly] public int visible_pixels; } // ReSharper restore InconsistentNaming static ProfilerMarker s_ProduceRenderedObjectInfoMetric = new ProfilerMarker("ProduceRenderedObjectInfoMetric"); /// /// The ID to use for visible pixels metrics in the resulting dataset /// public string objectInfoMetricId = "5ba92024-b3b7-41a7-9d3f-c03a6a8ddd01"; /// /// The which associates objects with labels. /// [FormerlySerializedAs("labelingConfiguration")] public IdLabelConfig idLabelConfig; RenderedObjectInfoValue[] m_VisiblePixelsValues; Dictionary m_ObjectInfoAsyncMetrics; MetricDefinition m_RenderedObjectInfoMetricDefinition; List vizEntries = null; /// /// Creates a new RenderedObjectInfoLabeler. Be sure to assign before adding to a . /// public RenderedObjectInfoLabeler() { } /// /// Creates a new RenderedObjectInfoLabeler with an . /// /// The which associates objects with labels. public RenderedObjectInfoLabeler(IdLabelConfig idLabelConfig) { this.idLabelConfig = idLabelConfig; } /// protected override bool supportsVisualization => true; /// protected override void Setup() { if (idLabelConfig == null) throw new InvalidOperationException("RenderedObjectInfoLabeler's idLabelConfig field must be assigned"); m_ObjectInfoAsyncMetrics = new Dictionary(); perceptionCamera.RenderedObjectInfosCalculated += (frameCount, objectInfo) => { ProduceRenderedObjectInfoMetric(objectInfo, frameCount); }; visualizationEnabled = supportsVisualization; } /// protected override void OnBeginRendering() { if (m_RenderedObjectInfoMetricDefinition.Equals(default)) { m_RenderedObjectInfoMetricDefinition = DatasetCapture.RegisterMetricDefinition( "rendered object info", idLabelConfig.GetAnnotationSpecification(), "Information about each labeled object visible to the sensor", id: new Guid(objectInfoMetricId)); } m_ObjectInfoAsyncMetrics[Time.frameCount] = perceptionCamera.SensorHandle.ReportMetricAsync(m_RenderedObjectInfoMetricDefinition); } void ProduceRenderedObjectInfoMetric(NativeArray renderedObjectInfos, int frameCount) { using (s_ProduceRenderedObjectInfoMetric.Auto()) { if (!m_ObjectInfoAsyncMetrics.TryGetValue(frameCount, out var metric)) return; m_ObjectInfoAsyncMetrics.Remove(frameCount); if (m_VisiblePixelsValues == null || m_VisiblePixelsValues.Length != renderedObjectInfos.Length) m_VisiblePixelsValues = new RenderedObjectInfoValue[renderedObjectInfos.Length]; bool visualize = visualizationEnabled; if (visualize && vizEntries == null) { vizEntries = new List(); } for (var i = 0; i < renderedObjectInfos.Length; i++) { var objectInfo = renderedObjectInfos[i]; if (!TryGetLabelEntryFromInstanceId(objectInfo.instanceId, out var labelEntry)) continue; m_VisiblePixelsValues[i] = new RenderedObjectInfoValue { label_id = labelEntry.id, instance_id = objectInfo.instanceId, visible_pixels = objectInfo.pixelCount }; if (visualize) { var label = labelEntry.label + "_" + objectInfo.instanceId; hudPanel.UpdateEntry(label, objectInfo.pixelCount.ToString()); vizEntries.Add(label); } } metric.ReportValues(m_VisiblePixelsValues); } } bool TryGetLabelEntryFromInstanceId(uint instanceId, out IdLabelEntry labelEntry) { return idLabelConfig.TryGetLabelEntryFromInstanceId(instanceId, out labelEntry); } /// protected override void PopulateVisualizationPanel(ControlPanel panel) { panel.AddToggleControl("Pixel Counts", enabled => { visualizationEnabled = enabled; }); } /// override protected void OnVisualizerEnabledChanged(bool enabled) { if (!enabled) { hudPanel.RemoveEntries(vizEntries); vizEntries.Clear(); } } } }