I am getting this error in the console using tensorflow.js

Uncaught (in promise) Error: pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData in browser, or OffscreenCanvas, ImageData in webworker or {data: Uint32Array, width: number, height: number}, but was HTMLVideoElement

I am just passing a video html element after it's loaded so what does it mean?

My js code code:

const input_tensor = tf.browser.fromPixels(document.getElementById("video"));

My html tag:

<video id="video" autoplay muted></video>

I finally managed to get it to work by opening a issue in the tfjs repo

I copy here the solution provided by the github user tafsiri

After digging around a bit, it looks like your project is running in separate context mode and there are two JS contexts in which code is run. Your code is being run in the browser context and the tfjs library code is being run in the node context. In the node context video instanceof HTMLVideoElement returns false. The nw.js docs suggest that browser api's aren't usable in the node context so there may be incompatibility there.

Their docs suggest you can switch to mixed context mode, I tried that by adding the following entry to the package.json and then it worked.

"chromium-args": "--mixed-context", If you want to stay in separate context mode (and I don't know enough about nw.js to know the advantages and disadvantages of each) you would need to not use any of the browser specific apis of tfjs.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.