有没有办法用 require 替换脚本标签 src 并在节点上运行相同的脚本? [英] Is there a way to replace script tag src with require and run the same script on node?

查看:58
本文介绍了有没有办法用 require 替换脚本标签 src 并在节点上运行相同的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览器中运行以下内容:

I am running the following in a browser:

<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>

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

What I am trying to do is convert the script to something I can run in node js, like this:

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 运行脚本,但它失败了:

I then run the script with node --experimental-modules index.js and it fails with:

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

我也试过require:

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();

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

I had to remove "type": "module" from package.json and run with node index index.js. It gave the same error.

我也尝试不捕获require:

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.

When I run this, I get the error: (node:24211) UnhandledPromiseRejectionWarning: ReferenceError: tf is not defined.

这似乎很明显,但是有没有办法做 <script src= 所做的事情,但是在节点中,即引入外部脚本,以便我的脚本可以看到和使用外部脚本中的变量/方法?

This seems like it might be obvious, but is there a way to do what <script src= does, but in node, i.e. bring in the external script so my script can see and use the variables/methods in the external script?

推荐答案

对于想要在节点上运行 tensorflow 预测的任何其他人:

For anyone else who wants to run tensorflow predictions on node:

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);

这篇关于有没有办法用 require 替换脚本标签 src 并在节点上运行相同的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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