ReferenceError:在将图像传递到函数

时间:2018-05-15 07:52:00

标签: node.js tensorflow.js deeplearn.js

当程序读取.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:如果需要更多信息,请通知我。

感谢。

1 个答案:

答案 0 :(得分:0)

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

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