using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using JetBrains.Annotations; using UnityEngine.Serialization; namespace UnityEngine.Perception.GroundTruth { /// /// A definition for how a should be resolved to a single label and id for ground truth generation. /// [CreateAssetMenu(fileName = "LabelingConfiguration", menuName = "Perception/Labeling Configuration", order = 1)] public class LabelingConfiguration : ScriptableObject { [FormerlySerializedAs("LabelingConfigurations")] [FormerlySerializedAs("LabelEntries")] [SerializeField] List m_LabelEntries = new List(); /// /// Whether the inspector will auto-assign ids based on the id of the first element. /// public bool AutoAssignIds = true; /// /// Whether the inspector will start label ids at zero or one when is enabled. /// public StartingLabelId StartingLabelId = StartingLabelId.One; /// /// A sequence of which defines the labels relevant for this configuration and their values. /// public IReadOnlyList LabelEntries => m_LabelEntries; /// /// Attempts to find the matching index in for the given . /// /// /// The matching index is the first class name in the given Labeling which matches an entry in . /// /// The to match /// When this method returns, contains the matching , or default if no match was found. /// Returns true if a match was found. False if not. public bool TryGetMatchingConfigurationEntry(Labeling labeling, out LabelEntry labelEntry) { return TryGetMatchingConfigurationEntry(labeling, out labelEntry, out int _); } /// /// Attempts to find the matching index in for the given . /// /// /// The matching index is the first class name in the given Labeling which matches an entry in . /// /// The to match /// When this method returns, contains the matching , or default if no match was found. /// When this method returns, contains the index of the matching , or -1 if no match was found. /// Returns true if a match was found. False if not. public bool TryGetMatchingConfigurationEntry(Labeling labeling, out LabelEntry labelEntry, out int labelEntryIndex) { foreach (var labelingClass in labeling.labels) { for (var i = 0; i < LabelEntries.Count; i++) { var entry = LabelEntries[i]; if (string.Equals(entry.label, labelingClass)) { labelEntry = entry; labelEntryIndex = i; return true; } } } labelEntryIndex = -1; labelEntry = default; return false; } [SuppressMessage("ReSharper", "InconsistentNaming")] internal struct LabelEntrySpec { /// /// The label id prepared for reporting in the annotation /// [UsedImplicitly] public int label_id; /// /// The label name prepared for reporting in the annotation /// [UsedImplicitly] public string label_name; } internal LabelEntrySpec[] GetAnnotationSpecification() { return LabelEntries.Select((l) => new LabelEntrySpec() { label_id = l.id, label_name = l.label, }).ToArray(); } } /// /// Structure defining a label configuration for . /// [Serializable] public struct LabelEntry { /// /// The id associated with the label. Used to associate objects with labels in various forms of ground truth. /// public int id; /// /// The label string /// public string label; /// /// The value to use when generating semantic segmentation images. /// public int value; /// /// Creates a new LabelingConfigurationEntry with the given values. /// /// The id associated with the label. Used to associate objects with labels in various forms of ground truth. /// The label string. /// The value to use when generating semantic segmentation images. public LabelEntry(int id, string label, int value) { this.id = id; this.label = label; this.value = value; } } }