加载图像并转换为 tensorflow.js 中的张量
load image and convert into tensor in tensorflow.js
我最近在用tensorflow.js学习浏览器的图片分类,现在只是一张图片转tensor,但是我想加载很多图片,不行,只有一张图片转换为张量,这是我只转换一张图片时的代码:
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);
}
}
当我打开控制台时,只有一个输出,就像我转换单个图像时一样
问题用这段代码解决
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)
}
}
我可以加载我所有的图像并将其转换为张量
我最近在用tensorflow.js学习浏览器的图片分类,现在只是一张图片转tensor,但是我想加载很多图片,不行,只有一张图片转换为张量,这是我只转换一张图片时的代码:
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);
}
}
当我打开控制台时,只有一个输出,就像我转换单个图像时一样
问题用这段代码解决
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)
}
}
我可以加载我所有的图像并将其转换为张量