Skip to content

Commit 9cd0cd3

Browse files
committed
fix cideo in posenet
1 parent 67efcbc commit 9cd0cd3

File tree

2 files changed

+72
-61
lines changed

2 files changed

+72
-61
lines changed

src/FeatureExtractor/index.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,47 @@
77
A General Feature Extractor class
88
*/
99

10-
const DEFAULTS = {
11-
mobilenet: {
12-
version: 1,
13-
alpha: 1.0,
14-
topk: 3,
15-
learningRate: 0.0001,
16-
hiddenUnits: 100,
17-
epochs: 20,
18-
numClasses: 2,
19-
batchSize: 0.4,
20-
},
21-
};
10+
// const DEFAULTS = {
11+
// mobilenet: {
12+
// version: 1,
13+
// alpha: 1.0,
14+
// topk: 3,
15+
// learningRate: 0.0001,
16+
// hiddenUnits: 100,
17+
// epochs: 20,
18+
// numClasses: 2,
19+
// batchSize: 0.4,
20+
// },
21+
// };
2222

2323
class FeatureExtractor {
24-
constructor(model, inputOrOptionsOrCallback, optionsOrCallback = {}, cb = null) {
25-
if (typeof model === 'string') {
26-
this.modelName = model;
27-
} else {
28-
console.error('Please specify a model to use. E.g: "Mobilenet"');
29-
}
24+
// constructor(model, inputOrOptionsOrCallback, optionsOrCallback = {}, cb = null) {
25+
// if (typeof model === 'string') {
26+
// this.modelName = model;
27+
// } else {
28+
// console.error('Please specify a model to use. E.g: "Mobilenet"');
29+
// }
3030

31-
this.options = {};
32-
let callback = cb;
33-
// TODO: handle sound inputs
34-
if (inputOrOptionsOrCallback instanceof HTMLVideoElement) {
35-
this.input = inputOrOptionsOrCallback;
36-
} else if (typeof inputOrOptionsOrCallback === 'object' && inputOrOptionsOrCallback.elt instanceof HTMLVideoElement) {
37-
this.input = inputOrOptionsOrCallback.elt; // Handle p5.js video
38-
} else if (inputOrOptionsOrCallback === 'object') {
39-
this.options = inputOrOptionsOrCallback;
40-
} else if (typeof inputOrOptionsOrCallback === 'function') {
41-
callback = inputOrOptionsOrCallback;
42-
}
31+
// this.options = {};
32+
// let callback = cb;
33+
// // TODO: handle sound inputs
34+
// if (inputOrOptionsOrCallback instanceof HTMLVideoElement) {
35+
// this.input = inputOrOptionsOrCallback;
36+
// } else if (typeof inputOrOptionsOrCallback === 'object'
37+
// && inputOrOptionsOrCallback.elt instanceof HTMLVideoElement) {
38+
// this.input = inputOrOptionsOrCallback.elt; // Handle p5.js video
39+
// } else if (inputOrOptionsOrCallback === 'object') {
40+
// this.options = inputOrOptionsOrCallback;
41+
// } else if (typeof inputOrOptionsOrCallback === 'function') {
42+
// callback = inputOrOptionsOrCallback;
43+
// }
4344

44-
if (optionsOrCallback === 'object') {
45-
this.options = optionsOrCallback;
46-
} else if (typeof optionsOrCallback === 'function') {
47-
callback = optionsOrCallback;
48-
}
49-
}
45+
// if (optionsOrCallback === 'object') {
46+
// this.options = optionsOrCallback;
47+
// } else if (typeof optionsOrCallback === 'function') {
48+
// callback = optionsOrCallback;
49+
// }
50+
// }
5051
}
5152

5253
export default FeatureExtractor;

src/PoseNet/index.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,60 @@ PoseNet
1010
import * as tf from '@tensorflow/tfjs';
1111
import * as posenet from '@tensorflow-models/posenet';
1212

13-
import ImageAndVideo from './../ImageAndVideo';
14-
1513
const DEFAULTS = {
16-
imageScaleFactor: 0.5,
14+
imageScaleFactor: 0.3,
1715
outputStride: 16,
1816
flipHorizontal: false,
1917
minConfidence: 0.5,
2018
maxPoseDetections: 5,
2119
scoreThreshold: 0.5,
2220
nmsRadius: 20,
2321
detectionType: 'single',
22+
multiplier: 1.01,
2423
};
2524

26-
class PoseNet extends ImageAndVideo {
27-
constructor(videoOrCallback, optionsOrCallback, cb = () => {}) {
28-
super(videoOrCallback, 315);
25+
class PoseNet {
26+
constructor(videoOrOptionsOrCallback, optionsOrCallback, cb = () => {}) {
2927
let options = {};
3028
let callback = cb;
31-
this.detectionType = DEFAULTS.detectionType;
29+
let detectionType = null;
30+
31+
if (videoOrOptionsOrCallback instanceof HTMLVideoElement) {
32+
this.video = videoOrOptionsOrCallback;
33+
} else if (typeof videoOrOptionsOrCallback === 'object' && videoOrOptionsOrCallback.elt instanceof HTMLVideoElement) {
34+
this.video = videoOrOptionsOrCallback.elt; // Handle a p5.js video element
35+
} else if (typeof videoOrOptionsOrCallback === 'object') {
36+
options = videoOrOptionsOrCallback;
37+
} else if (typeof videoOrOptionsOrCallback === 'function') {
38+
callback = videoOrOptionsOrCallback;
39+
}
3240

33-
if (typeof videoOrCallback === 'function') {
34-
callback = videoOrCallback;
35-
} else if (typeof optionsOrCallback === 'object') {
41+
if (typeof optionsOrCallback === 'object') {
3642
options = optionsOrCallback;
37-
this.detectionType = options.detectionType || DEFAULTS.detectionType;
3843
} else if (typeof optionsOrCallback === 'function') {
3944
callback = optionsOrCallback;
40-
} else if (optionsOrCallback === 'multiple') {
41-
this.detectionType = optionsOrCallback;
45+
} else if (typeof optionsOrCallback === 'string') {
46+
detectionType = optionsOrCallback;
4247
}
4348

49+
this.detectionType = detectionType || DEFAULTS.detectionType;
4450
this.imageScaleFactor = options.imageScaleFactor || DEFAULTS.imageScaleFactor;
4551
this.outputStride = options.outputStride || DEFAULTS.outputStride;
4652
this.flipHorizontal = options.flipHorizontal || DEFAULTS.flipHorizontal;
4753
this.minConfidence = options.minConfidence || DEFAULTS.minConfidence;
54+
this.multiplier = options.multiplier || DEFAULTS.multiplier;
4855

49-
// TODO: Specify model
50-
posenet.load()
56+
posenet.load(this.multiplier)
5157
.then((net) => {
5258
this.net = net;
53-
if (this.video && callback) {
54-
if (this.detectionType === 'single') {
55-
this.singlePose(callback);
56-
} else if (this.detectionType === 'multiple') {
57-
this.multiPose(callback);
58-
}
59+
if (this.video) {
60+
(this.video.onplay = () => {
61+
if (this.detectionType === 'single') {
62+
this.singlePose(callback);
63+
} else if (this.detectionType === 'multiple') {
64+
this.multiPose(callback);
65+
}
66+
})();
5967
}
6068
})
6169
.catch((err) => { console.error(`Error loading the model: ${err}`); });
@@ -68,11 +76,12 @@ class PoseNet extends ImageAndVideo {
6876
/* eslint max-len: ["error", { "code": 180 }] */
6977
singlePose(inputOrCallback, cb = () => {}) {
7078
let input;
71-
let callback;
79+
let callback = cb;
7280

7381
if (inputOrCallback instanceof HTMLImageElement || inputOrCallback instanceof HTMLVideoElement) {
7482
input = inputOrCallback;
75-
callback = cb;
83+
} else if (typeof inputOrCallback === 'object' && (inputOrCallback.elt instanceof HTMLImageElement || inputOrCallback.elt instanceof HTMLVideoElement)) {
84+
input = inputOrCallback.elt; // Handle p5.js image and video
7685
} else if (typeof inputOrCallback === 'function' && this.video) {
7786
input = this.video;
7887
callback = inputOrCallback;
@@ -87,11 +96,12 @@ class PoseNet extends ImageAndVideo {
8796

8897
multiPose(inputOrCallback, cb = () => {}) {
8998
let input;
90-
let callback;
99+
let callback = cb;
91100

92101
if (inputOrCallback instanceof HTMLImageElement || inputOrCallback instanceof HTMLVideoElement) {
93102
input = inputOrCallback;
94-
callback = cb;
103+
} else if (typeof inputOrCallback === 'object' && (inputOrCallback.elt instanceof HTMLImageElement || inputOrCallback.elt instanceof HTMLVideoElement)) {
104+
input = inputOrCallback.elt; // Handle p5.js image and video
95105
} else if (typeof inputOrCallback === 'function' && this.video) {
96106
input = this.video;
97107
callback = inputOrCallback;

0 commit comments

Comments
 (0)