ShuuRi
ShuuRi

Reputation: 31

ReferenceError: ImageData is not defined at MathBackendCPU.fromPixels when pass image into the function

I got this error when my program read the .png image file and used the data as the parameter for the fromPixels() function in tensorflow.js. Why it said ImageData is not defined? Am I miss out any dependencies?

/home/R/Desktop/process_img/node_modules/deeplearn/dist/kernels/backend_cpu.js:85
        if (pixels instanceof ImageData) {
                              ^

ReferenceError: ImageData is not defined
    at MathBackendCPU.fromPixels (/home/R/Desktop/process_img/node_modules/deeplearn/dist/kernels/backend_cpu.js:85:31)
    at Engine.fromPixels (/home/R/Desktop/process_img/node_modules/deeplearn/dist/engine.js:287:29)
    at Ops.fromPixels (/home/R/Desktop/process_img/node_modules/deeplearn/dist/ops/array_ops.js:184:41)
    at (/home/R/Desktop/process_img/node_modules/deeplearn/dist/ops/operation.js:11:61
    at Object.Tracking.tidy (/home/R/Desktop/process_img/node_modules/deeplearn/dist/tracking.js:34:22)
    at Object.descriptor.value [as fromPixels] (/home/R/Desktop/process_img/node_modules/deeplearn/dist/ops/operation.js:11:26)
    at /home/R/Desktop/process_img/index.js:29:29
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:447:3)

Here is the code:

#!/usr/bin/node
"use strict";

const {KNNImageClassifier} = require('deeplearn-knn-image-classifier');
const _deeplearn = require('deeplearn');

const child = require('child_process');
const fs = require('fs');

const { Image } = require('canvas');

const NUM_CLASSES = 3;
const IMAGE_SIZE = 227; // Must be 227
const TOPK = 10;

var filename = 'images.png';
//console.log(img.dtype);

var knn = new KNNImageClassifier(NUM_CLASSES, TOPK);
fs.readFile(filename, function(err, squid){
    if (err) throw err;
    var img = new Image();
    img.src = squid;
    const img1 = _deeplearn.fromPixels(img);
  });

p/s: Please inform me if need more information.

Thanks.

Upvotes: 1

Views: 2915

Answers (1)

ShuuRi
ShuuRi

Reputation: 31

Currently tf.fromPixels is not supported in Node. Need to manually load the image into a Tensor.

https://groups.google.com/a/tensorflow.org/forum/#!topic/tfjs/JE8KlPwOz4g

Upvotes: 1

Related Questions