Skip to content

Commit 48186f8

Browse files
committed
Use resizeImageAsTensor for size mismatch bug
Replace the code fixing the size mismatch bug by using the new function designed for that: resizeImageAsTensor.
1 parent 954c730 commit 48186f8

File tree

1 file changed

+15
-40
lines changed

1 file changed

+15
-40
lines changed

src/DepthEstimation/index.js

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import handleArguments from "../utils/handleArguments";
1616
import { mediaReady } from "../utils/imageUtilities";
1717
import handleOptions from "../utils/handleOptions";
1818
import { handleModelName } from "../utils/handleOptions";
19+
import { resizeImageAsTensor } from "../utils/imageUtilities";
1920

2021
/**
2122
* @typedef {'COLOR' | 'GRAYSCALE'} ColormapName
@@ -286,28 +287,13 @@ class DepthEstimation {
286287

287288
if (image instanceof HTMLElement) {
288289
/*
289-
* If the input is an HTML element, these models ignore the element's "width" and "height"
290-
* attributes, opting instead for the intrinsic dimensions of the video. This causes a mismatch
291-
* between what the user expects and what the model returns. Here we turn it to a tensor and resize it
292-
* to match the element's .width and .height; the width and height attributes set by the user.
293-
*
290+
* If the input is an HTML element, turn it to a tensor and resize it to make sure it matches
291+
* the element's .width and .height: the size set by the user in p5.js.
294292
*/
295-
const { resized, normalized } = tf.tidy(() => {
296-
const sourcePixelsTensor = tf.browser.fromPixels(image);
297-
const resized = tf.image.resizeBilinear(sourcePixelsTensor, [
298-
image.height,
299-
image.width,
300-
]);
301-
const normalized = resized.clipByValue(0, 255).div(255.0); // Clip and normalize for use in drawMask()
302-
303-
return {
304-
resized: resized.clone(), // Clone to keep outside tidy
305-
normalized: normalized.clone(), // Clone to keep outside tidy
306-
};
293+
resizedSource = resizeImageAsTensor(image, width, height);
294+
normalizedSource = tf.tidy(() => {
295+
return resizedSource.div(255.0);
307296
});
308-
309-
resizedSource = resized;
310-
normalizedSource = normalized;
311297
inputForDepth = resizedSource;
312298
}
313299

@@ -649,28 +635,17 @@ class DepthEstimation {
649635

650636
if (this.detectMedia instanceof HTMLElement) {
651637
/*
652-
* If the input is an HTML element, these models ignore the element's "width" and "height"
653-
* attributes, opting instead for the intrinsic dimensions of the video. This causes a mismatch
654-
* between what the user expects and what the model returns. Here we turn it to a tensor and resize it
655-
* to match the element's .width and .height; the width and height attributes set by the user.
656-
*
638+
* If the input is an HTML element, turn it to a tensor and resize it to make sure it matches
639+
* the element's .width and .height: the size set by the user in p5.js.
657640
*/
658-
const { resized, normalized } = tf.tidy(() => {
659-
const sourcePixelsTensor = tf.browser.fromPixels(this.detectMedia);
660-
const resized = tf.image.resizeBilinear(sourcePixelsTensor, [
661-
this.detectMedia.height,
662-
this.detectMedia.width,
663-
]);
664-
const normalized = resized.clipByValue(0, 255).div(255.0); // Clip and normalize for use in drawMask()
665-
666-
return {
667-
resized: resized.clone(), // Clone to keep outside tidy
668-
normalized: normalized.clone(), // Clone to keep outside tidy
669-
};
641+
resizedSource = resizeImageAsTensor(
642+
this.detectMedia,
643+
this.detectMedia.width,
644+
this.detectMedia.height
645+
);
646+
normalizedSource = tf.tidy(() => {
647+
return resizedSource.div(255.0);
670648
});
671-
672-
resizedSource = resized;
673-
normalizedSource = normalized;
674649
inputForDepth = resizedSource;
675650
}
676651

0 commit comments

Comments
 (0)