using System; using System.Collections.Generic; using System.Linq; using Unity.Simulation; using UnityEngine.Perception.GroundTruth.DataModel; namespace UnityEngine.Perception.GroundTruth { public partial class SimulationState { Dictionary m_SequenceMap = new Dictionary(); Sensor ToSensor(PendingFrame pendingFrame, SimulationState simulationState, int captureFileIndex) { var sensor = new RgbSensor { Id = "camera", sensorType = "camera", description = "this is the description of the sensor", position = Vector3.zero, rotation = Vector3.zero, velocity = Vector3.zero, acceleration = Vector3.zero, imageFormat = "png", dimension = Vector2.zero, buffer = null }; return sensor; } // TODO rename this to 'WritePendingFrames' void WritePendingCaptures(bool flush = false, bool writeCapturesFromThisFrame = false) { m_SerializeCapturesSampler.Begin(); var pendingFramesToWrite = new List>(m_PendingFrames.Count); var currentFrame = Time.frameCount; foreach (var frame in m_PendingFrames) { var recordedFrame = m_PendingIdToFrameMap[frame.Value.PendingId]; if ((writeCapturesFromThisFrame || recordedFrame < currentFrame) && frame.Value.IsReadyToReport()) { pendingFramesToWrite.Add(frame); } } foreach (var pf in pendingFramesToWrite) { m_PendingFrames.Remove(pf.Key); } IEnumerable ConvertToSensors(PendingFrame frame, SimulationState simulationState) { return frame.sensors.Values.Where(s => s.IsReadyToReport()).Select(s => s.ToSensor()); } Frame ConvertToFrameData(PendingFrame pendingFrame, SimulationState simState) { var frameId = m_PendingIdToFrameMap[pendingFrame.PendingId]; var frame = new Frame(frameId, pendingFrame.PendingId.Sequence, pendingFrame.PendingId.Step); frame.sensors = ConvertToSensors(pendingFrame, simState); #if false foreach (var annotation in pendingFrame.annotations.Values) { frame.annotations.Add(annotation); } foreach (var metric in pendingFrame.metrics.Values) { frame.metrics.Add(metric); } #endif return frame; } void Write(List> frames, SimulationState simulationState) { foreach (var pendingFrame in frames) { var frame = ConvertToFrameData(pendingFrame.Value, simulationState); GetActiveConsumer()?.OnFrameGenerated(frame); } } if (flush) { Write(pendingFramesToWrite, this); } else { var req = Manager.Instance.CreateRequest>(); req.data = new WritePendingCaptureRequestData() { PendingFrames = pendingFramesToWrite, SimulationState = this }; req.Enqueue(r => { Write(r.data.PendingFrames, r.data.SimulationState); return AsyncRequest.Result.Completed; }); req.Execute(AsyncRequest.ExecutionContext.JobSystem); } m_SerializeCapturesSampler.End(); m_CaptureFileIndex++; } struct WritePendingCaptureRequestData { public List> PendingFrames; public SimulationState SimulationState; } } }