Skip to content

Commit

Permalink
Update Detector classes to run their tfjs detectors off of a sing…
Browse files Browse the repository at this point in the history
…le static `Detector.currentFrame` instance variable
  • Loading branch information
Valkryst committed Dec 6, 2023
1 parent 754c765 commit 0f9b979
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
3 changes: 1 addition & 2 deletions js/detector/body_detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class BodyDetector extends Detector {
BodyDetector.instance = this;

this.mesh = mesh;
this.inputElement = document.getElementById("jellron-video-canvas");

poseDetection.createDetector(
poseDetection.SupportedModels.MoveNet,
Expand All @@ -56,7 +55,7 @@ export class BodyDetector extends Detector {

let rawBodies = [];
try {
rawBodies = await this.detector.estimatePoses(this.inputElement);
rawBodies = await this.detector.estimatePoses(Detector.getCurrentFrame());
} catch (e) {
/*
* Depending on the state of the video element, this can throw a "Requested texture size [0x0] is
Expand Down
23 changes: 23 additions & 0 deletions js/detector/detector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {RunnableInterval} from "../runnable_interval.js";
import {validateInstanceOf} from "../utility/validation.js";

export class Detector extends RunnableInterval {
/** Current frame to be processed. */
static currentFrame = null;

/** Creates a new Detector. */
constructor() {
super();
Expand All @@ -25,4 +29,23 @@ export class Detector extends RunnableInterval {
isReady() {
return this.detector != null;
}

/**
* Retrieves the current frame.
*
* @returns {Tensor} Current frame.
*/
static getCurrentFrame() {
return Detector.currentFrame;
}

/**
* Sets the current frame.
*
* @param frame New current frame.
*/
static setCurrentFrame(frame) {
Detector.currentFrame?.dispose();
Detector.currentFrame = frame;
}
}
3 changes: 1 addition & 2 deletions js/detector/face_detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class FaceDetector extends Detector {
FaceDetector.instance = this;

this.mesh = mesh;
this.inputElement = document.getElementById("jellron-video-canvas");

faceLandmarksDetection.createDetector(
faceLandmarksDetection.SupportedModels.MediaPipeFaceMesh,
Expand All @@ -56,7 +55,7 @@ export class FaceDetector extends Detector {

let rawFaces = [];
try {
rawFaces = await this.detector.estimateFaces(this.inputElement);
rawFaces = await this.detector.estimateFaces(Detector.getCurrentFrame());
} catch (e) {
/*
* Depending on the state of the video element, this can throw a "Requested texture size [0x0] is
Expand Down
3 changes: 1 addition & 2 deletions js/detector/hand_detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class HandDetector extends Detector {
HandDetector.instance = this;

this.mesh = mesh;
this.inputElement = document.getElementById("jellron-video-canvas");

handPoseDetection.createDetector(
handPoseDetection.SupportedModels.MediaPipeHands,
Expand All @@ -56,7 +55,7 @@ export class HandDetector extends Detector {

let rawHands = [];
try {
rawHands = await this.detector.estimateHands(this.inputElement);
rawHands = await this.detector.estimateHands(Detector.getCurrentFrame());
} catch (e) {
/*
* Depending on the state of the video element, this can throw a "Requested texture size [0x0] is
Expand Down
5 changes: 5 additions & 0 deletions js/renderer/keypoint_renderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Detector} from "../detector/detector.js";
import {Keypoint} from "../keypoint.js";
import {Mesh} from "../mesh.js";
import {Renderer} from "./renderer.js";
Expand Down Expand Up @@ -37,6 +38,7 @@ export class KeypointRenderer extends Renderer {
}

const scene = new Scene();
const videoCanvas = document.getElementById("jellron-video-canvas");

this.dispatchEvent(new CustomEvent("started"));
this.intervalId = setInterval(async () => {
Expand Down Expand Up @@ -110,6 +112,9 @@ export class KeypointRenderer extends Renderer {

this.glContext.render(scene, this.getCamera());

// We set this here because the KeypointRenderer runs at a lower framerate than VideoRenderer.
Detector.setCurrentFrame(tf.browser.fromPixels(videoCanvas));

this.lastRuntime = performance.now() - currentTime;
this.dispatchEvent(new CustomEvent("rendered", {detail: {runtime: this.lastRuntime}}));
}, 1000 / KeypointRenderer.fps);
Expand Down
9 changes: 9 additions & 0 deletions js/renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export class Renderer extends RunnableInterval {
return camera;
}

/**
* Retrieves the canvas to use for rendering.
*
* @returns {HTMLCanvasElement} Canvas to use for rendering.
*/
getCanvas() {
return this.canvas;
}

/**
* Sets the canvas to use for rendering.
*
Expand Down

0 comments on commit 0f9b979

Please sign in to comment.