【问题标题】:load image and convert into tensor in tensorflow.js在 tensorflow.js 中加载图像并转换为张量
【发布时间】:2020-09-13 07:42:45
【问题描述】:

我最近在浏览器中使用tensorflow.js学习图像分类,现在我只是将一张图片转换为张量,但是我想加载很多图片,它不起作用,只有一张图片转换为张量,这是我只转换一张图片时的代码:

const im = new Image()
im.src = "/folder_name/1.jpg";
im.onload = () => {
const a = tf.fromPixels(im, 4)
a.print()
console.log(a.shape)
}

这是我尝试转换大量图像时的代码:

const im = new Image()
for(let i=1;i<= 15;i++){
    im.src = "folder_name/"+i+".jpg";
    im.onload[i] = () => {
    const a = tf.browser.fromPixels(im, 4)
    console.log(a.shape);
    }
    
}

当我打开控制台时,只有一个输出,就像我转换单个图像时一样

【问题讨论】:

    标签: javascript tensorflow.js


    【解决方案1】:

    用这段代码解决的问题

    for(let i=1;i<=15;i++){
    const im = new Image()
    im.src = "eyes/"+i+".jpg";
    im.onload = () => {
        const a = tf.browser.fromPixels(im, 4)
        a.print()
        console.log(a.shape)
    }
    }
    

    我可以加载我所有的图像并将其转换为张量

    【讨论】:

      猜你喜欢
      • 2019-04-13
      • 2018-10-29
      • 2018-12-14
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      相关资源
      最近更新 更多