question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: When running in node, pixels must be an HTMLCanvasElement like the one returned by the `canvas` npm package

See original GitHub issue

To get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

TensorFlow.js version

latest version in package.json

Browser version

node.js

Describe the problem or feature request

I get this error whe calling tf.browser.fromPixels

Code to reproduce the bug / link to feature request

const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
//...
img = tf.browser.fromPixels(img);

Full stacktrace:

(node:48448) UnhandledPromiseRejectionWarning: Error: When running in node, pixels must be an HTMLCanvasElement like the one returned by the `canvas` npm package
    at NodeJSKernelBackend.fromPixels (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-node/dist/nodejs_kernel_backend.js:1306:19)
    at Engine.fromPixels (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-core/dist/engine.js:498:29)
    at fromPixels_ (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-core/dist/ops/browser.js:83:37)
    at Object.fromPixels (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-core/dist/ops/operation.js:46:29)
    at /Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/lib/index.js:112:34
    at /Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-core/dist/engine.js:114:22
    at Engine.scopedRun (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-core/dist/engine.js:124:23)
    at Engine.tidy (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-core/dist/engine.js:113:21)
    at Object.Environment.tidy (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/node_modules/@tensorflow/tfjs-core/dist/environment.js:170:35)
    at NSFWJS.infer (/Users/loretoparisi/Documents/Projects/AI/nsfwjs-node/lib/index.js:110:19)
(node:48448) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:48448) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My package.json

{
  "name": "nsfw-node-test",
  "description": "test",
  "version": "1.1.3",
  "author": {
    "name": "Loreto Parisi",
    "email": "loretoparisi@gmail.com"
  },
  "main": "lib/index",
  "engines": {
    "node": "*"
  },
  "scripts": {},
  "devDependencies": {},
  "keywords": [],
  "dependencies": {
    "@tensorflow/tfjs-node": "latest",
    "node-fetch": "^2.2.0"
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

6reactions
nsthoratcommented, Mar 19, 2019

+1 we will prioritize it, it should be pretty simple!

2reactions
nsthoratcommented, Mar 19, 2019

I think the issue is you need to pass a canvas object to tf.browser.fromPixels, but you are passing an Image object. If you look at the node-canvas documentation you can draw the image to the canvas with:

const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
...
ctx.drawImage(image, 0, 0, width, height);
tf.browser.fromPixels(canvas).print();
Read more comments on GitHub >

github_iconTop Results From Across the Web

canvas - npm
node -canvas is a Cairo-backed Canvas implementation for Node.js. Installation. $ npm install canvas. By default, binaries for macOS, Linux and ...
Read more >
Object Detection (coco-ssd) Node.js: Error: pixels passed to tf ...
How do i have to load the image? When i do it like i do, i get an error: Error: pixels passed to...
Read more >
Writing Files with Node.js and JSDOM | by Danny Lee - Medium
Things like the wrong type of return value from one function, ... I still need to install the NPM package/module canvas (npm) to...
Read more >
How to predict image by url (images on s3 bucket) using ...
Error : When running in node, pixels must be an HTMLCanvasElement like the one returned by the `canvas` npm package. So Im using...
Read more >
Examples
Remember though that PDF.js uses promises, and the above will return a ... Alternatively, if you want the canvas to render to a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found