Skip to content

Commit 76366a3

Browse files
danidaskcvalenzuela
authored andcommitted
fix(addImage, classify, predict): now featureExtractor works with images (it was working only with video) (#191)
* fix(addImage, classify, predict): now featureExtractor works with images (it was working only with v * fix(addImage, classify, predict): now featureExtractor works with images (it was working only with video
1 parent c73d1aa commit 76366a3

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/FeatureExtractor/Mobilenet.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Mobilenet {
9595
if (inputOrLabel instanceof HTMLImageElement || inputOrLabel instanceof HTMLVideoElement) {
9696
imgToAdd = inputOrLabel;
9797
} else if (typeof inputOrLabel === 'object' && (inputOrLabel.elt instanceof HTMLImageElement || inputOrLabel.elt instanceof HTMLVideoElement)) {
98-
imgToAdd = inputOrLabel;
98+
imgToAdd = inputOrLabel.elt;
9999
} else if (typeof inputOrLabel === 'string' || typeof inputOrLabel === 'number') {
100100
imgToAdd = this.video;
101101
label = inputOrLabel;
@@ -121,7 +121,8 @@ class Mobilenet {
121121
async addImageInternal(imgToAdd, label) {
122122
await this.ready;
123123
tf.tidy(() => {
124-
const processedImg = imgToTensor(imgToAdd);
124+
const imageResize = (imgToAdd === this.video) ? null : [IMAGESIZE, IMAGESIZE];
125+
const processedImg = imgToTensor(imgToAdd, imageResize);
125126
const prediction = this.mobilenetFeatures.predict(processedImg);
126127

127128
let y;
@@ -243,7 +244,8 @@ class Mobilenet {
243244
await tf.nextFrame();
244245
this.isPredicting = true;
245246
const predictedClass = tf.tidy(() => {
246-
const processedImg = imgToTensor(imgToPredict);
247+
const imageResize = (imgToPredict === this.video) ? null : [IMAGESIZE, IMAGESIZE];
248+
const processedImg = imgToTensor(imgToPredict, imageResize);
247249
const activation = this.mobilenetFeatures.predict(processedImg);
248250
const predictions = this.customModel.predict(activation);
249251
return predictions.as1D().argMax();
@@ -281,7 +283,8 @@ class Mobilenet {
281283
await tf.nextFrame();
282284
this.isPredicting = true;
283285
const predictedClass = tf.tidy(() => {
284-
const processedImg = imgToTensor(imgToPredict);
286+
const imageResize = (imgToPredict === this.video) ? null : [IMAGESIZE, IMAGESIZE];
287+
const processedImg = imgToTensor(imgToPredict, imageResize);
285288
const activation = this.mobilenetFeatures.predict(processedImg);
286289
const predictions = this.customModel.predict(activation);
287290
return predictions.as1D();

0 commit comments

Comments
 (0)