有没有办法用require替换脚本标签src并在节点上运行相同的脚本?

时间:2020-05-14 17:56:05

标签: node.js tensorflow require google-cloud-automl script-tag

我正在浏览器中运行以下程序:

INDEX.HTML(BODY)

<script src="https://unpkg.com/@tensorflow/tfjs"></script>
<script src="https://unpkg.com/@tensorflow/tfjs-automl"></script>
<img
  id="daisy"
  crossorigin="anonymous"
  src="https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg"
/>
<script>
  async function run() {
    const model = await tf.automl.loadImageClassification("model.json");
    const image = document.getElementById("daisy");
    const predictions = await model.classify(image);

    const pre = document.createElement("pre");
    pre.textContent = JSON.stringify(predictions, null, 2);
    document.body.append(pre);
  }

  run();
</script>

我想要做的就是将脚本转换为可以在节点js中运行的脚本,如下所示:

INDEX.JS(IMPORT / ESM)

import * as tf from "@tensorflow/tfjs";
import * as automl from "@tensorflow/tfjs-automl";

async function run() {
  const model = await tf.automl.loadImageClassification("model.json");
  const image = document.createElement("img");
  image.src =
    "https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg";
  const predictions = await model.classify(image);

  console.log(predictions);
}

run();

然后我用node --experimental-modules index.js运行脚本,但失败:

(node:24163) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'loadImageClassification' of undefined

我也尝试过require

INDEX.JS(要求/与CONST共同使用)

const tf = require("@tensorflow/tfjs");
const automl = require("@tensorflow/tfjs-automl");

async function run() {
  const model = await tf.automl.loadImageClassification("model.json");
  const image = document.createElement("img");
  image.src =
    "https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg";
  const predictions = await model.classify(image);

  console.log(predictions);
}

run();

我不得不从"type": "module"中删除package.json并使用node index index.js运行。它给出了相同的错误。

我还尝试了不捕获require

INDEX.JS(要求/共用)

require("@tensorflow/tfjs");
require("@tensorflow/tfjs-automl");

async function run() {
  const model = await tf.automl.loadImageClassification("model.json");
  const image = document.createElement("img");
  image.src =
    "https://storage.googleapis.com/tfjs-testing/tfjs-automl/img_classification/daisy.jpg";
  const predictions = await model.classify(image);

  console.log(predictions);
}

run();

运行此命令时,出现错误:(node:24211) UnhandledPromiseRejectionWarning: ReferenceError: tf is not defined

这似乎很明显,但是有一种方法可以执行<script src=的操作,但是在节点中,即引入外部脚本,以便我的脚本可以查看和使用外部的变量/方法。脚本?

2 个答案:

答案 0 :(得分:1)

对于任何想在节点上运行张量流预测的人:

const tf = require("@tensorflow/tfjs-node");
const automl = require("@tensorflow/tfjs-automl");
const fs = require("fs");

const model_url = "<your-model-url>";
const image_path = process.argv.slice(2)[0];

if (!image_path) {
  throw new Error("missing argument: path to image");
}

const image = fs.readFileSync(image_path);
const decoded_image = tf.node.decodeJpeg(image);

async function run() {
  const model = await automl.loadImageClassification(model_url);
  const predictions = await model.classify(decoded_image);

  console.log(predictions);
}

run().catch(console.error);

答案 1 :(得分:0)

起初,我误解了您的问题。根据我现在所知道的,您不需要使用github问题中所述的tfjs节点,而是tfjs节点

https://us04web.zoom.us/j/74533735324?pwd=d2NkcENZS3Q2MUtEeGFjc2V6TUowdz09

原因是默认情况下tfjs设计为在浏览器上运行。我仍然认为以下信息可能对将来的某些人有所帮助。

这是NodeJS的官方资源 https://nodejs.dev/differences-between-nodejs-and-the-browser

脚本失败的原因可能与多种原因有关。这是我的想法:

  1. 您正在尝试在应用程序中导入Node不知道如何加载的模块。这可能是因为您尚未使用以下方式安装模块:
npm install --save @tensorflow/tfjs-automl @tensorflow/tfjs
  1. tfjs可能需要先捆绑在一起,然后才能在浏览器中使用。这意味着仅凭一个简单的nodejs应用程序还不够,而是您需要做一个简单的设置将模块和脚本捆绑到一个脚本中,然后就可以在浏览器中使用。

  2. 您确实在正确使用模块,但是浏览器不知道如何加载它们,可能是因为您的浏览器不理解“导入”语法。尽管不太可能,但我只是想着要这样。

  3. 您的设置很好,但是您没有正确使用tensorflow库。从你告诉我的评论。我感到也许loadImageClassification方法或classify方法都没有收到他们期望的参数。请记住,loadImageClassification需要一个tensorflow在分类期间将使用的json文件的URL。我还发现,如果不设置宽度和高度,那么在动态创建图像时,tfjs会遇到很多麻烦,如此处所述:

Requested texture size [0x0] is invalid. error when i am loading image in browser

我浏览了tfjs示例,发现其中一个实际上看起来像您在此处发布的示例: https://github.com/tensorflow/tfjs/tree/master/tfjs-automl/demo/img_classification

我也自己在github个人资料中进行了类似的设置

https://github.com/taro-0/tsfjs-sample

相关问题