【发布时间】:2020-08-28 16:28:05
【问题描述】:
我将图像作为 base64 字符串发送到 node express 服务器以使用 tensorflow 分析对象检测。如何在 node js 中使用 cocossd 模型将 base64 图像更改为张量进行对象检测。
【问题讨论】:
标签: node.js tensorflow image-processing object-detection tensorflow.js
我将图像作为 base64 字符串发送到 node express 服务器以使用 tensorflow 分析对象检测。如何在 node js 中使用 cocossd 模型将 base64 图像更改为张量进行对象检测。
【问题讨论】:
标签: node.js tensorflow image-processing object-detection tensorflow.js
const readImage = path => {
//reads the entire contents of a file.
//readFileSync() is synchronous and blocks execution until finished.
const imageBuffer = fs.readFileSync(path);
//Given the encoded bytes of an image,
//it returns a 3D or 4D tensor of the decoded image. Supports BMP, GIF, JPEG and PNG formats.
const tfimage = tfnode.node.decodeImage(imageBuffer);
return tfimage;
}
【讨论】:
服务器端 NodeJs
base64 字符串可以转换为二进制,然后使用tf.node 读取为张量
const b = Buffer.from(base64str, 'base64')
// get the tensor
const t = tf.node.decodeImage(b)
如果其他属性/值没有随请求一起发送,最好在 post 请求或 websocket 中直接将图像作为二进制发送。在这种情况下,就不需要从 base64 服务器端重做转换
浏览器端
const b = atob(base64str)
let byteNumbers = new Array(b.length);
for (let i = 0; i < b.length; i++) {
byteNumbers[i] = b.charCodeAt(i);
}
let tensor = tf.tensor(byteNumbers)
第一个选项是同步的。对于大图像,它可能会冻结主线程。为了缓解这种情况,可以在 web-worker 中完成此操作。
另一种选择是创建一个图像元素并将其 href 属性设置为base64str,然后使用tf.browser.fromPixels
function load(url){
return new Promise((resolve, reject) => {
const im = new Image()
im.crossOrigin = 'anonymous'
im.src = 'url'
im.onload = () => {
resolve(im)
}
})
}
// use the load function inside an async function
(async() => {
const image = await load(url)
let tensor = await tf.browser.fromPixels(image)
})()
【讨论】:
[height, width, channels]。您需要知道图像的宽度和高度。您可以添加一个批次维度(最左边的位置),这将使形状为[b, height, width, channels]。使用 tf.fromPixels 时的区别,它直接返回一个 3d 或 4d 张量
let buff = Buffer.from(data.photo.base64,'base64') let tensor = tf.tensor(buff,[data.photo.height,data.photo.width,3]) 但我在获取张量时出错 根据提供的形状 [1280,722,3],张量应该有 2772480 个值但有 610963