【问题标题】:Object Detection (coco-ssd) Node.js: Error: pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement对象检测(coco-ssd)Node.js:错误:传递给 tf.browser.fromPixels() 的像素必须是 HTMLVideoElement
【发布时间】:2021-05-27 06:34:06
【问题描述】:

我在我的 iobroker 上使用 node.js tensorflow-models/coco-ssd'。我如何加载图像?

当我这样做时,我得到一个错误: 错误:传递给 tf.browser.fromPixels() 的像素必须是 HTMLVideoElement、HTMLImageElement、HTMLCanvasElement、浏览器中的 ImageData 或 OffscreenCanvas,

这是我的代码:

const cocoSsd = require('@tensorflow-models/coco-ssd');
init();
function init() {
    (async () => {

        // Load the model.
        const model = await cocoSsd.load();
        
        // Classify the image.
        var image = fs.readFileSync('/home/iobroker/12-14-2020-tout.jpg');
    
        // Classify the image.
        const predictions = await model.detect(image);
    
        console.log('Predictions: ');
        console.log(predictions);

    })();
}

【问题讨论】:

    标签: javascript node.js tensorflow coco


    【解决方案1】:

    您在这种情况下看到的错误消息是准确的。

    首先,在这一部分中,您将使用文件字符串/缓冲区实例初始化image

            // Classify the image.
            var image = fs.readFileSync('/home/iobroker/12-14-2020-tout.jpg');
    

    然后,您将其传递给model.detect()

            // Classify the image.
            const predictions = await model.detect(image);
    

    问题在于 model.detect() 实际上需要一个 HTML 图像/视频/画布元素。根据@tensorflow-models/coco-ssd 对象检测文档:

    它可以将输入作为任何基于浏览器的图像元素(例如<img><video><canvas> 元素)并返回具有类名和置信度的边界框数组。

    如同一文档所述,它不适用于 Node 服务器环境:

    注意:以下是如何使用 coco-ssd npm 进行 web 部署的转译,而不是如何在 node env 中使用 coco-ssd 的示例。

    不过,您可以按照like the ones of this guide 的步骤操作,该步骤展示了如何实现在 Node 服务器上运行它的目标。

    下面的例子:

     const cocoSsd = require('@tensorflow-models/coco-ssd');
     const tf = require('@tensorflow/tfjs-node');
     const fs = require('fs').promises;
    
     // Load the Coco SSD model and image.
     Promise.all([cocoSsd.load(), fs.readFile('/home/iobroker/12-14-2020-tout.jpg')])
     .then((results) => {
       // First result is the COCO-SSD model object.
       const model = results[0];
       // Second result is image buffer.
       const imgTensor = tf.node.decodeImage(new Uint8Array(results[1]), 3);
       // Call detect() to run inference.
       return model.detect(imgTensor);
     })
     .then((predictions) => {
       console.log(JSON.stringify(predictions, null, 2));
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 2017-08-31
      相关资源
      最近更新 更多