Skip to content

Commit

Permalink
Update code to hide keypoints if they cannot be detected
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkryst committed Dec 5, 2023
1 parent c317d5b commit d30e048
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The _Detector_ classes emit the following events:
* `ready` - Emitted when the detector's TFJS model has finished loading.
* `started` - Emitted when the detector starts running.
* `stopped` - Emitted when the detector stops running.
* `updated` - Emitted when the detector has _detected and updated_ the position of its keypoints.
* `updated` - Emitted when the detector has updated the state of its keypoints.
* The event's `runtime` property contains the number of milliseconds taken to detect and update the keypoints.

#### Renderer Events
Expand Down
9 changes: 3 additions & 6 deletions js/detector/body_detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ export class BodyDetector extends Detector {
*/
}

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

const rawBody = rawBodies[0];
this.mesh.updateBodyKeypoints(rawBody);

this.lastRuntime = performance.now() - currentTime;
this.dispatchEvent(new CustomEvent("updated", {detail: {runtime: this.lastRuntime}}));
}, 1000 / BodyDetector.fps);
Expand Down
14 changes: 6 additions & 8 deletions js/detector/face_detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ export class FaceDetector extends Detector {
*/
}

if (rawFaces.length === 0) {
this.lastRuntime = performance.now() - currentTime;
return;
}

const rawFace = rawFaces[0];
for (let i = 0 ; i < rawFace.keypoints.length ; i++) {
this.relabelKeypoint(i, rawFace.keypoints[i]);
let rawFace = null;
if (rawFaces.length > 0) {
rawFace = rawFaces[0];
for (let i = 0 ; i < rawFace.keypoints.length ; i++) {
this.relabelKeypoint(i, rawFace.keypoints[i]);
}
}

this.mesh.updateFaceKeypoints(rawFace);
Expand Down
13 changes: 5 additions & 8 deletions js/detector/hand_detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ export class HandDetector extends Detector {
*/
}

if (rawHands.length === 0) {
this.lastRuntime = performance.now() - currentTime;
return;
}

for (const rawHand of rawHands) {
for (const rawKeypoint of rawHand.keypoints) {
this.relabelKeypoint(rawHand, rawKeypoint);
if (rawHands.length > 0) {
for (const rawHand of rawHands) {
for (const rawKeypoint of rawHand.keypoints) {
this.relabelKeypoint(rawHand, rawKeypoint);
}
}
}

Expand Down
16 changes: 11 additions & 5 deletions js/mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export class Mesh {
* }]} rawFace Raw face data.
*/
updateFaceKeypoints(rawFace) {
// todo We know the face is not detected if rawFace is null, so we could hide/show jewellery based on this.
if (rawFace == null || rawFace.keypoints == null) {
if (rawFace == null || rawFace.keypoints == null || rawFace.length === 0 || rawFace.keypoints.length === 0) {
for (let i = 0 ; i < this.faceKeypoints.length; i++) {
this.faceKeypoints[i].setConfidence(0);
}
return;
}

Expand Down Expand Up @@ -91,8 +93,10 @@ export class Mesh {
* }]} rawBody Raw body data.
*/
updateBodyKeypoints(rawBody) {
// todo We know the body is not detected if rawPost is null, so we could hide/show jewellery based on this.
if (rawBody == null || rawBody.keypoints == null) {
if (rawBody == null || rawBody.keypoints == null || rawBody.length === 0 || rawBody.keypoints.length === 0) {
for (let i = 0 ; i < this.bodyKeypoints.length; i++) {
this.bodyKeypoints[i].setConfidence(0);
}
return;
}

Expand Down Expand Up @@ -130,8 +134,10 @@ export class Mesh {
* }]} rawHands
*/
updateHandKeypoints(rawHands) {
// todo Check what happens when one or both hands aren't visible.
if (rawHands == null || rawHands.length === 0) {
for (const keypoint of this.handKeypoints) {
keypoint.setConfidence(0);
}
return;
}

Expand Down

0 comments on commit d30e048

Please sign in to comment.