File tree Expand file tree Collapse file tree 6 files changed +40
-6
lines changed Expand file tree Collapse file tree 6 files changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,6 @@ export class BodyDetector extends Detector {
29
29
BodyDetector . instance = this ;
30
30
31
31
this . mesh = mesh ;
32
- this . inputElement = document . getElementById ( "jellron-video-canvas" ) ;
33
32
34
33
poseDetection . createDetector (
35
34
poseDetection . SupportedModels . MoveNet ,
@@ -56,7 +55,7 @@ export class BodyDetector extends Detector {
56
55
57
56
let rawBodies = [ ] ;
58
57
try {
59
- rawBodies = await this . detector . estimatePoses ( this . inputElement ) ;
58
+ rawBodies = await this . detector . estimatePoses ( Detector . getCurrentFrame ( ) ) ;
60
59
} catch ( e ) {
61
60
/*
62
61
* Depending on the state of the video element, this can throw a "Requested texture size [0x0] is
Original file line number Diff line number Diff line change 1
1
import { RunnableInterval } from "../runnable_interval.js" ;
2
+ import { validateInstanceOf } from "../utility/validation.js" ;
2
3
3
4
export class Detector extends RunnableInterval {
5
+ /** Current frame to be processed. */
6
+ static currentFrame = null ;
7
+
4
8
/** Creates a new Detector. */
5
9
constructor ( ) {
6
10
super ( ) ;
@@ -25,4 +29,23 @@ export class Detector extends RunnableInterval {
25
29
isReady ( ) {
26
30
return this . detector != null ;
27
31
}
32
+
33
+ /**
34
+ * Retrieves the current frame.
35
+ *
36
+ * @returns {Tensor } Current frame.
37
+ */
38
+ static getCurrentFrame ( ) {
39
+ return Detector . currentFrame ;
40
+ }
41
+
42
+ /**
43
+ * Sets the current frame.
44
+ *
45
+ * @param frame New current frame.
46
+ */
47
+ static setCurrentFrame ( frame ) {
48
+ Detector . currentFrame ?. dispose ( ) ;
49
+ Detector . currentFrame = frame ;
50
+ }
28
51
}
Original file line number Diff line number Diff line change @@ -29,7 +29,6 @@ export class FaceDetector extends Detector {
29
29
FaceDetector . instance = this ;
30
30
31
31
this . mesh = mesh ;
32
- this . inputElement = document . getElementById ( "jellron-video-canvas" ) ;
33
32
34
33
faceLandmarksDetection . createDetector (
35
34
faceLandmarksDetection . SupportedModels . MediaPipeFaceMesh ,
@@ -56,7 +55,7 @@ export class FaceDetector extends Detector {
56
55
57
56
let rawFaces = [ ] ;
58
57
try {
59
- rawFaces = await this . detector . estimateFaces ( this . inputElement ) ;
58
+ rawFaces = await this . detector . estimateFaces ( Detector . getCurrentFrame ( ) ) ;
60
59
} catch ( e ) {
61
60
/*
62
61
* Depending on the state of the video element, this can throw a "Requested texture size [0x0] is
Original file line number Diff line number Diff line change @@ -29,7 +29,6 @@ export class HandDetector extends Detector {
29
29
HandDetector . instance = this ;
30
30
31
31
this . mesh = mesh ;
32
- this . inputElement = document . getElementById ( "jellron-video-canvas" ) ;
33
32
34
33
handPoseDetection . createDetector (
35
34
handPoseDetection . SupportedModels . MediaPipeHands ,
@@ -56,7 +55,7 @@ export class HandDetector extends Detector {
56
55
57
56
let rawHands = [ ] ;
58
57
try {
59
- rawHands = await this . detector . estimateHands ( this . inputElement ) ;
58
+ rawHands = await this . detector . estimateHands ( Detector . getCurrentFrame ( ) ) ;
60
59
} catch ( e ) {
61
60
/*
62
61
* Depending on the state of the video element, this can throw a "Requested texture size [0x0] is
Original file line number Diff line number Diff line change
1
+ import { Detector } from "../detector/detector.js" ;
1
2
import { Keypoint } from "../keypoint.js" ;
2
3
import { Mesh } from "../mesh.js" ;
3
4
import { Renderer } from "./renderer.js" ;
@@ -37,6 +38,7 @@ export class KeypointRenderer extends Renderer {
37
38
}
38
39
39
40
const scene = new Scene ( ) ;
41
+ const videoCanvas = document . getElementById ( "jellron-video-canvas" ) ;
40
42
41
43
this . dispatchEvent ( new CustomEvent ( "started" ) ) ;
42
44
this . intervalId = setInterval ( async ( ) => {
@@ -110,6 +112,9 @@ export class KeypointRenderer extends Renderer {
110
112
111
113
this . glContext . render ( scene , this . getCamera ( ) ) ;
112
114
115
+ // We set this here because the KeypointRenderer runs at a lower framerate than VideoRenderer.
116
+ Detector . setCurrentFrame ( tf . browser . fromPixels ( videoCanvas ) ) ;
117
+
113
118
this . lastRuntime = performance . now ( ) - currentTime ;
114
119
this . dispatchEvent ( new CustomEvent ( "rendered" , { detail : { runtime : this . lastRuntime } } ) ) ;
115
120
} , 1000 / KeypointRenderer . fps ) ;
Original file line number Diff line number Diff line change @@ -52,6 +52,15 @@ export class Renderer extends RunnableInterval {
52
52
return camera ;
53
53
}
54
54
55
+ /**
56
+ * Retrieves the canvas to use for rendering.
57
+ *
58
+ * @returns {HTMLCanvasElement } Canvas to use for rendering.
59
+ */
60
+ getCanvas ( ) {
61
+ return this . canvas ;
62
+ }
63
+
55
64
/**
56
65
* Sets the canvas to use for rendering.
57
66
*
You can’t perform that action at this time.
0 commit comments