Skip to content

Commit d30e048

Browse files
committed
Update code to hide keypoints if they cannot be detected
1 parent c317d5b commit d30e048

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The _Detector_ classes emit the following events:
4040
* `ready` - Emitted when the detector's TFJS model has finished loading.
4141
* `started` - Emitted when the detector starts running.
4242
* `stopped` - Emitted when the detector stops running.
43-
* `updated` - Emitted when the detector has _detected and updated_ the position of its keypoints.
43+
* `updated` - Emitted when the detector has updated the state of its keypoints.
4444
* The event's `runtime` property contains the number of milliseconds taken to detect and update the keypoints.
4545

4646
#### Renderer Events

js/detector/body_detector.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,11 @@ export class BodyDetector extends Detector {
6464
*/
6565
}
6666

67-
if (rawBodies.length === 0) {
68-
this.lastRuntime = performance.now() - currentTime;
69-
return;
67+
if (rawBodies.length > 0) {
68+
const rawBody = rawBodies[0];
69+
this.mesh.updateBodyKeypoints(rawBody);
7070
}
7171

72-
const rawBody = rawBodies[0];
73-
this.mesh.updateBodyKeypoints(rawBody);
74-
7572
this.lastRuntime = performance.now() - currentTime;
7673
this.dispatchEvent(new CustomEvent("updated", {detail: {runtime: this.lastRuntime}}));
7774
}, 1000 / BodyDetector.fps);

js/detector/face_detector.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ export class FaceDetector extends Detector {
6464
*/
6565
}
6666

67-
if (rawFaces.length === 0) {
68-
this.lastRuntime = performance.now() - currentTime;
69-
return;
70-
}
71-
72-
const rawFace = rawFaces[0];
73-
for (let i = 0 ; i < rawFace.keypoints.length ; i++) {
74-
this.relabelKeypoint(i, rawFace.keypoints[i]);
67+
let rawFace = null;
68+
if (rawFaces.length > 0) {
69+
rawFace = rawFaces[0];
70+
for (let i = 0 ; i < rawFace.keypoints.length ; i++) {
71+
this.relabelKeypoint(i, rawFace.keypoints[i]);
72+
}
7573
}
7674

7775
this.mesh.updateFaceKeypoints(rawFace);

js/detector/hand_detector.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,11 @@ export class HandDetector extends Detector {
6464
*/
6565
}
6666

67-
if (rawHands.length === 0) {
68-
this.lastRuntime = performance.now() - currentTime;
69-
return;
70-
}
71-
72-
for (const rawHand of rawHands) {
73-
for (const rawKeypoint of rawHand.keypoints) {
74-
this.relabelKeypoint(rawHand, rawKeypoint);
67+
if (rawHands.length > 0) {
68+
for (const rawHand of rawHands) {
69+
for (const rawKeypoint of rawHand.keypoints) {
70+
this.relabelKeypoint(rawHand, rawKeypoint);
71+
}
7572
}
7673
}
7774

js/mesh.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ export class Mesh {
5454
* }]} rawFace Raw face data.
5555
*/
5656
updateFaceKeypoints(rawFace) {
57-
// todo We know the face is not detected if rawFace is null, so we could hide/show jewellery based on this.
58-
if (rawFace == null || rawFace.keypoints == null) {
57+
if (rawFace == null || rawFace.keypoints == null || rawFace.length === 0 || rawFace.keypoints.length === 0) {
58+
for (let i = 0 ; i < this.faceKeypoints.length; i++) {
59+
this.faceKeypoints[i].setConfidence(0);
60+
}
5961
return;
6062
}
6163

@@ -91,8 +93,10 @@ export class Mesh {
9193
* }]} rawBody Raw body data.
9294
*/
9395
updateBodyKeypoints(rawBody) {
94-
// todo We know the body is not detected if rawPost is null, so we could hide/show jewellery based on this.
95-
if (rawBody == null || rawBody.keypoints == null) {
96+
if (rawBody == null || rawBody.keypoints == null || rawBody.length === 0 || rawBody.keypoints.length === 0) {
97+
for (let i = 0 ; i < this.bodyKeypoints.length; i++) {
98+
this.bodyKeypoints[i].setConfidence(0);
99+
}
96100
return;
97101
}
98102

@@ -130,8 +134,10 @@ export class Mesh {
130134
* }]} rawHands
131135
*/
132136
updateHandKeypoints(rawHands) {
133-
// todo Check what happens when one or both hands aren't visible.
134137
if (rawHands == null || rawHands.length === 0) {
138+
for (const keypoint of this.handKeypoints) {
139+
keypoint.setConfidence(0);
140+
}
135141
return;
136142
}
137143

0 commit comments

Comments
 (0)