浏览代码

some work in progress

/multiple-perception-cameras
Mohsen Kamalzadeh 4 年前
当前提交
a94c4e4d
共有 3 个文件被更改,包括 67 次插入16 次删除
  1. 9
      com.unity.perception/Runtime/GroundTruth/Labelers/SemanticSegmentationLabeler.cs
  2. 66
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  3. 8
      com.unity.perception/Tests/Editor/PerceptionCameraEditorTests.cs

9
com.unity.perception/Runtime/GroundTruth/Labelers/SemanticSegmentationLabeler.cs


protected set {}
}
const string k_SemanticSegmentationDirectory = "SemanticSegmentation";
//const string k_SemanticSegmentationDirectory = "SemanticSegmentation";
string SemanticSegmentationDirectory => "SemanticSegmentation" + perceptionCamera.CameraGuid;
const string k_SegmentationFilePrefix = "segmentation_";
/// <summary>

m_TargetTextureOverride = new RenderTexture(camWidth, camHeight, 8, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
targetTexture.Create();
targetTexture.name = "Labeling";
targetTexture.name = "SemanticLabelerTexture_" + perceptionCamera.CameraGuid;
#if HDRP_PRESENT
var gameObject = perceptionCamera.gameObject;

if (!m_AsyncAnnotations.TryGetValue(frameCount, out var annotation))
return;
var datasetRelativePath = $"{k_SemanticSegmentationDirectory}/{k_SegmentationFilePrefix}{frameCount}.png";
var localPath = $"{Manager.Instance.GetDirectoryFor(k_SemanticSegmentationDirectory)}/{k_SegmentationFilePrefix}{frameCount}.png";
var datasetRelativePath = $"{SemanticSegmentationDirectory}/{k_SegmentationFilePrefix}{frameCount}.png";
var localPath = $"{Manager.Instance.GetDirectoryFor(SemanticSegmentationDirectory)}/{k_SegmentationFilePrefix}{frameCount}.png";
annotation.ReportFile(datasetRelativePath);

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


public partial class PerceptionCamera : MonoBehaviour
{
//TODO: Remove the Guid path when we have proper dataset merging in Unity Simulation and Thea
internal static string RgbDirectory { get; } = $"RGB{Guid.NewGuid()}";
private string m_Guid = string.Empty;
public string RgbDirectory => $"RGB{CameraGuid}";
public string CameraGuid {
get
{
if (m_Guid == string.Empty)
{
m_Guid = Guid.NewGuid().ToString();
}
return m_Guid;
}
}
static string s_RgbFilePrefix = "rgb_";
/// <summary>

/// </summary>
public bool captureRgbImages = true;
Camera m_AttachedCamera = null;
private Camera m_AttachedCamera = null;
public Camera attachedCamera => m_AttachedCamera;
public Camera attachedCamera
{
get
{
if (m_AttachedCamera == null)
{
m_AttachedCamera = GetComponent<Camera>();
}
return m_AttachedCamera;
}
}
/// <summary>
/// Event invoked after the camera finishes rendering during a frame.

List<CameraLabeler> m_Labelers = new List<CameraLabeler>();
Dictionary<string, object> m_PersistentSensorData = new Dictionary<string, object>();
private bool m_IsHighestDepthCamera;
int m_LastFrameCaptured = -1;

AsyncRequest.maxAsyncRequestFrameAge = 4; // Ensure that readbacks happen before Allocator.TempJob allocations get stale
SetupInstanceSegmentation();
m_AttachedCamera = GetComponent<Camera>();
SetupVisualizationCamera(m_AttachedCamera);
SetupVisualizationCamera(attachedCamera);
//AddTargetTextureIfMultiCam(attachedCamera);
void AddTargetTextureIfMultiCam(Camera cam)
{
var perceptionCams = FindObjectsOfType<PerceptionCamera>();
if (perceptionCams.Length > 1)
{
cam.targetTexture = RenderTexture.GetTemporary(cam.pixelWidth, cam.pixelHeight, 0, GraphicsFormat.R8G8B8A8_SRGB);
var maxDepth = perceptionCams.ToList().Max(c => c.attachedCamera.depth);
if (cam.depth == maxDepth)
{
m_IsHighestDepthCamera = true;
}
}
}
void EnsureSensorRegistered()
{
if (m_SensorHandle.IsNil)

void CheckForRendererFeature(ScriptableRenderContext context, Camera camera)
{
if (camera == m_AttachedCamera)
if (camera == attachedCamera)
{
#if URP_PRESENT
if (!m_GroundTruthRendererFeatureRun)

if (!SensorHandle.IsValid)
return;
m_AttachedCamera.enabled = SensorHandle.ShouldCaptureThisFrame;
attachedCamera.enabled = SensorHandle.ShouldCaptureThisFrame;
bool anyVisualizing = false;
foreach (var labeler in m_Labelers)

if (m_ShowingVisualizations)
CaptureOptions.useAsyncReadbackIfSupported = !anyVisualizing;
if (m_IsHighestDepthCamera)
{
Graphics.Blit(attachedCamera.targetTexture, null as RenderTexture);
}
}
private Vector2 scrollPosition;

if (labeler.isInitialized)
labeler.InternalCleanup();
}
var tmp = attachedCamera.targetTexture;
attachedCamera.targetTexture = null;
RenderTexture.ReleaseTemporary(tmp);
if (cam != m_AttachedCamera)
if (cam != attachedCamera)
return;
if (!SensorHandle.ShouldCaptureThisFrame)
return;

8
com.unity.perception/Tests/Editor/PerceptionCameraEditorTests.cs


public IEnumerator EditorPause_DoesNotLogErrors()
{
ResetScene();
SetupCamera(p =>
var pCamera = SetupCamera(p =>
{
var idLabelConfig = ScriptableObject.CreateInstance<IdLabelConfig>();
p.captureRgbImages = true;

var capturesJson = File.ReadAllText(capturesPath);
for (int iFrameCount = expectedFirstFrame; iFrameCount <= expectedLastFrame; iFrameCount++)
{
var imagePath = $"{PerceptionCamera.RgbDirectory}/rgb_{iFrameCount}";
var imagePath = $"{pCamera.RgbDirectory}/rgb_{iFrameCount}";
StringAssert.Contains(imagePath, capturesJson);
}

}
#endif
static GameObject SetupCamera(Action<PerceptionCamera> initPerceptionCameraCallback)
static PerceptionCamera SetupCamera(Action<PerceptionCamera> initPerceptionCameraCallback)
{
var cameraObject = new GameObject();
cameraObject.SetActive(false);

initPerceptionCameraCallback?.Invoke(perceptionCamera);
cameraObject.SetActive(true);
return cameraObject;
return perceptionCamera;
}
}
}
正在加载...
取消
保存