浏览代码

Fix multiple cameras (#203)

* Fixing bug where disabled PerceptionCameras interfere with the enabled PerceptionCamera. Note: this does not add full support for multiple simultaneous cameras.

* Adding test for instance segmentation with a separate disabled perceptioncamera

* Updating changelog

* Fix compilation and test failures
/main
GitHub 4 年前
当前提交
8cf464b0
共有 6 个文件被更改,包括 49 次插入30 次删除
  1. 2
      com.unity.perception/CHANGELOG.md
  2. 27
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  3. 9
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs
  4. 5
      com.unity.perception/Tests/Editor/PerceptionCameraEditorTests.cs
  5. 29
      com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs
  6. 7
      com.unity.perception/Tests/Runtime/GroundTruthTests/VisualizationTests.cs

2
com.unity.perception/CHANGELOG.md


### Fixed
Fixed ground truth not properly produced when there are other disabled PerceptionCameras present. Note: this does not yet add support for multiple enabled PerceptionCameras.
## [0.7.0-preview.2] - 2021-02-08
### Upgrade Notes

27
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()}";
internal string rgbDirectory { get; } = $"RGB{Guid.NewGuid()}";
static string s_RgbFilePrefix = "rgb_";
/// <summary>

return m_PersistentSensorData.Remove(key);
}
// Start is called before the first frame update
void Awake()
void Start()
{
AsyncRequest.maxJobSystemParallelism = 0; // Jobs are not chained to one another in any way, maximizing parallelism
AsyncRequest.maxAsyncRequestFrameAge = 4; // Ensure that readbacks happen before Allocator.TempJob allocations get stale

// Record the camera's projection matrix
SetPersistentSensorData("camera_intrinsic", ToProjectionMatrix3x3(cam.projectionMatrix));
var captureFilename = $"{Manager.Instance.GetDirectoryFor(RgbDirectory)}/{s_RgbFilePrefix}{Time.frameCount}.png";
var dxRootPath = $"{RgbDirectory}/{s_RgbFilePrefix}{Time.frameCount}.png";
var captureFilename = $"{Manager.Instance.GetDirectoryFor(rgbDirectory)}/{s_RgbFilePrefix}{Time.frameCount}.png";
var dxRootPath = $"{rgbDirectory}/{s_RgbFilePrefix}{Time.frameCount}.png";
var flipY = ShouldFlipY(cam);
colorFunctor = r =>
{

CaptureCamera.Capture(cam, colorFunctor, flipY: flipY);
#endif
Profiler.EndSample();
}
// ReSharper disable once ParameterHidesMember
bool ShouldFlipY(Camera camera)
{
#if HDRP_PRESENT
var hdAdditionalCameraData = GetComponent<HDAdditionalCameraData>();
//Based on logic in HDRenderPipeline.PrepareFinalBlitParameters
return hdAdditionalCameraData.flipYMode == HDAdditionalCameraData.FlipYMode.ForceFlipY || (camera.targetTexture == null && camera.cameraType == CameraType.Game);
#elif URP_PRESENT
return (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D11 || SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal) &&
(camera.targetTexture == null && camera.cameraType == CameraType.Game);
#else
return false;
#endif
}
void OnSimulationEnding()

9
com.unity.perception/Runtime/GroundTruth/PerceptionCamera_InstanceSegmentation.cs


internal bool m_fLensDistortionEnabled = false;
#if HDRP_PRESENT || URP_PRESENT
private float? m_LensDistortionIntensityOverride;
#if HDRP_PRESENT
InstanceSegmentationPass m_InstanceSegmentationPass;
LensDistortionPass m_LensDistortionPass;

internal void OverrideLensDistortionIntensity(float? intensity)
{
m_LensDistortionPass.m_LensDistortionCrossPipelinePass.lensDistortionOverride = intensity;
m_LensDistortionIntensityOverride = intensity;
if (m_LensDistortionPass != null)
m_LensDistortionPass.m_LensDistortionCrossPipelinePass.lensDistortionOverride = intensity;
}
#endif

m_RenderedObjectInfoGenerator = new RenderedObjectInfoGenerator();
#if HDRP_PRESENT || URP_PRESENT
#if HDRP_PRESENT
var customPassVolume = this.GetComponent<CustomPassVolume>() ?? gameObject.AddComponent<CustomPassVolume>();
customPassVolume.injectionPoint = CustomPassInjectionPoint.BeforeRendering;

AddScriptableRenderPass(m_LensDistortionPass);
m_fLensDistortionEnabled = true;
#endif
m_LensDistortionPass.m_LensDistortionCrossPipelinePass.lensDistortionOverride =
m_LensDistortionIntensityOverride;
#endif
m_InstanceSegmentationReader = new RenderTextureReader<Color32>(m_InstanceSegmentationTexture, myCamera, (frameCount, data, tex) =>

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


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

cameraObject.name = "Camera";
yield return new EnterPlayMode();
var expectedFirstFrame = Time.frameCount;
yield return null;

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

29
com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs


public class SegmentationPassTests : GroundTruthTestBase
{
static readonly Color32 k_SemanticPixelValue = new Color32(10, 20, 30, Byte.MaxValue);
private static readonly Color32 k_InstanceSegmentationPixelValue = new Color32(255,0,0, 255);
public enum SegmentationKind
{

{
case SegmentationKind.Instance:
//expectedPixelValue = new Color32(0, 74, 255, 255);
expectedPixelValue = new Color32(255,0,0, 255);
expectedPixelValue = k_InstanceSegmentationPixelValue;
cameraObject = SetupCameraInstanceSegmentation(OnSegmentationImageReceived);
break;
case SegmentationKind.Semantic:

Assert.AreEqual(2, timesSegmentationImageReceived);
}
[UnityTest]
public IEnumerator InstanceSegmentationPass_WithSeparateDisabledPerceptionCamera_ProducesCorrectValues()
{
int timesSegmentationImageReceived = 0;
void OnSegmentationImageReceived(NativeArray<Color32> data)
{
CollectionAssert.AreEqual(Enumerable.Repeat(k_InstanceSegmentationPixelValue, data.Length), data);
timesSegmentationImageReceived++;
}
var cameraObject = SetupCameraInstanceSegmentation((frame, data, renderTexture) => OnSegmentationImageReceived(data));
var cameraObject2 = SetupCameraInstanceSegmentation(null);
cameraObject2.SetActive(false);
var plane = TestHelper.CreateLabeledPlane();
AddTestObjectForCleanup(plane);
yield return null;
//destroy the object to force all pending segmented image readbacks to finish and events to be fired.
DestroyTestObject(cameraObject);
DestroyTestObject(cameraObject2);
Assert.AreEqual(1, timesSegmentationImageReceived);
}
[UnityTest]
public IEnumerator SegmentationPassProducesCorrectValuesEachFrame(
[Values(SegmentationKind.Instance, SegmentationKind.Semantic)] SegmentationKind segmentationKind)

SetupCameraSemanticSegmentation((a) => OnSegmentationImageReceived<Color32>(a.frameCount, a.data, a.sourceTexture), false);
//object expectedPixelValue = segmentationKind == SegmentationKind.Instance ? (object) new Color32(0, 74, 255, 255) : k_SemanticPixelValue;
object expectedPixelValue = segmentationKind == SegmentationKind.Instance ? (object) new Color32(255, 0, 0, 255) : k_SemanticPixelValue;
object expectedPixelValue = segmentationKind == SegmentationKind.Instance ? (object) k_InstanceSegmentationPixelValue : k_SemanticPixelValue;
expectedLabelAtFrame = new Dictionary<int, object>
{

7
com.unity.perception/Tests/Runtime/GroundTruthTests/VisualizationTests.cs


Assert.IsNotNull(GameObject.Find("overlay_canvas"));
}
[Test]
public void TwoCamerasVisualizing_CausesWarningAndDisablesVisualization()
[UnityTest]
public IEnumerator TwoCamerasVisualizing_CausesWarningAndDisablesVisualization()
{
var object1 = new GameObject();
object1.name = nameof(TwoCamerasVisualizing_CausesWarningAndDisablesVisualization);

AddTestObjectForCleanup(object2);
object1.SetActive(true);
yield return null;
LogAssert.ignoreFailingMessages = true;
yield return null;
}
[UnityTest]
public IEnumerator DestroyCamera_RemovesVisualization()

正在加载...
取消
保存