@@ -16,6 +16,7 @@ import handleArguments from "../utils/handleArguments";
16
16
import { mediaReady } from "../utils/imageUtilities" ;
17
17
import handleOptions from "../utils/handleOptions" ;
18
18
import { handleModelName } from "../utils/handleOptions" ;
19
+ import { resizeImageAsTensor } from "../utils/imageUtilities" ;
19
20
20
21
/**
21
22
* @typedef {'COLOR' | 'GRAYSCALE' } ColormapName
@@ -286,28 +287,13 @@ class DepthEstimation {
286
287
287
288
if ( image instanceof HTMLElement ) {
288
289
/*
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.
294
292
*/
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 ) ;
307
296
} ) ;
308
-
309
- resizedSource = resized ;
310
- normalizedSource = normalized ;
311
297
inputForDepth = resizedSource ;
312
298
}
313
299
@@ -649,28 +635,17 @@ class DepthEstimation {
649
635
650
636
if ( this . detectMedia instanceof HTMLElement ) {
651
637
/*
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.
657
640
*/
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 ) ;
670
648
} ) ;
671
-
672
- resizedSource = resized ;
673
- normalizedSource = normalized ;
674
649
inputForDepth = resizedSource ;
675
650
}
676
651
0 commit comments