From 5031b9ee068ea2d70d1bdbc5d6b3dc307da838bf Mon Sep 17 00:00:00 2001 From: Nikhil Thorat Date: Fri, 30 Aug 2019 10:28:13 -0400 Subject: [PATCH 01/13] Only throw an error about dom being ready for isVideo. --- tfjs-core/src/backends/webgl/backend_webgl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tfjs-core/src/backends/webgl/backend_webgl.ts b/tfjs-core/src/backends/webgl/backend_webgl.ts index 9a35faf134f..d06041ae3a4 100644 --- a/tfjs-core/src/backends/webgl/backend_webgl.ts +++ b/tfjs-core/src/backends/webgl/backend_webgl.ts @@ -312,7 +312,7 @@ export class MathBackendWebGL implements KernelBackend { if (isImage || isVideo) { if (this.fromPixels2DContext == null) { - if (document.readyState !== 'complete') { + if (isVideo && document.readyState !== 'complete') { throw new Error( 'The DOM is not ready yet. Please call ' + 'tf.browser.fromPixels() once the DOM is ready. One way to ' + From 7a830c5bc01091c624396980c0b3172ad52bdba9 Mon Sep 17 00:00:00 2001 From: Nikhil Thorat Date: Fri, 30 Aug 2019 11:01:59 -0400 Subject: [PATCH 02/13] save --- tfjs-core/src/backends/webgl/backend_webgl.ts | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tfjs-core/src/backends/webgl/backend_webgl.ts b/tfjs-core/src/backends/webgl/backend_webgl.ts index d06041ae3a4..e2d707eef3d 100644 --- a/tfjs-core/src/backends/webgl/backend_webgl.ts +++ b/tfjs-core/src/backends/webgl/backend_webgl.ts @@ -286,8 +286,6 @@ export class MathBackendWebGL implements KernelBackend { throw new Error( 'pixels passed to tf.browser.fromPixels() can not be null'); } - const texShape: [number, number] = [pixels.height, pixels.width]; - const outShape = [pixels.height, pixels.width, numChannels]; const isCanvas = (typeof (OffscreenCanvas) !== 'undefined' && pixels instanceof OffscreenCanvas) || @@ -300,6 +298,15 @@ export class MathBackendWebGL implements KernelBackend { pixels instanceof HTMLVideoElement; const isImage = typeof (HTMLImageElement) !== 'undefined' && pixels instanceof HTMLImageElement; + const [width, height] = isVideo ? + [ + (pixels as HTMLVideoElement).videoWidth, + (pixels as HTMLVideoElement).videoHeight + ] : + [pixels.width, pixels.height]; + + const texShape: [number, number] = [height, width]; + const outShape = [height, width, numChannels]; if (!isCanvas && !isPixelData && !isImageData && !isVideo && !isImage) { throw new Error( @@ -312,21 +319,23 @@ export class MathBackendWebGL implements KernelBackend { if (isImage || isVideo) { if (this.fromPixels2DContext == null) { - if (isVideo && document.readyState !== 'complete') { + const HAVE_CURRENT_DATA_READY_STATE = 2; + if (isVideo && + (pixels as HTMLVideoElement).readyState < + HAVE_CURRENT_DATA_READY_STATE) { throw new Error( - 'The DOM is not ready yet. Please call ' + - 'tf.browser.fromPixels() once the DOM is ready. One way to ' + - 'do that is to add an event listener for `load` ' + - 'on the document object'); + 'The video element has not loaded data yet. Please wait for ' + + '`loadeddata` event on the