浏览代码

Updates to COCO export to support running on USim

/coco_export
Steve Borkman 3 年前
当前提交
7910da17
共有 19 个文件被更改,包括 432 次插入35 次删除
  1. 33
      com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
  2. 5
      com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml
  3. 7
      com.unity.perception/Runtime/GroundTruth/DatasetCapture.cs
  4. 10
      com.unity.perception/Runtime/GroundTruth/Exporters/Coco/AnnotationHandler.cs
  5. 39
      com.unity.perception/Runtime/GroundTruth/Exporters/Coco/CocoExporter.cs
  6. 2
      com.unity.perception/Runtime/GroundTruth/Exporters/Coco/CocoTypes.cs
  7. 2
      com.unity.perception/Runtime/GroundTruth/Exporters/IDatasetExporter.cs
  8. 19
      com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionFormat/PerceptionExporter.cs
  9. 6
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  10. 58
      com.unity.perception/Runtime/GroundTruth/SimulationState.cs
  11. 3
      com.unity.perception/Runtime/GroundTruth/SimulationState_Json.cs
  12. 6
      com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs
  13. 2
      com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenarioConstants.cs
  14. 8
      com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew.meta
  15. 94
      com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/AnnotationHandler.cs
  16. 11
      com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/AnnotationHandler.cs.meta
  17. 151
      com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/PerceptionNewExporter.cs
  18. 11
      com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/PerceptionNewExporter.cs.meta

33
com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.GroundTruth.Exporters;
using UnityEngine.Perception.GroundTruth.Exporters.PerceptionFormat;
using UnityEngine.Perception.Randomization.Samplers;
using UnityEngine.Perception.Randomization.Scenarios;
using UnityEngine.SceneManagement;

Label m_PrevExecutionIdLabel;
Label m_PrevRandomSeedLabel;
RunParameters m_RunParameters;
string[] m_OutputFormats;
ToolbarMenu m_OutputFormatMenu;
int m_OutputFormatIndex;
[MenuItem("Window/Run in Unity Simulation")]
static void ShowWindow()

m_RandomSeedField.value = BitConverter.ToUInt32(bytes, 0);
};
m_OutputFormats = TypeCache.GetTypesDerivedFrom<IDatasetExporter>().Select(exporter => exporter.Name).ToArray();
m_OutputFormatMenu = root.Q<ToolbarMenu>("output-format");
var i = 0;
foreach (var format in m_OutputFormats)
{
var index = i++;
m_OutputFormatMenu.menu.AppendAction(
format,
action =>
{
m_OutputFormatIndex = index;
m_OutputFormatMenu.text = format;
});
}
for (var i = 0; i < m_SysParamDefinitions.Length; i++)
for (i = 0; i < m_SysParamDefinitions.Length; i++)
{
var index = i;
var param = m_SysParamDefinitions[i];

m_ProjectIdLabel.text = $"Project ID: {CloudProjectSettings.projectId}";
m_PrevExecutionIdLabel.text = $"Execution ID: {PlayerPrefs.GetString("SimWindow/prevExecutionId")}";
m_PrevRandomSeedLabel.text = $"Random Seed: {PlayerPrefs.GetString("SimWindow/prevRandomSeed")}";
m_OutputFormatMenu.text = PlayerPrefs.GetString(SimulationState.outputFormatMode, nameof(PerceptionExporter));
}
static string IncrementRunName(string runName)

sysParamIndex = m_SysParamIndex,
scenarioConfig = (TextAsset)m_ScenarioConfigField.value,
currentOpenScenePath = SceneManager.GetSceneAt(0).path,
currentScenario = FindObjectOfType<ScenarioBase>()
currentScenario = FindObjectOfType<ScenarioBase>(),
outputFormat = m_OutputFormats[m_OutputFormatIndex]
};
var runGuid = Guid.NewGuid();
PerceptionEditorAnalytics.ReportRunInUnitySimulationStarted(

constants["totalIterations"] = m_RunParameters.totalIterations;
constants["instanceCount"] = m_RunParameters.instanceCount;
constants["randomSeed"] = m_RunParameters.randomSeed;
constants["outputFormat"] = m_RunParameters.outputFormat;
var appParamName = $"{m_RunParameters.runName}";
var appParamsString = JsonConvert.SerializeObject(configuration, Formatting.Indented);

PlayerPrefs.SetInt("SimWindow/sysParamIndex", m_RunParameters.sysParamIndex);
PlayerPrefs.SetString("SimWindow/scenarioConfig",
m_RunParameters.scenarioConfig != null ? m_RunParameters.scenarioConfigAssetPath : string.Empty);
PlayerPrefs.SetString(SimulationState.outputFormatMode, m_RunParameters.outputFormat);
SetFieldsFromPlayerPreferences();
}

public TextAsset scenarioConfig;
public string currentOpenScenePath;
public ScenarioBase currentScenario;
public string outputFormat;
public string scenarioConfigAssetPath => AssetDatabase.GetAssetPath(scenarioConfig);
}

5
com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml


<editor:ToolbarMenu name="sys-param" class="unity-base-field__input" style="border-width: 1px;"/>
</VisualElement>
<VisualElement class="unity-base-field" tooltip="TODO">
<Label text="Output Format" class="unity-base-field__label"/>
<editor:ToolbarMenu name="output-format" class="unity-base-field__input" style="border-width: 1px;"/>
</VisualElement>
<Label text="Optional Configuration" class="sim-window__header-1" style="margin-top: 18px;"/>
<editor:ObjectField name="scenario-config" label="Scenario JSON Config" allow-scene-object="false"
tooltip="Selects a scenario JSON configuration to load during the run.

7
com.unity.perception/Runtime/GroundTruth/DatasetCapture.cs


return ego;
}
/// <summary>
/// Register a new sensor under the given ego.
/// </summary>

throw new ArgumentException("The given annotationDefinition is invalid", nameof(annotationDefinition));
return DatasetCapture.SimulationState.ReportAnnotationAsync(annotationDefinition, this);
}
public string GetRgbCaptureFilename(string defaultFilename, params(string, object)[] additionalSensorValues)
{
return DatasetCapture.SimulationState.GetRgbCaptureFilename(defaultFilename, additionalSensorValues);
}
/// <summary>

10
com.unity.perception/Runtime/GroundTruth/Exporters/Coco/AnnotationHandler.cs


public static CocoTypes.KeypointCategory ToKeypointCategory(KeypointLabeler.KeypointJson keypointJson)
{
var keypoints = new string[keypointJson.key_points.Length];
var skeleton = new int[keypointJson.skeleton.Length * 2];
var skeleton = new int[keypointJson.skeleton.Length][];
foreach (var kp in keypointJson.key_points)
{

var i = 0;
foreach (var bone in keypointJson.skeleton)
{
skeleton[i++] = bone.joint1;
skeleton[i++] = bone.joint2;
var joints = new int[]
{
bone.joint1,
bone.joint2
};
skeleton[i++] = joints;
}
return new CocoTypes.KeypointCategory

39
com.unity.perception/Runtime/GroundTruth/Exporters/Coco/CocoExporter.cs


using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Unity.Simulation;
namespace UnityEngine.Perception.GroundTruth.Exporters.Coco
{

Guid m_SessionGuid;
public string GetRgbCaptureFilename(params(string, object)[] additionalSensorValues)
{
return string.Empty;
}
Debug.Log($"SS - COCO - OnSimBegin");
m_DirectoryName = directoryName;
m_DataCaptured = false;
}

Debug.Log("SS - coco - writing");
WriteOutCategories();
if (m_ObjectDetectionWritingTask != null)

public async void OnSimulationEnd()
{
Debug.Log($"SS - COCO - OnSimEnd");
if (!m_DataCaptured) return;
await AwaitAllWrites();

m_SessionGuid = Guid.NewGuid();
var prefix = m_DirectoryName + Path.DirectorySeparatorChar + m_SessionGuid;
//var prefix = m_DirectoryName + Path.DirectorySeparatorChar + m_SessionGuid;
m_RgbCaptureFilename = prefix + "_coco_captures.json";
m_RgbCaptureFilename = Path.Combine(m_DirectoryName, m_SessionGuid + "_coco_captures.json");
m_ObjectDetectionFilename = prefix + "_coco_box_annotations.json";
m_ObjectDetectionFilename = Path.Combine(m_DirectoryName, m_SessionGuid + "_coco_box_annotations.json");
m_KeypointFilename = prefix + "_coco_keypoint_annotations.json";
m_KeypointFilename = Path.Combine(m_DirectoryName, m_SessionGuid + "_coco_keypoint_annotations.json");
m_ObjectDetectionCategoryFilename = prefix + "_coco_obj_detection_categories.json";
m_KeypointCategoryFilename = prefix + "_coco_keypoint_categories.json";
m_ObjectDetectionCategoryFilename = Path.Combine(m_DirectoryName, m_SessionGuid + "_coco_obj_detection_categories.json");
m_KeypointCategoryFilename = Path.Combine(m_DirectoryName, m_SessionGuid + "_coco_keypoint_categories.json");
m_Initialized = true;
}

json = JToken.Parse(json).ToString(Formatting.Indented);
}
Debug.Log($"SS - COCO - writing to path: {m_DirectoryName}, file: coco_object_detection_annotations.json");
var filename = m_DirectoryName + Path.DirectorySeparatorChar + "coco_object_detection_annotations.json";
var filename = Path.Combine(m_DirectoryName, "coco_object_detection_annotations.json");
Debug.Log($"SS - COCO - file: {filename}");
Manager.Instance.ConsumerFileProduced(filename);
File.Delete(m_ObjectDetectionFilename);
File.Delete(m_ObjectDetectionCategoryFilename);

}
// Write out the files
var filename = m_DirectoryName + Path.DirectorySeparatorChar + "coco_keypoint_annotations.json";
var filename = Path.Combine(m_DirectoryName, "coco_keypoint_annotations.json");
Manager.Instance.ConsumerFileProduced(filename);
File.Delete(m_KeypointFilename);
File.Delete(m_KeypointCategoryFilename);

static string descriptionEntry = "Description of dataset";
static string contributorEntry = "Anonymous";
static string urlEntry = "Not Set";
static void CreateHeaderInfo(StringBuilder stringBuilder)
{

2
com.unity.perception/Runtime/GroundTruth/Exporters/Coco/CocoTypes.cs


public class KeypointCategory : ObjectDetectionCategory
{
public string[] keypoints;
public int[] skeleton;
public int[][] skeleton;
}
[Serializable]

2
com.unity.perception/Runtime/GroundTruth/Exporters/IDatasetExporter.cs


{
public interface IDatasetExporter
{
public string GetRgbCaptureFilename(params(string, object)[] additionalSensorValues);
public void OnSimulationBegin(string directoryName);
public void OnSimulationEnd();

19
com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionFormat/PerceptionExporter.cs


string outputDirectory = string.Empty;
int captureFileIndex = 0;
public string GetRgbCaptureFilename(params(string, object)[] additionalSensorValues)
{
return string.Empty;
}
Debug.Log($"SS - Perception - OnSimBegin");
Debug.Log($"SS - Perception - OnSimEnd");
// do nothing :-)
}

}
void WriteJObjectToFile(JObject jObject, string filename)
public static void WriteJObjectToFile(JObject jObject, string directory, string filename)
{
var stringWriter = new StringWriter(new StringBuilder(256), CultureInfo.InvariantCulture);
using (var jsonTextWriter = new JsonTextWriter(stringWriter))

var contents = stringWriter.ToString();
var path = Path.Combine(outputDirectory, filename);
var path = Path.Combine(directory, filename);
File.WriteAllText(path, contents);
// TODO what to do about this...

capturesJObject.Add("version", DatasetCapture.SchemaVersion);
capturesJObject.Add("captures", capturesJArray);
WriteJObjectToFile(capturesJObject, $"captures_{captureFileIndex:000}.json");
Debug.Log("SS - perception - writing");
WriteJObjectToFile(capturesJObject, outputDirectory, $"captures_{captureFileIndex:000}.json");
// TODO what to do about this...
return null;

return null;
}
static JToken JObjectFromPendingCapture(SimulationState.PendingCapture pendingCapture)
public static JToken JObjectFromPendingCapture(SimulationState.PendingCapture pendingCapture)
{
var sensorJObject = new JObject();//new SensorCaptureJson
sensorJObject["sensor_id"] = pendingCapture.SensorHandle.Id.ToString();

return capture;
}
static JObject JObjectFromAnnotation((Annotation, SimulationState.AnnotationData) annotationInfo)
public static JObject JObjectFromAnnotation((Annotation, SimulationState.AnnotationData) annotationInfo)
{
var annotationJObject = new JObject();
annotationJObject["id"] = annotationInfo.Item1.Id.ToString();

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


void CaptureRgbData(Camera cam)
{
// TODO - Steve - this could be a place where we override the capture of the RGB image to allow the
// active reporter to determine where we should save RGB images, or even write them out
if (!captureRgbImages)
return;

SetPersistentSensorData("frame", frameCount);
var dxRootPath = $"{rgbDirectory}/{k_RgbFilePrefix}{frameCount}.png";
captureFilename = SensorHandle.GetRgbCaptureFilename(captureFilename, m_PersistentSensorData.Select(kvp => (kvp.Key, kvp.Value)).ToArray());
SensorHandle.ReportCapture(dxRootPath, SensorSpatialData.FromGameObjects(
m_EgoMarker == null ? null : m_EgoMarker.gameObject, gameObject),
m_PersistentSensorData.Select(kvp => (kvp.Key, kvp.Value)).ToArray());

58
com.unity.perception/Runtime/GroundTruth/SimulationState.cs


using UnityEngine.Perception.GroundTruth.Exporters;
using UnityEngine.Perception.GroundTruth.Exporters.Coco;
using UnityEngine.Perception.GroundTruth.Exporters.PerceptionFormat;
using UnityEngine.Perception.GroundTruth.Exporters.PerceptionNew;
using UnityEngine.Profiling;
namespace UnityEngine.Perception.GroundTruth

HashSet<Guid> m_Ids = new HashSet<Guid>();
Guid m_SequenceId = Guid.NewGuid();
IDatasetExporter m_ActiveReporter = new PerceptionExporter();
IDatasetExporter _ActiveReporter = null;
// Always use the property SequenceTimeMs instead
int m_FrameCountLastUpdatedSequenceTime;

public SimulationState(string outputDirectory)
{
var mode = PlayerPrefs.GetString(outputFormatMode, nameof(PerceptionExporter));
m_ActiveReporter = mode switch
{
nameof(PerceptionExporter) => new PerceptionExporter(),
nameof(CocoExporter) => new CocoExporter(),
_ => m_ActiveReporter
};
_ActiveReporter = null;
PlayerPrefs.SetString(defaultOutputBaseDirectory, Configuration.Instance.GetStorageBasePath());
m_OutputDirectoryName = outputDirectory;

PlayerPrefs.SetString(latestOutputDirectoryKey, Manager.Instance.GetDirectoryFor("", basePath));
IsRunning = true;
m_ActiveReporter?.OnSimulationBegin(Manager.Instance.GetDirectoryFor(m_OutputDirectoryName));
}
IDatasetExporter GetActiveReporter()
{
if (_ActiveReporter != null) return _ActiveReporter;
var mode = PlayerPrefs.GetString(outputFormatMode, nameof(CocoExporter));
#if false
// TODO figure out how to do this with just the class name and not have to have the switch
var exporter = Activator.CreateInstance(Type.GetType(mode) ?? typeof(PerceptionExporter));
if (exporter is IDatasetExporter casted)
{
m_ActiveReporter = casted;
}
#else
Debug.Log($"SS - Sim State setting active reporter: {mode}");
_ActiveReporter = mode switch
{
nameof(PerceptionExporter) => new PerceptionExporter(),
nameof(CocoExporter) => new CocoExporter(),
nameof(PerceptionNewExporter) => new PerceptionNewExporter(),
_ => new PerceptionExporter()
};
#endif
Debug.Log("Calling SS::OnSimulationBegin");
_ActiveReporter?.OnSimulationBegin(Manager.Instance.GetDirectoryFor(m_OutputDirectoryName));
return _ActiveReporter;
}
public string GetRgbCaptureFilename(string defaultFilename, params(string, object)[] additionalSensorValues)
{
var directory = GetActiveReporter()?.GetRgbCaptureFilename(additionalSensorValues);
return directory == string.Empty ? defaultFilename : directory;
}
/// <summary>

}
}
m_ActiveReporter.OnCaptureReported(frameCount, width, height, filename);
GetActiveReporter()?.OnCaptureReported(frameCount, width, height, filename);
}
static string GetFormatFromFilename(string filename)

Time.captureDeltaTime = 0;
IsRunning = false;
m_ActiveReporter?.OnSimulationEnd();
Debug.Log($"Calling SS::OnSimulationEnd");
GetActiveReporter()?.OnSimulationEnd();
public AnnotationDefinition RegisterAnnotationDefinition<TSpec>(string name, TSpec[] specValues, string description, string format, Guid id)
{

RegisterAdditionalInfoType(name, specValues, description, format, id, AdditionalInfoKind.Annotation);
m_ActiveReporter.OnAnnotationRegistered(id, specValues); // <- Not sure about this one either
GetActiveReporter()?.OnAnnotationRegistered(id, specValues); // <- Not sure about this one either
return new AnnotationDefinition(id);
}

3
com.unity.perception/Runtime/GroundTruth/SimulationState_Json.cs


m_WriteToDiskSampler.Begin();
var path = Path.Combine(OutputDirectory, filename);
Debug.Log($"ss - sensors.json - {path}");
File.WriteAllText(path, contents);
Manager.Instance.ConsumerFileProduced(path);
m_WriteToDiskSampler.End();

void Write(List<PendingCapture> pendingCaptures, SimulationState simulationState, int captureFileIndex)
{
m_ActiveReporter.ProcessPendingCaptures(pendingCaptures, simulationState);
GetActiveReporter()?.ProcessPendingCaptures(pendingCaptures, simulationState);
#if false
simulationState.m_SerializeCapturesAsyncSampler.Begin();

6
com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs


using System;
using Unity.Simulation;
using UnityEngine.Perception.GroundTruth;
namespace UnityEngine.Perception.Randomization.Scenarios
{

{
DeserializeFromFile(new Uri(Configuration.Instance.SimulationConfig.app_param_uri).LocalPath);
constants.instanceIndex = int.Parse(Configuration.Instance.GetInstanceId()) - 1;
// Debug.Log($"SS - NOT SETTING - SEE IF WE GET the ADVAPI ERROR");
Debug.Log($"SS - Scenario Import - Setting player prefs ouput format mode: {constants.outputFormat}");
PlayerPrefs.SetString(SimulationState.outputFormatMode, constants.outputFormat);
}
else
base.OnConfigurationImport();

2
com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenarioConstants.cs


/// </summary>
[Tooltip("The Unity Simulation instance index of the currently executing worker.")]
public int instanceIndex;
public string outputFormat;
}
}

8
com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew.meta


fileFormatVersion: 2
guid: 8b80f219b9e258043aeae665de9f8d90
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

94
com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/AnnotationHandler.cs


using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Unity.Simulation;
namespace UnityEngine.Perception.GroundTruth.Exporters.PerceptionNew
{
public static class AnnotationHandler
{
[Serializable]
struct BoundingBox2dRecord
{
public uint instanceId;
public int frame;
public int labelId;
public string labelName;
public float x;
public float y;
public float width;
public float height;
public string annotationId;
public string annotationDefinition;
public static BoundingBox2dRecord FromBoundingBoxValue(Guid annotationId, Guid annotationDefinition, BoundingBox2DLabeler.BoundingBoxValue bbox)
{
return new BoundingBox2dRecord
{
instanceId = bbox.instance_id,
frame = bbox.frame,
labelId = bbox.label_id,
labelName = bbox.label_name,
x = bbox.x,
y = bbox.y,
width = bbox.width,
height = bbox.height,
annotationId = annotationId.ToString(),
annotationDefinition = annotationDefinition.ToString()
};
}
public string ToJson()
{
return JsonUtility.ToJson(this, true);
}
}
public static async Task WriteOutJson(string path, string filename, string json)
{
if (true)
{
json = JToken.Parse(json).ToString(Formatting.Indented);
}
var writePath = Path.Combine(path, filename);
var file = File.CreateText(writePath);
await file.WriteAsync(json);
file.Close();
Manager.Instance.ConsumerFileProduced(writePath);
}
static async Task HandleBoundingBoxAnnotation(string path, Annotation annotation, AnnotationDefinition def, BoundingBox2DLabeler.BoundingBoxValue bbox)
{
var id = annotation.Id;
var defId = def.Id;
var converted = BoundingBox2dRecord.FromBoundingBoxValue(id, defId, bbox);
var filename = $"frame_{converted.frame}_id_{converted.instanceId}_bounding_box_2d.json";
var writePath = Path.Combine(path, filename);
var file = File.CreateText(writePath);
await file.WriteAsync(converted.ToJson());
file.Close();
Manager.Instance.ConsumerFileProduced(writePath);
}
public static async Task HandleAnnotation(string path, Annotation annotation, AnnotationDefinition def, object annotatedData)
{
switch (annotatedData)
{
case BoundingBox2DLabeler.BoundingBoxValue bbox:
await HandleBoundingBoxAnnotation(path, annotation, def, bbox);
break;
}
}
}
}

11
com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/AnnotationHandler.cs.meta


fileFormatVersion: 2
guid: 9c350f412031d374ebe3027d0e8283de
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

151
com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/PerceptionNewExporter.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Simulation;
using UnityEngine.Perception.GroundTruth.Exporters.PerceptionFormat;
namespace UnityEngine.Perception.GroundTruth.Exporters.PerceptionNew
{
public class PerceptionNewExporter : IDatasetExporter
{
public bool prettyPrint = true;
string m_DirectoryName = string.Empty;
int m_UnknownFrameCount = 0;
public string GetRgbCaptureFilename(params(string, object)[] additionalSensorValues)
{
var frameArray = additionalSensorValues.Where(p => p.Item1 == "frame").Select(p => p.Item2);
var frame = frameArray.Any() ? (int)frameArray.First() : m_UnknownFrameCount++;
return Path.Combine(m_DirectoryName, $"rgb_{frame}.png");
}
public void OnSimulationBegin(string directoryName)
{
Debug.Log($"SS - New Perception - OnSimBegin");
m_Metadata = new Metadata
{
version = "0.0.1",
image_width = 0,
image_height = 0,
dataset_size = 0
};
m_DirectoryName = directoryName + Path.DirectorySeparatorChar + Guid.NewGuid() + Path.DirectorySeparatorChar;
if (!Directory.Exists(m_DirectoryName))
Directory.CreateDirectory(m_DirectoryName);
}
[Serializable]
struct Metadata
{
public string version;
public int image_width;
public int image_height;
public int dataset_size;
}
Metadata m_Metadata;
public void OnSimulationEnd()
{
Debug.Log($"SS - New Perception - OnSimEnd");
var writePath = Path.Combine(m_DirectoryName, "metadata.json");
var file = File.CreateText(writePath);
Debug.Log("SS - New Perception - writing");
file.Write(JsonUtility.ToJson(m_Metadata, true));
file.Close();
Manager.Instance.ConsumerFileProduced(writePath);
Task.WhenAll(m_PendingTasks);
}
public void OnAnnotationRegistered<TSpec>(Guid annotationId, TSpec[] values)
{
// Right now, do nothing :-)
}
static bool GetFilenameForAnnotation(object rawData, out string filename)
{
filename = string.Empty;
if (rawData is BoundingBox2DLabeler.BoundingBoxValue bbox)
{
var frame = bbox.frame;
filename = $"frame_{frame}_bounding_bocx_2d.json";
return true;
}
return false;
}
// TODO - handle the 1000's of file writes we will be doing in a more intelligent fashion. Perhaps create a bg thread
// that reads json records off of a queue and writes them out
List<Task> m_PendingTasks = new List<Task>();
public Task ProcessPendingCaptures(List<SimulationState.PendingCapture> pendingCaptures, SimulationState simState)
{
foreach (var cap in pendingCaptures)
{
foreach (var (annotation, annotationData) in cap.Annotations)
{
// Create a file for the annotation
#if false
if (annotationData.RawValues.Any())
{
var first = annotationData.RawValues.First();
if (GetFilenameForAnnotation(first, frame, out var filename))
{
var json = new StringBuilder("{");
json.Append(annotationData.ValuesJson);
json.Append("}");
m_PendingTasks.Add(AnnotationHandler.WriteOutJson(m_DirectoryName, filename, json.ToString()));
#if false
// Need to revisit this and handle this in a performant way
var jObject = PerceptionExporter.JObjectFromAnnotation((annotation, annotationData));
PerceptionExporter.WriteJObjectToFile(jObject, m_DirectoryName, filename);
#endif
}
}
#endif
foreach (var rawValue in annotationData.RawValues)
{
if (GetFilenameForAnnotation(rawValue, out var filename))
{
#if true
var json = new StringBuilder();
json.Append(annotationData.ValuesJson);
m_PendingTasks.Add(AnnotationHandler.WriteOutJson(m_DirectoryName, filename, json.ToString()));
#else
// Need to revisit this and handle this in a performant way
var jObject = PerceptionExporter.JObjectFromAnnotation((annotation, annotationData));
PerceptionExporter.WriteJObjectToFile(jObject, m_DirectoryName, filename);
#endif
}
}
}
}
return Task.CompletedTask;
}
public Task OnCaptureReported(int frame, int width, int height, string filename)
{
m_Metadata.dataset_size++;
m_Metadata.image_height = height;
m_Metadata.image_width = width;
return null;
}
}
}

11
com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/PerceptionNewExporter.cs.meta


fileFormatVersion: 2
guid: 50287c95e34d8354d935c14caf487750
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存