【问题标题】:convert base64 image to tensor将base64图像转换为张量
【发布时间】:2020-08-28 16:28:05
【问题描述】:

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

【问题讨论】:

    标签: node.js tensorflow image-processing object-detection tensorflow.js


    【解决方案1】:
    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;
    }
    

    【讨论】:

      【解决方案2】:

      服务器端 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)
         })()
      

      【讨论】:

      • 在服务器端 node.js tf.tensor(b, shape) 值 shape 应该有什么?
      • 第二种解决方案我收到错误,因为 model.execute(dict) 中提供的 dict['image_tensor'] 的形状必须是 [-1,-1,-1,3],但是是 [1,629051]。 @edkeveked 我应该如何将形状赋予张量。
      • 形状应该是[height, width, channels]。您需要知道图像的宽度和高度。您可以添加一个批次维度(最左边的位置),这将使形状为[b, height, width, channels]。使用 tf.fromPixels 时的区别,它直接返回一个 3d 或 4d 张量
      • { base64: '/9j/4AAQSkZJRgABAQAASABIAA........', width: 722, pictureOrientation: 1, deviceOrientation: 1, height: 1280 } 它我的 base64 图像分辨率。我在服务器中使用的代码是 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
      • @Jayaprakash 你找到解决方案了吗?
      猜你喜欢
      • 2018-12-14
      • 2015-12-09
      • 1970-01-01
      • 2014-04-06
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多