Steve Borkman
3 年前
当前提交
7910da17
共有 19 个文件被更改,包括 432 次插入 和 35 次删除
-
33com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
-
5com.unity.perception/Editor/Randomization/Uxml/RunInUnitySimulationWindow.uxml
-
7com.unity.perception/Runtime/GroundTruth/DatasetCapture.cs
-
10com.unity.perception/Runtime/GroundTruth/Exporters/Coco/AnnotationHandler.cs
-
39com.unity.perception/Runtime/GroundTruth/Exporters/Coco/CocoExporter.cs
-
2com.unity.perception/Runtime/GroundTruth/Exporters/Coco/CocoTypes.cs
-
2com.unity.perception/Runtime/GroundTruth/Exporters/IDatasetExporter.cs
-
19com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionFormat/PerceptionExporter.cs
-
6com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
-
58com.unity.perception/Runtime/GroundTruth/SimulationState.cs
-
3com.unity.perception/Runtime/GroundTruth/SimulationState_Json.cs
-
6com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs
-
2com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenarioConstants.cs
-
8com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew.meta
-
94com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/AnnotationHandler.cs
-
11com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/AnnotationHandler.cs.meta
-
151com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/PerceptionNewExporter.cs
-
11com.unity.perception/Runtime/GroundTruth/Exporters/PerceptionNew/PerceptionNewExporter.cs.meta
|
|||
fileFormatVersion: 2 |
|||
guid: 8b80f219b9e258043aeae665de9f8d90 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; |
|||
} |
|||
|
|||
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9c350f412031d374ebe3027d0e8283de |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 50287c95e34d8354d935c14caf487750 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue