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

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

当我的程序读取 .png 图像文件并将数据用作 tensorflow.js 中的 fromPixels() 函数的参数时出现此错误。 为什么它说 ImageData 未定义?我是否遗漏了任何依赖项?

/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)

代码如下:

#!/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: 如果需要更多信息,请通知我。

谢谢。

目前 tf.fromPixels 在 Node.js 中不受支持。需要手动将图像加载到 Tensor 中。

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