给定URL,如何加载图像,然后将其转换为Javascript中的tf.tensor数据? [英] Given the url, how to load an image and then transform it into a tf.tensor data in Javascript?

查看:34
本文介绍了给定URL,如何加载图像,然后将其转换为Javascript中的tf.tensor数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是前端和javascript的新手.现在,我正在编写一个基于CNN的Web应用程序.要从给定的URL加载图像,然后将其输入到CNN中,我尝试编写以下代码:

I am new to frontend and javascript. Now I am writing a CNN-based web application. To load an image from the given url, and then feed the image into a CNN, I tried to write the following codes:

async function getImage() {
  var img = new Image();
  img.src = 'https://www.google.com/favicon.ico';
  var output = tf.browser.fromPixels(img);
  return output;
}

不幸的是,它没有用;(

Unfortunately it didn't work ;(

如何加载图像并将其转换为tf.tensor?有人可以帮助我实现我的目标吗?

How do I load the image and transform it into a tf.tensor? Could somebody help me achieve my goal?

推荐答案

在图像加载完成后必须创建张量

The tensor has to be created once the image has completed to be loaded

async function getImage() {
  var img = new Image();
  img.src = 'https://www.google.com/favicon.ico';

  img.onload = () => {
  var output = tf.browser.fromPixels(img);
  return output;
  }

}

这篇关于给定URL,如何加载图像,然后将其转换为Javascript中的tf.tensor数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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