浏览代码

almost done now

/main
Mohsen Kamalzadeh 4 年前
当前提交
ba4f24b3
共有 13 个文件被更改,包括 403 次插入307 次删除
  1. 5
      com.unity.perception/Editor/AssetPreparation/AssetPreparationTools.cs
  2. 214
      com.unity.perception/Editor/GroundTruth/AddToConfigWindow.cs
  3. 31
      com.unity.perception/Editor/GroundTruth/IdLabelConfigEditor.cs
  4. 62
      com.unity.perception/Editor/GroundTruth/LabelConfigEditor.cs
  5. 212
      com.unity.perception/Editor/GroundTruth/LabelingEditor.cs
  6. 11
      com.unity.perception/Editor/GroundTruth/SemanticSegmentationLabelConfigEditor.cs
  7. 9
      com.unity.perception/Editor/GroundTruth/Uxml/AddToConfigWindow.uxml
  8. 10
      com.unity.perception/Editor/GroundTruth/Uxml/LabelConfig_Main.uxml
  9. 5
      com.unity.perception/Editor/GroundTruth/Uxml/Labeling_Main.uxml
  10. 22
      com.unity.perception/Runtime/GroundTruth/Labeling/IdLabelConfig.cs
  11. 11
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelConfig.cs
  12. 107
      com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs
  13. 11
      com.unity.perception/Runtime/GroundTruth/Labeling/SemanticSegmentationLabelConfig.cs

5
com.unity.perception/Editor/AssetPreparation/AssetPreparationTools.cs


using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO;
using UnityEngine;
namespace UnityEditor.Perception.AssetPreparation

}
}
}
}

214
com.unity.perception/Editor/GroundTruth/AddToConfigWindow.cs


using System;
using System.Collections;
using Unity.Entities;
using UnityEditorInternal;
using UnityEngine.PlayerLoop;
using UnityEngine.Rendering.UI;
using Object = System.Object;
namespace UnityEditor.Perception.GroundTruth
{

private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;
private static string m_LabelValue;
private static HashSet<string> m_LabelValues = new HashSet<string>();
private static Label m_Status;
public void SetStatus(string status)
{
m_Status.text = status;
m_Status.style.display = DisplayStyle.Flex;
}
private ListView m_PresentConfigsListview;
private ListView m_NonPresentConfigsListview;
private static ListView m_PresentConfigsListview;
private static ListView m_NonPresentConfigsListview;
private static Label m_CurrentlyPresentTitle;
private static Label m_OtherConfigsTitle;
m_LabelValue = labelValue;
m_LabelValues.Clear();
m_LabelValues.Add(labelValue);
ShowWindow(m_LabelValues);
}
public static void ShowWindow(HashSet<string> labelValues)
{
m_LabelValues = new HashSet<string>(labelValues);
window.minSize = new Vector2(350, 385);
window.maxSize = new Vector2(350, 385);
window.titleContent = new GUIContent("Manage Label");
if (labelValues.Count == 1)
{
if(m_TitleLabel != null)
m_TitleLabel.text = "Label: \"" + m_LabelValues.First() + "\"";
if (m_PresentConfigsListview != null)
{
m_PresentConfigsListview.style.display = DisplayStyle.Flex;
}
if (m_CurrentlyPresentTitle != null)
{
m_CurrentlyPresentTitle.style.display = DisplayStyle.Flex;
}
if (m_OtherConfigsTitle != null)
{
m_OtherConfigsTitle.text = "Other Label Configs in Project:";
}
if (m_NonPresentConfigsListview != null)
{
m_NonPresentConfigsListview.style.height = 150;
}
window.titleContent = new GUIContent("Manage Label");
window.minSize = new Vector2(350, 385);
window.maxSize = new Vector2(350, 600);
}
else
{
if(m_TitleLabel != null)
m_TitleLabel.text = "Multiple Labels Selected";
if (m_PresentConfigsListview != null)
{
m_PresentConfigsListview.style.display = DisplayStyle.None;
}
if (m_CurrentlyPresentTitle != null)
{
m_CurrentlyPresentTitle.style.display = DisplayStyle.None;
}
if (m_OtherConfigsTitle != null)
{
m_OtherConfigsTitle.text = "All Label Configurations in Project:";
}
if (m_NonPresentConfigsListview != null)
{
m_NonPresentConfigsListview.style.height = 250;
}
window.titleContent = new GUIContent("Manage Labels");
window.minSize = new Vector2(350, 300);
window.maxSize = new Vector2(350, 600);
}
window.Show();
Show();
if(m_TitleLabel != null)
m_TitleLabel.text = "Label: \"" + m_LabelValue + "\"";
CheckInclusionInConfigs(m_AllLabelConfigGuids, m_LabelConfigTypes, m_LabelValue, this);
CheckInclusionInConfigs(m_AllLabelConfigGuids, m_LabelValues.Count == 1? m_LabelValues.First() : null);
SetupListViews();
}

void SetupListViews()
{
//configs containing label
m_PresentConfigsListview.itemsSource = m_ConfigsContainingLabel;
VisualElement MakeItem1() => new ConfigElementLabelPresent(this, m_LabelValue);
void BindItem1(VisualElement e, int i)
if (m_LabelValues.Count == 1)
if (e is ConfigElementLabelPresent element)
//we are dealing with only one label
m_PresentConfigsListview.itemsSource = m_ConfigsContainingLabel;
VisualElement MakeItem1() => new ConfigElementLabelPresent(this, m_LabelValues.First());
void BindItem1(VisualElement e, int i)
element.m_Label.text = m_ConfigsContainingLabel[i].name;
element.m_LabelConfig = m_ConfigsContainingLabel[i];
if (e is ConfigElementLabelPresent element)
{
element.m_Label.text = m_ConfigsContainingLabel[i].name;
element.m_LabelConfig = m_ConfigsContainingLabel[i];
}
m_PresentConfigsListview.itemHeight = 30;
m_PresentConfigsListview.bindItem = BindItem1;
m_PresentConfigsListview.makeItem = MakeItem1;
m_PresentConfigsListview.selectionType = SelectionType.None;
m_PresentConfigsListview.itemHeight = 30;
m_PresentConfigsListview.bindItem = BindItem1;
m_PresentConfigsListview.makeItem = MakeItem1;
m_PresentConfigsListview.selectionType = SelectionType.None;
VisualElement MakeItem2() => new ConfigElementLabelNotPresent(this, m_LabelValue);
VisualElement MakeItem2() => new ConfigElementLabelNotPresent(this, m_LabelValues);
void BindItem2(VisualElement e, int i)
{

public void RefreshLists()
{
CheckInclusionInConfigs(m_AllLabelConfigGuids, m_LabelConfigTypes, m_LabelValue, this);
CheckInclusionInConfigs(m_AllLabelConfigGuids, m_LabelValues.Count == 1? m_LabelValues.First() : null);
m_PresentConfigsListview.Refresh();
m_NonPresentConfigsListview.Refresh();
}

m_Root = rootVisualElement;
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree(m_Root);
m_TitleLabel = m_Root.Query<Label>("title");
m_TitleLabel.text = "Add \"" + m_LabelValue + "\" to Label Configurations";
m_PresentConfigsListview = m_Root.Query<ListView>("current-configs-listview");
m_NonPresentConfigsListview = m_Root.Query<ListView>("other-configs-listview");
m_TitleLabel = m_Root.Q<Label>("title");
m_CurrentlyPresentTitle = m_Root.Q<Label>("currently-present-label");
m_OtherConfigsTitle = m_Root.Q<Label>("other-configs-label");
m_PresentConfigsListview = m_Root.Q<ListView>("current-configs-listview");
m_NonPresentConfigsListview = m_Root.Q<ListView>("other-configs-listview");
m_Status = m_Root.Q<Label>("status");
m_Status.style.display = DisplayStyle.None;
void CheckInclusionInConfigs(List<string> configGuids, List<Type> configTypes, string label, AddToConfigWindow window)
void CheckInclusionInConfigs(List<string> configGuids, string label = null)
{
m_ConfigsContainingLabel.Clear();
m_ConfigsNotContainingLabel.Clear();

var asset = AssetDatabase.LoadAssetAtPath<ScriptableObject>(AssetDatabase.GUIDToAssetPath(configGuid));
var methodInfo = asset.GetType().GetMethod(IdLabelConfig.DoesLabelMatchAnEntryName);
if (label != null)
{
//means we are dealing with only one label not a set
var methodInfo = asset.GetType().GetMethod(IdLabelConfig.DoesLabelMatchAnEntryName);
if (methodInfo == null)
continue;
if (methodInfo == null)
continue;
object[] parametersArray = new object[1];
parametersArray[0] = label;
object[] parametersArray = new object[1];
parametersArray[0] = label;
var labelExistsInConfig = (bool) methodInfo.Invoke(asset, parametersArray);
var labelExistsInConfig = (bool) methodInfo.Invoke(asset, parametersArray);
if (labelExistsInConfig)
{
m_ConfigsContainingLabel.Add(asset);
if (labelExistsInConfig)
{
m_ConfigsContainingLabel.Add(asset);
}
else
{
m_ConfigsNotContainingLabel.Add(asset);
}
}
else
{

class ConfigElementLabelPresent : VisualElement
{
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;
private Button m_RemoveButton;
private ObjectField m_ConfigObjectField;
public Label m_Label;

{
m_UxmlPath = m_UxmlDir + "ConfigElementLabelPresent.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree(this);
var uxmlPath = m_UxmlDir + "ConfigElementLabelPresent.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath).CloneTree(this);
m_RemoveButton = this.Q<Button>("remove-from-config-button");
m_RemoveButton.text = "Remove Label";
var removeButton = this.Q<Button>("remove-from-config-button");
removeButton.text = "Remove Label";
m_RemoveButton.clicked += () =>
removeButton.clicked += () =>
{
var methodInfo = m_LabelConfig.GetType().GetMethod(IdLabelConfig.RemoveLabelName);
if (methodInfo != null)

class ConfigElementLabelNotPresent : VisualElement
{
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;
private Button m_AddButton;
public ConfigElementLabelNotPresent(AddToConfigWindow window, string targetLabel)
public ConfigElementLabelNotPresent(AddToConfigWindow window, HashSet<string> targetLabels)
m_UxmlPath = m_UxmlDir + "ConfigElementLabelPresent.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree(this);
var uxmlPath = m_UxmlDir + "ConfigElementLabelPresent.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath).CloneTree(this);
m_AddButton = this.Q<Button>("remove-from-config-button");
m_AddButton.text = "Add Label";
var addButton = this.Q<Button>("remove-from-config-button");
addButton.text = targetLabels.Count ==1 ? "Add Label" : "Add All Labels";
m_AddButton.clicked += () =>
addButton.clicked += () =>
parametersArray[0] = targetLabel;
methodInfo.Invoke(m_LabelConfig, parametersArray);
foreach (var targetLabel in targetLabels)
{
parametersArray[0] = targetLabel;
methodInfo.Invoke(m_LabelConfig, parametersArray);
}
}
if (targetLabels.Count > 1)
{
window.SetStatus("All Labels Added to " + m_LabelConfig.name);
}
};
}

31
com.unity.perception/Editor/GroundTruth/IdLabelConfigEditor.cs


using System;
using System.Collections;
#define ENABLED
#if ENABLED
using System;
using System.IO;
using UnityEditor.VersionControl;
using UnityEditorInternal;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;
namespace UnityEditor.Perception.GroundTruth
{

protected override void OnEnableExtended()
{
m_StartingIdEnumField.RegisterValueChangedCallback(evt =>
{
var prop = serializedObject.FindProperty(nameof(IdLabelConfig.startingLabelId));
var id = (int) ((StartingLabelId) evt.newValue);
serializedObject.FindProperty(nameof(IdLabelConfig.startingLabelId)).enumValueIndex = id;
serializedObject.ApplyModifiedProperties();
AutoAssignIds();
});
AutoAssignIdsIfNeeded();
m_MoveDownButton.clicked += MoveSelectedItemDown;
m_MoveUpButton.clicked += MoveSelectedItemUp;

topProperty.stringValue = currentProperty.stringValue;
currentProperty.stringValue = tmpString;
m_LabelListView.SetSelection(selectedIndex - 1);
m_LabelListView.selectedIndex = selectedIndex - 1;
serializedObject.ApplyModifiedProperties();
RefreshAddedLabels();

bottomProperty.stringValue = currentProperty.stringValue;
currentProperty.stringValue = tmpString;
m_LabelListView.SetSelection(selectedIndex + 1);
m_LabelListView.selectedIndex = selectedIndex + 1;
serializedObject.ApplyModifiedProperties();
RefreshAddedLabels();

private void AutoAssignIds()
{
serializedObject.Update();
var serializedProperty = serializedObject.FindProperty(IdLabelConfig.labelEntriesFieldName);
var size = serializedProperty.arraySize;
if (size == 0)

nextId;
nextId++;
}
serializedObject.ApplyModifiedProperties();
RefreshListDataAndPresenation();
EditorUtility.SetDirty(target);
}
private void AutoAssignIdsIfNeeded()

AutoAssignIds();
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target);
}
}
}

}
}
}
#endif

62
com.unity.perception/Editor/GroundTruth/LabelConfigEditor.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEditor.UIElements;
using UnityEditor.VersionControl;
using UnityEditorInternal;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;
using UnityEditor.UIElements;
namespace UnityEditor.Perception.GroundTruth
{

private List<string> m_LabelsNotPresentInConfig = new List<string>();
private VisualElement m_Root;
protected ListView m_LabelListView;
private ListView m_NonPresentLabelsListView;
private Button m_RemoveAllButton;
private Button m_AddAllButton;
private Button m_ImportFromFileButton;
private Button m_ExportToFileButton;
private ListView m_NonPresentLabelsListView;
protected ListView m_LabelListView;
private Button m_ImportFromFileButton;
private Button m_ExportToFileButton;
protected EnumField m_StartingIdEnumField;
public void OnEnable()
{

m_NonPresentLabelsListView = m_Root.Q<ListView>("labels-in-project-listview");
m_SaveButton = m_Root.Q<Button>("save-button");
m_AddNewLabelButton = m_Root.Q<Button>("add-label");
m_RemoveAllButton = m_Root.Q<Button>("remove-all-labels");
m_AddAllButton = m_Root.Q<Button>("add-all-labels-in-project");
m_StartingIdEnumField = m_Root.Q<EnumField>("starting-id-dropdown");
m_SaveButton.SetEnabled(false);
m_SerializedLabelsArray = serializedObject.FindProperty(IdLabelConfig.labelEntriesFieldName);

m_AddNewLabelButton.clicked += () => { AddNewLabel(m_SerializedLabelsArray, m_AddedLabels); };
m_LabelListView.RegisterCallback<ClickEvent>(evt => { UpdateMoveButtonState(); });
m_RemoveAllButton.clicked += () =>
{
m_SerializedLabelsArray.ClearArray();
serializedObject.ApplyModifiedProperties();
RefreshListDataAndPresenation();
};
m_AddAllButton.clicked += () =>
{
foreach (var label in m_LabelsNotPresentInConfig)
{
AppendLabelEntryToSerializedArray(m_SerializedLabelsArray, CreateLabelEntryFromLabelString(m_SerializedLabelsArray, label));
}
serializedObject.ApplyModifiedProperties();
RefreshListDataAndPresenation();
};
m_ImportFromFileButton.clicked += () =>
{

var path = EditorUtility.SaveFilePanel("Export label configuration to file", "", this.name, "json");
if (path.Length != 0)
{
string fileContents = ExportToJson(this);
string fileContents = ExportToJson();
var writer = File.CreateText(path);
writer.Write(fileContents);

private void ScrollToBottomAndSelectLastItem()
{
m_LabelListView.SetSelection(m_LabelListView.itemsSource.Count - 1);
m_LabelListView.selectedIndex = m_LabelListView.itemsSource.Count - 1;
UpdateMoveButtonState();
m_Root.schedule.Execute(() => { m_LabelListView.ScrollToItem(-1); })

protected abstract void AddLabelIdentifierToJson(SerializedProperty labelEntry, JObject jObj);
private string ExportToJson(LabelConfigEditor<T> editor)
private string ExportToJson()
{
JObject result = new JObject();
result.Add("LabelEntryType", typeof(T).Name);

InitExtended();
m_LabelTextField.RegisterValueChangedCallback<string>((cEvent) =>
m_LabelTextField.RegisterValueChangedCallback((cEvent) =>
{
if (m_LabelConfigEditor.AddedLabels.Contains(cEvent.newValue) && m_LabelConfigEditor.AddedLabels.IndexOf(cEvent.newValue) != m_IndexInList)
{

class NonPresentLabelElement<T> : VisualElement where T : ILabelEntry
{
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;
private Button m_AddButton;
m_UxmlPath = m_UxmlDir + "SuggestedLabelElement.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree(this);
var uxmlPath = m_UxmlDir + "SuggestedLabelElement.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath).CloneTree(this);
m_AddButton = this.Q<Button>("add-button");
var addButton = this.Q<Button>("add-button");
m_AddButton.clicked += () => { editor.AddLabel(labelsArray, m_Label.text); };
addButton.clicked += () => { editor.AddLabel(labelsArray, m_Label.text); };
}
}
}

212
com.unity.perception/Editor/GroundTruth/LabelingEditor.cs


#define ENABLED
#if ENABLED
using System.Diagnostics.Contracts;
using Unity.Entities;
using UnityEditorInternal;
using UnityEngine.PlayerLoop;
using UnityEngine.Rendering.UI;
using UnityEngine.UI;
using Object = UnityEngine.Object;
using Toggle = UnityEngine.UIElements.Toggle;
namespace UnityEditor.Perception.GroundTruth

{
private Labeling m_Labeling;
private SerializedProperty m_SerializedLabelsArray;
//private SerializedProperty m_AutoLabelingBoolProperty;
private BindableElement m_OuterElement;
private VisualElement m_ManualLabelingContainer;
private VisualElement m_AutoLabelingContainer;
private ListView m_CurrentLabelsListView;

private PopupField<string> m_LabelingSchemesPopup;
private Button m_AddButton;
private Button m_AddAutoLabelToConfButton;
private Toggle m_AutoLabelingToggle;
private Toggle m_AutoLabelingToggle;
private Labeling m_Labeling;
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;

private List<string> m_CommonLabels = new List<string>(); //labels that are common between all selected Labeling objects (for multi editing)
public List<string> CommonLabels => m_CommonLabels;
public List<string> CommonLabels { get; private set; } = new List<string>();
private List<Type> m_LabelConfigTypes = new List<Type>();
private List<ScriptableObject> m_AllLabelConfigsInProject = new List<ScriptableObject>();
private List<Type> m_LabelConfigTypes;
private readonly List<ScriptableObject> m_AllLabelConfigsInProject = new List<ScriptableObject>();
private List<AssetLabelingScheme> m_LabelingSchemes = new List<AssetLabelingScheme>();
private readonly List<AssetLabelingScheme> m_LabelingSchemes = new List<AssetLabelingScheme>();
private void OnEnable()
{

m_SerializedLabelsArray = mySerializedObject.FindProperty("labels");
//m_AutoLabelingBoolProperty = serializedObject.FindProperty(nameof(Labeling.useAutoLabeling));
m_Root = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree();
m_CurrentLabelsListView = m_Root.Q<ListView>("current-labels-listview");

m_AutoLabelingToggle = m_Root.Q<Toggle>("auto-or-manual-toggle");
m_ManualLabelingContainer = m_Root.Q<VisualElement>("manual-labeling");
m_AutoLabelingContainer = m_Root.Q<VisualElement>("automatic-labeling");
m_AddAutoLabelToConfButton = m_Root.Q<Button>("add-auto-label-to-config");
var dropdownParent = m_Root.Q<VisualElement>("drop-down-parent");
var dropdownParent = m_Root.Q<VisualElement>("drop-down-parent");
m_ItIsPossibleToAddMultipleAutoLabelsToConfig = false;
AssesAutoLabelingStatus();
if (serializedObject.targetObjects.Length > 1)

var suggestedOnNamePanel = m_Root.Q<VisualElement>("suggested-labels-from-name");
suggestedOnNamePanel.RemoveFromHierarchy();
m_AddAutoLabelToConfButton.text = "Add Automatic Labels of All Selected Assets to Config...";
else
{
m_AddAutoLabelToConfButton.text = "Add to Label Config...";
}
m_AddAutoLabelToConfButton.clicked += () =>
{
AddToConfigWindow.ShowWindow(CreateUnionOfAllLabels());
};
string newLabel = FindNewLabelValue(labelsUnion);
var newLabel = FindNewLabelValue(labelsUnion);
foreach (var targetObject in targets)
{
if (targetObject is Labeling labeling)

RefreshManualLabelingData();
};
m_AutoLabelingToggle.RegisterValueChangedCallback<bool>(evt =>
m_AutoLabelingToggle.RegisterValueChangedCallback(evt =>
bool SerializedObjectHasValidLabelingScheme(SerializedObject serObj)
private bool SerializedObjectHasValidLabelingScheme(SerializedObject serObj)
bool IsValidLabelingSchemeName(string schemeName)
private bool IsValidLabelingSchemeName(string schemeName)
void UpdateUiAspects()
private bool m_ItIsPossibleToAddMultipleAutoLabelsToConfig;
private void UpdateUiAspects()
{
m_ManualLabelingContainer.SetEnabled(!m_AutoLabelingToggle.value);
m_AutoLabelingContainer.SetEnabled(m_AutoLabelingToggle.value);

{
m_CurrentAutoLabel.style.display = DisplayStyle.None;
m_AddAutoLabelToConfButton.SetEnabled(false);
m_AddAutoLabelToConfButton.SetEnabled(true);
}
if(m_AutoLabelingToggle.value && serializedObject.targetObjects.Length > 1 && m_ItIsPossibleToAddMultipleAutoLabelsToConfig)
{
m_AddAutoLabelToConfButton.SetEnabled(true);
}

}
void UpdateCurrentAutoLabelValue(SerializedObject serObj)
private void UpdateCurrentAutoLabelValue(SerializedObject serObj)
{
var array = serObj.FindProperty(nameof(Labeling.labels));
if (array.arraySize > 0)

}
void InitializeLabelingSchemes(VisualElement parent)
private void InitializeLabelingSchemes(VisualElement parent)
{
//this function should be called only once during the lifecycle of the editor element
m_LabelingSchemes.Add(new AssetNameLabelingScheme());

m_LabelingSchemesPopup.style.marginLeft = 0;
parent.Add(m_LabelingSchemesPopup);
m_LabelingSchemesPopup.RegisterValueChangedCallback<string>(evt => AssignAutomaticLabelToSelectedAssets());
m_LabelingSchemesPopup.RegisterValueChangedCallback(evt => AssignAutomaticLabelToSelectedAssets());
void AutoLabelToggleChanged()
private void AutoLabelToggleChanged()
{
UpdateUiAspects();

RefreshManualLabelingData();
}
void AssignAutomaticLabelToSelectedAssets()
private void AssignAutomaticLabelToSelectedAssets()
{
//the 0th index of this popup is "<Select Scheme>" and should not do anything
if (m_LabelingSchemesPopup.index == 0)

m_ItIsPossibleToAddMultipleAutoLabelsToConfig = true;
var labelingScheme = m_LabelingSchemes[m_LabelingSchemesPopup.index - 1];

RefreshManualLabelingData();
}
void AssignAutomaticLabelToSerializedObject(SerializedObject serObj, AssetLabelingScheme labelingScheme)
{
var serLabelsArray = serObj.FindProperty(nameof(Labeling.labels));
var label = labelingScheme.GenerateLabel(serObj.targetObject);
serLabelsArray.ClearArray();
serLabelsArray.InsertArrayElementAtIndex(0);
serLabelsArray.GetArrayElementAtIndex(0).stringValue = label;
serObj.ApplyModifiedProperties();
serObj.SetIsDifferentCacheDirty();
}
void AssesAutoLabelingStatus()
private void AssesAutoLabelingStatus()
{
var enabledOrNot = true;
if (serializedObject.targetObjects.Length == 1)

else
{
string unifiedLabelingScheme = null;
bool allAssetsUseSameLabelingScheme = true;
var allAssetsUseSameLabelingScheme = true;
foreach (var targetObj in serializedObject.targetObjects)
{

// if (enabled)
// {
var schemeName = serObj.FindProperty(nameof(Labeling.autoLabelingSchemeType)).stringValue;
if (schemeName == string.Empty)
{

if (allAssetsUseSameLabelingScheme)
{
//all selected assets are using auto labeling, all using the same scheme
//all selected assets have the same scheme recorded in their serialized objects
if (enabledOrNot)
{
//all selected assets have the same scheme recorded in their serialized objects, and they all
//have auto labeling enabled
m_ItIsPossibleToAddMultipleAutoLabelsToConfig = true;
}
//the selected assets are all using auto labeling, but not using the same scheme
//the selected DO NOT have the same scheme recorded in their serialized objects
HashSet<string> CreateUnionOfAllLabels()
private HashSet<string> CreateUnionOfAllLabels()
{
HashSet<String> result = new HashSet<string>();
foreach (var obj in targets)

}
return result;
}
string FindNewLabelValue(HashSet<string> labels)
private string FindNewLabelValue(HashSet<string> labels)
{
string baseLabel = "New Label";
string label = baseLabel;

public override VisualElement CreateInspectorGUI()
{
serializedObject.Update();
var mySerializedObject = new SerializedObject(serializedObject.targetObjects[0]);
m_SerializedLabelsArray = mySerializedObject.FindProperty("labels");
RefreshCommonLabels();
RefreshSuggestedLabelLists();
RefreshLabelConfigsList();

public void RemoveAddedLabelsFromSuggestedLists()
{
m_SuggestedLabelsBasedOnName.RemoveAll(s => m_CommonLabels.Contains(s));
m_SuggestedLabelsBasedOnPath.RemoveAll(s => m_CommonLabels.Contains(s));
m_SuggestedLabelsBasedOnName.RemoveAll(s => CommonLabels.Contains(s));
m_SuggestedLabelsBasedOnPath.RemoveAll(s => CommonLabels.Contains(s));
}
public void RefreshSuggestedLabelLists()

{
serializedObject.SetIsDifferentCacheDirty();
serializedObject.Update();
var mySerializedObject = new SerializedObject(serializedObject.targetObjects[0]);
m_SerializedLabelsArray = mySerializedObject.FindProperty(nameof(Labeling.labels));
RefreshCommonLabels();
RefreshSuggestedLabelLists();
SetupSuggestedLabelsListViews();

void RefreshCommonLabels()
{
m_CommonLabels.Clear();
m_CommonLabels.AddRange(((Labeling)serializedObject.targetObjects[0]).labels);
CommonLabels.Clear();
CommonLabels.AddRange(((Labeling)serializedObject.targetObjects[0]).labels);
m_CommonLabels = m_CommonLabels.Intersect(((Labeling) obj).labels).ToList();
CommonLabels = CommonLabels.Intersect(((Labeling) obj).labels).ToList();
m_CurrentLabelsListView.itemsSource = m_CommonLabels;
var mySerializedObject = new SerializedObject(serializedObject.targetObjects[0]);
m_CurrentLabelsListView.itemsSource = CommonLabels;
new AddedLabelEditor(this, m_CurrentLabelsListView, mySerializedObject, m_SerializedLabelsArray);
new AddedLabelEditor(this, m_CurrentLabelsListView);
void BindItem(VisualElement e, int i)
{

addedLabel.m_LabelTextField.value = m_CommonLabels[i];
addedLabel.m_LabelTextField.value = CommonLabels[i];
}
}

m_CurrentLabelsListView.makeItem = MakeItem;
m_CurrentLabelsListView.itemHeight = itemHeight;
m_CurrentLabelsListView.itemsSource = m_CommonLabels;
m_CurrentLabelsListView.itemsSource = CommonLabels;
m_CurrentLabelsListView.selectionType = SelectionType.None;
}

internal class AddedLabelEditor : VisualElement
{
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;
private Button m_RemoveButton;
private Button m_AddToConfigButton;
public AddedLabelEditor(LabelingEditor editor, ListView listView, SerializedObject serializedLabelingObject, SerializedProperty labelsArrayProperty)
public AddedLabelEditor(LabelingEditor editor, ListView listView)
m_UxmlPath = m_UxmlDir + "AddedLabelElement.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree(this);
var uxmlPath = m_UxmlDir + "AddedLabelElement.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath).CloneTree(this);
m_RemoveButton = this.Q<Button>("remove-button");
m_AddToConfigButton = this.Q<Button>("add-to-config-button");
var removeButton = this.Q<Button>("remove-button");
var addToConfigButton = this.Q<Button>("add-to-config-button");
ScrollView tmp = listView.Q<ScrollView>();
tmp.verticalScroller.slider.RegisterCallback<MouseDownEvent>(evt =>
{
Debug.Log("mouse down");
});
listView.RegisterCallback<FocusInEvent>(evt =>
{
Debug.Log("list focused");
});
m_LabelTextField.RegisterCallback<FocusOutEvent>(evt =>
{
Debug.Log("focus out");
});
m_LabelTextField.RegisterCallback<FocusInEvent>(evt =>
{
Debug.Log("focus in");
});
m_LabelTextField.RegisterValueChangedCallback<string>((cEvent) =>
m_LabelTextField.RegisterValueChangedCallback((cEvent) =>
{
//Do not let the user define a duplicate label
if (editor.CommonLabels.Contains(cEvent.newValue) && editor.CommonLabels.IndexOf(cEvent.newValue) != m_IndexInList)

editor.RefreshManualLabelingData();
});
m_AddToConfigButton.clicked += () =>
addToConfigButton.clicked += () =>
m_RemoveButton.clicked += () =>
removeButton.clicked += () =>
List<string> m_CommonLabels = new List<string>();
List<string> commonLabels = new List<string>();
m_CommonLabels.Clear();
commonLabels.Clear();
if (firstTarget)
if (firstTarget != null)
m_CommonLabels.AddRange(firstTarget.labels);
commonLabels.AddRange(firstTarget.labels);
m_CommonLabels = m_CommonLabels.Intersect(((Labeling) obj).labels).ToList();
commonLabels = commonLabels.Intersect(((Labeling) obj).labels).ToList();
}
foreach (var targetObject in editor.targets)

RemoveLabelFromLabelingSerObj(labeling, m_CommonLabels);
RemoveLabelFromLabelingSerObj(labeling, commonLabels);
}
}
editor.serializedObject.SetIsDifferentCacheDirty();

internal class SuggestedLabelElement : VisualElement
{
private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;
private Button m_AddButton;
m_UxmlPath = m_UxmlDir + "SuggestedLabelElement.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree(this);
var uxmlPath = m_UxmlDir + "SuggestedLabelElement.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath).CloneTree(this);
m_AddButton = this.Q<Button>("add-button");
var addButton = this.Q<Button>("add-button");
m_AddButton.clicked += () =>
addButton.clicked += () =>
{
foreach (var targetObject in editor.serializedObject.targetObjects)
{

}
}
editor.RefreshManualLabelingData();
//editor.RefreshUi();
};
}
}

private string m_UxmlDir = "Packages/com.unity.perception/Editor/GroundTruth/Uxml/";
private string m_UxmlPath;
private Label m_ConfigName;
//private Toggle m_HiddenCollapsedToggle;
m_UxmlPath = m_UxmlDir + "ConfigElementForAddingLabelsFrom.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(m_UxmlPath).CloneTree(this);
var uxmlPath = m_UxmlDir + "ConfigElementForAddingLabelsFrom.uxml";
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(uxmlPath).CloneTree(this);
m_ConfigName = this.Q<Label>("config-name");
m_ConfigName.text = config.name;
var configName = this.Q<Label>("config-name");
configName.text = config.name;
m_CollapseToggle = this.Q<VisualElement>("collapse-toggle");
var propertyInfo = config.GetType().GetProperty(IdLabelConfig.publicLabelEntriesFieldName);

}
}
}
}
#endif

11
com.unity.perception/Editor/GroundTruth/SemanticSegmentationLabelConfigEditor.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEditor.VersionControl;
using UnityEditorInternal;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;
using Random = UnityEngine.Random;
namespace UnityEditor.Perception.GroundTruth

protected override void OnEnableExtended()
{
m_MoveButtons.style.display = DisplayStyle.None;
m_StartingIdEnumField.style.display = DisplayStyle.None;
}
public override void PostRemoveOperations()

9
com.unity.perception/Editor/GroundTruth/Uxml/AddToConfigWindow.uxml


<UXML xmlns="UnityEngine.UIElements">
<BindableElement class="outer-container-in-window" name="outer-container">
<VisualElement class="outer-container-in-window" name="outer-container">
<Label text="Currently Present In:" style="margin: 10px 0 4px 0"/>
<Label name="currently-present-label" text="Currently Present In:" style="margin: 10px 0 4px 0"/>
<Label text="Other Label Configs in Project:" style="margin: 10px 0 4px 0"/>
<Label name="other-configs-label" style="margin: 10px 0 4px 0"/>
</BindableElement>
<Label name="status" style="display:none; margin-top: 7px; -unity-font-style: bold; color:yellow"/>
</VisualElement>
</UXML>

10
com.unity.perception/Editor/GroundTruth/Uxml/LabelConfig_Main.uxml


<UXML xmlns="UnityEngine.UIElements">
<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<VisualElement style="flex-direction: row; flex-grow: 1; margin-bottom: 5px">
<editor:EnumField label="Starting ID" name="starting-id-dropdown" binding-path="startingLabelId" style="flex-grow:1; margin:0"/>
</VisualElement>
<VisualElement name="added-labels" class="inner-container" style="margin-top:0px">
<Label text="Added Labels" name="added-labels-title" class="title-label"/>
<ListView name="labels-listview" class="labeling__label-listview" style="margin-top: 5px;"/>

<Button name="move-down-button" class="move-label-in-config-button move-down"/>
</VisualElement>
<VisualElement style="flex-grow:1"/>
<Button name="remove-all-labels" text="Remove All" class="labeling__add-label-button"
style="color:red"/>
<!--<Toggle name = "auto-assign-toggle" text="Auto Assign Ids" style="margin-top: 5px"/>-->
</VisualElement>
<VisualElement name="other-labels" class="inner-container depth2">

<Button name="add-all-labels-in-project" text="Add All Labels to Config" class="labeling__add-label-button"
style="margin-top: 5px"/>
</VisualElement>
<VisualElement name="other-labels" class="inner-container depth2">

5
com.unity.perception/Editor/GroundTruth/Uxml/Labeling_Main.uxml


<UXML xmlns="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements">
<BindableElement class="outer-container" name="outer-container">
<VisualElement class="outer-container" name="outer-container">
<Style src="../Uss/Styles.uss"/>
<Toggle name = "auto-or-manual-toggle" text="Use Automatic Labeling" style="margin-top: 5px"/>
<VisualElement name="automatic-labeling" class="inner-container">

<Label name ="current-auto-label" style="-unity-font-style: bold; margin-left: 54px;"/>
</VisualElement>
<Button name ="add-auto-label-to-config" text="Add to Label Config..." style="margin-top: 5px; align-self: flex-end;"/>
</VisualElement>
<VisualElement name="manual-labeling" class="inner-container" style="">
<Label text="Added Labels" name="added-labels-title" class="title-label"/>

</VisualElement>
</VisualElement>
</VisualElement>
</BindableElement>
</VisualElement>
</UXML>

22
com.unity.perception/Runtime/GroundTruth/Labeling/IdLabelConfig.cs


return TryGetLabelEntryFromInstanceId(instanceId, out labelEntry, out var _);
}
/// <summary>
/// Add a string to the list of label entries in this label configuration. The id for this entry will be the
/// maximum id present in the configuration plus one.
/// </summary>
/// <param name="labelToAdd"></param>
if (DoesLabelMatchAnEntry(labelToAdd))
return;
int newId = startingLabelId == StartingLabelId.One ? 1 : 0;
if(m_LabelEntries.Count > 0)
newId = m_LabelEntries.Max(entry => entry.id) + 1;
label = labelToAdd
label = labelToAdd,
id = newId
});
if (autoAssignIds)

}
/// <summary>
/// Remove a label entry matching the given string from this label configuration.
/// Label configurations can not have duplicate labels, so the given string can match only one entry.
/// </summary>
/// <param name="label"></param>
public override void RemoveLabel(string label)
{
base.RemoveLabel(label);

11
com.unity.perception/Runtime/GroundTruth/Labeling/LabelConfig.cs


/// </summary>
public const string labelEntriesFieldName = nameof(m_LabelEntries);
/// <summary>
/// List of LabelEntry items added to this label configuration
/// </summary>
[FormerlySerializedAs("LabelEntries")]
[FormerlySerializedAs("LabelingConfigurations")]
[SerializeField]

}
/// <summary>
/// Name of the function that checks whether a string label is included in the config, used for reflection purposes.
/// Name of the function that checks whether a given string matches any of the label entries in this label configuration, used for reflection purposes.
/// <summary>
/// Does the given string match any of the label entries added to this label configuration.
/// </summary>
/// <param name="label"></param>
/// <returns></returns>
public bool DoesLabelMatchAnEntry(string label)
{
return m_LabelEntries.Any(entry => string.Equals(entry.label, label));

107
com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs


using System.Linq;
using Unity.Entities;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
namespace UnityEngine.Perception.GroundTruth

/// </summary>
public class Labeling : MonoBehaviour
{
/// <summary>
/// List of separator characters used for parsing asset names for auto labeling or label suggestion purposes
/// </summary>
/// <summary>
/// List of separator characters used for parsing asset paths for auto labeling or label suggestion purposes
/// </summary>
public static readonly string[] PathSeparators = {"/"};
/// <summary>

/// <summary>
/// Whether this labeling component is currently using an automatic labeling scheme. When this is enabled, the asset can have only one label (the automatic one) and the user cannot add more labels.
/// </summary>
public bool useAutoLabeling = false;
public bool useAutoLabeling;
public string autoLabelingSchemeType = String.Empty;
public string autoLabelingSchemeType = string.Empty;
/// <summary>
/// The unique id of this labeling component instance

{
labels.Clear();
useAutoLabeling = false;
autoLabelingSchemeType = String.Empty;
autoLabelingSchemeType = string.Empty;
#if UNITY_EDITOR
EditorUtility.SetDirty(gameObject);
#endif

.RefreshLabeling(m_Entity);
}
/// <summary>
/// Get the path of the given asset in the project, or get the path of the given Scene GameObject's source prefab if any
/// </summary>
/// <param name="gObj"></param>
/// <returns></returns>
if (assetPath == String.Empty)
if (assetPath == string.Empty)
{
//this indicates that gObj is a scene object and not a prefab directly selected from the Project tab
var prefabObject = PrefabUtility.GetCorrespondingObjectFromSource(gObj);

}
}
/// <summary>
/// A labeling scheme based on which an automatic label can be produced for a given asset. E.g. based on asset name, asset path, etc.
/// </summary>
protected Object m_TargetAsset;
/// <summary>
/// The description of how this scheme generates labels. Used in the dropdown menu in the UI.
/// </summary>
public abstract string Description { get; }
//public abstract string Title { get; protected set; }
public abstract string Description { get; protected set; }
/// <summary>
/// Generate a label for the given asset
/// </summary>
/// <param name="asset"></param>
/// <returns></returns>
public abstract bool IsCompatibleWithAsset(Object asset);
/// <summary>
/// Asset labeling scheme that outputs the given asset's name as its automatic label
/// </summary>
// public override string Title
// {
// get => "Use Asset Name";
// protected set { }
// }
///<inheritdoc/>
public override string Description => "Use asset name";
public override string Description
{
get => "Use asset name";
protected set { }
}
public override bool IsCompatibleWithAsset(Object asset)
{
return true;
}
///<inheritdoc/>
public override string GenerateLabel(Object asset)
{
return asset.name;

/// <summary>
/// Asset labeling scheme that outputs the given asset's file name, including extension, as its automatic label
/// </summary>
// public override string Title
// {
// get => "Use File Name with Extension";
// protected set { }
// }
///<inheritdoc/>
public override string Description => "Use file name with extension";
public override string Description
{
//get => "Uses the full file name of the asset, including the extension.";
get => "Use file name with extension";
protected set { }
}
public override bool IsCompatibleWithAsset(Object asset)
{
string assetPath = Labeling.GetAssetOrPrefabPath(asset);
return assetPath != null;
}
///<inheritdoc/>
public override string GenerateLabel(Object asset)
{
string assetPath = Labeling.GetAssetOrPrefabPath(asset);

}
}
/// <summary>
/// Asset labeling scheme that outputs the given asset's folder name as its automatic label
/// </summary>
// public override string Title
// {
// get => "Use folder name of asset or its ancestors";
// protected set { }
// }
public override string Description
{
get => "Use folder name of asset or its ancestors";
protected set { }
}
///<inheritdoc/>
public override string Description => "Use folder name of asset or its ancestors";
public override bool IsCompatibleWithAsset(Object asset)
{
string assetPath = Labeling.GetAssetOrPrefabPath(asset);
return assetPath != null;
}
///<inheritdoc/>
public override string GenerateLabel(Object asset)
{
string assetPath = Labeling.GetAssetOrPrefabPath(asset);

11
com.unity.perception/Runtime/GroundTruth/Labeling/SemanticSegmentationLabelConfig.cs


[CreateAssetMenu(fileName = "SemanticSegmentationLabelConfig", menuName = "Perception/Semantic Segmentation Label Config", order = 1)]
public class SemanticSegmentationLabelConfig : LabelConfig<SemanticSegmentationLabelEntry>
{
/// <summary>
/// List of standard color based on which this type of label configuration assigns new colors to added labels.
/// </summary>
public static readonly List<Color> s_StandardColors = new List<Color>()
{
Color.blue,

Color.gray
};
/// <summary>
/// Add a string to the list of label entries in this label configuration. The color for this entry will be
/// a unique color not previously present in the config.
/// </summary>
/// <param name="labelToAdd"></param>
if (DoesLabelMatchAnEntry(labelToAdd))
return;
m_LabelEntries.Add(new SemanticSegmentationLabelEntry
{
label = labelToAdd,

正在加载...
取消
保存