将base64图像转换为张量 [英] convert base64 image to tensor

查看:449
本文介绍了将base64图像转换为张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像作为base64字符串发送到节点表达服务器,以使用tensorflow分析对象检测.如何在节点js中使用cocossd模型将base64图像更改为张量以进行对象检测.

I'm sending the image as base64 string to the node express server to analysis the object detection with tensorflow. How to change the base64 image as tensors for object detection with cocossd model in node js.

推荐答案

服务器端NodeJ

base64字符串可以转换为二进制,然后使用tf.node

The base64 string can be converted to binary and then be read as tensor using tf.node

 const b = Buffer.from(base64str, 'base64')
    // get the tensor
 const t = tf.node.decodeImage(b)

如果未随请求一起发送其他属性/值,则最好在发布请求或Websocket中以二进制形式直接发送图像.在这种情况下,无需从base64服务器端重做转换

If other properties/values are not sent along the request, it would be best to send directly the image as binary in a post request or in a websocket. In that case, there would be no need to redo the conversion from base64 server side

浏览器端

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)

第一个选项是同步的.对于大图像,它可能会冻结主线程.为了减轻这种情况,可以在网络工作者中完成此操作.

This first option is synchronous. For big image it can possibly freeze the main thread. To alleviate that, this operation can be done in a web-worker.

另一种选择是创建图像元素并将其href属性设置为base64str,然后使用tf.browser.fromPixels

The other option would be to create an image element and set it href attribute to the base64str and then use 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)
   })()

这篇关于将base64图像转换为张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆