Skip to content

Commit 204224e

Browse files
JonathanHUnityaryan-mann
authored andcommitted
Fixing keypoint bugs when visualizations are disabled (#290)
* Fixing keypoint bugs when visualizations are disabled (enabling async readback) * Remove commented out code * Pointing to stable ubuntu to work around issue in latest.
1 parent df2a2f8 commit 204224e

File tree

3 files changed

+91
-8
lines changed

3 files changed

+91
-8
lines changed

.yamato/environments.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_platforms:
5353
standalone-platform: StandaloneOSX
5454
- name: ubuntu
5555
type: Unity::VM
56-
image: package-ci/ubuntu:latest
56+
image: package-ci/ubuntu:stable
5757
flavor: b1.large
5858

5959
performance_platforms:
@@ -71,7 +71,7 @@ performance_platforms:
7171
standalone-platform: StandaloneOSX
7272
- name: ubuntu
7373
type: Unity::VM
74-
image: package-ci/ubuntu:latest
74+
image: package-ci/ubuntu:stable
7575
flavor: b1.large
7676

7777
performance_suites:

com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ void OnInstanceSegmentationImageReadback(int frameCount, NativeArray<Color32> da
177177
{
178178
if (InstanceIdToColorMapping.TryGetColorFromInstanceId(keypointSet.Key, out var idColor))
179179
{
180-
foreach (var keypoint in keypointSet.Value.keypoints)
180+
for (var i = 0; i < keypointSet.Value.keypoints.Length; i++)
181181
{
182+
var keypoint = keypointSet.Value.keypoints[i];
182183
keypoint.state = DetermineKeypointState(keypoint, idColor, dimensions, data);
183184

184185
if (keypoint.state == 0)
@@ -191,6 +192,8 @@ void OnInstanceSegmentationImageReadback(int frameCount, NativeArray<Color32> da
191192
keypoint.x = math.clamp(keypoint.x, 0, dimensions.width - .001f);
192193
keypoint.y = math.clamp(keypoint.y, 0, dimensions.height - .001f);
193194
}
195+
196+
keypointSet.Value.keypoints[i] = keypoint;
194197
}
195198
}
196199
}
@@ -286,7 +289,7 @@ public class KeypointEntry
286289
/// The values of a specific keypoint
287290
/// </summary>
288291
[Serializable]
289-
public class Keypoint
292+
public struct Keypoint
290293
{
291294
/// <summary>
292295
/// The index of the keypoint in the template file
@@ -458,7 +461,17 @@ void ProcessLabel(Labeling labeledEntity)
458461
cachedData.keypoints.pose = GetPose(cachedData.animator);
459462
}
460463

461-
m_AsyncAnnotations[m_CurrentFrame].keypoints[labeledEntity.instanceId] = cachedData.keypoints;
464+
465+
var cachedKeypointEntry = cachedData.keypoints;
466+
var keypointEntry = new KeypointEntry()
467+
{
468+
instance_id = cachedKeypointEntry.instance_id,
469+
keypoints = cachedKeypointEntry.keypoints.ToArray(),
470+
label_id = cachedKeypointEntry.label_id,
471+
pose = cachedKeypointEntry.pose,
472+
template_guid = cachedKeypointEntry.template_guid
473+
};
474+
m_AsyncAnnotations[m_CurrentFrame].keypoints[labeledEntity.instanceId] = keypointEntry;
462475
}
463476
}
464477

@@ -506,7 +519,7 @@ string GetPose(Animator animator)
506519
return "unset";
507520
}
508521

509-
Keypoint GetKeypointForJoint(KeypointEntry entry, int joint)
522+
Keypoint? GetKeypointForJoint(KeypointEntry entry, int joint)
510523
{
511524
if (joint < 0 || joint >= entry.keypoints.Length) return null;
512525
return entry.keypoints[joint];
@@ -530,9 +543,9 @@ protected override void OnVisualize()
530543
var joint1 = GetKeypointForJoint(entry, bone.joint1);
531544
var joint2 = GetKeypointForJoint(entry, bone.joint2);
532545

533-
if (joint1 != null && joint1.state == 2 && joint2 != null && joint2.state == 2)
546+
if (joint1 != null && joint1.Value.state == 2 && joint2 != null && joint2.Value.state == 2)
534547
{
535-
VisualizationHelper.DrawLine(joint1.x, joint1.y, joint2.x, joint2.y, bone.color, 8, skeletonTexture);
548+
VisualizationHelper.DrawLine(joint1.Value.x, joint1.Value.y, joint2.Value.x, joint2.Value.y, bone.color, 8, skeletonTexture);
536549
}
537550
}
538551

com.unity.perception/Tests/Runtime/GroundTruthTests/KeypointGroundTruthTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,76 @@ public IEnumerator Keypoint_TestAllOffScreen()
436436
}
437437
}
438438

439+
[UnityTest]
440+
public IEnumerator Keypoint_TestMovingCube()
441+
{
442+
var incoming = new List<List<KeypointLabeler.KeypointEntry>>();
443+
444+
var keypoints = new[]
445+
{
446+
new KeypointDefinition
447+
{
448+
label = "Center",
449+
associateToRig = false,
450+
color = Color.black
451+
}
452+
};
453+
var template = ScriptableObject.CreateInstance<KeypointTemplate>();
454+
template.templateID = Guid.NewGuid().ToString();
455+
template.templateName = "label";
456+
template.jointTexture = null;
457+
template.skeletonTexture = null;
458+
template.keypoints = keypoints;
459+
template.skeleton = new SkeletonDefinition[0];
460+
461+
var texture = new RenderTexture(1024, 768, 16);
462+
texture.Create();
463+
var cam = SetupCamera(SetUpLabelConfig(), template, (frame, data) =>
464+
{
465+
incoming.Add(new List<KeypointLabeler.KeypointEntry>(data));
466+
}, texture);
467+
cam.GetComponent<PerceptionCamera>().showVisualizations = false;
468+
469+
var cube = TestHelper.CreateLabeledCube(scale: 6, z: 8);
470+
SetupCubeJoint(cube, template, "Center", 0, 0, 0);
471+
472+
cube.SetActive(true);
473+
cam.SetActive(true);
474+
475+
AddTestObjectForCleanup(cam);
476+
AddTestObjectForCleanup(cube);
477+
478+
yield return null;
479+
cube.transform.localPosition = new Vector3(-1, 0, 0);
480+
yield return null;
481+
482+
//force all async readbacks to complete
483+
DestroyTestObject(cam);
484+
texture.Release();
485+
486+
var testCase = incoming[0];
487+
Assert.AreEqual(1, testCase.Count);
488+
var t = testCase.First();
489+
Assert.NotNull(t);
490+
Assert.AreEqual(1, t.instance_id);
491+
Assert.AreEqual(1, t.label_id);
492+
Assert.AreEqual(template.templateID.ToString(), t.template_guid);
493+
Assert.AreEqual(1, t.keypoints.Length);
494+
495+
Assert.AreEqual(1024 / 2, t.keypoints[0].x);
496+
Assert.AreEqual(768 / 2, t.keypoints[0].y);
497+
498+
Assert.AreEqual(0, t.keypoints[0].index);
499+
Assert.AreEqual(2, t.keypoints[0].state);
500+
501+
502+
var testCase2 = incoming[1];
503+
Assert.AreEqual(1, testCase2.Count);
504+
var t2 = testCase2.First();
505+
Assert.AreEqual(445, t2.keypoints[0].x, 1);
506+
Assert.AreEqual(768 / 2, t2.keypoints[0].y);
507+
}
508+
439509
[UnityTest]
440510
public IEnumerator Keypoint_TestPartialOffScreen([Values(1,5)] int framesToRunBeforeAsserting)
441511
{

0 commit comments

Comments
 (0)