问题描述
我正在使用 tensorflow bodypix 模型创建一个身体分割应用程序.它在浏览器中运行良好.我正在使用 webpack 来使用它的模块(见下文)
import * as wasm from "@tensorflow/tfjs-backend-wasm";从@tensorflow/tfjs-core"导入 * as tf;从@tensorflow-models/body-pix"导入*作为bodyPix;wasm.setWasmPaths("./wasm/");tf.setBackend("wasm").then(() => {//一些简单的香草js代码});//更多的香草js代码...
它在 chrome 中运行良好,运行 npx webpack
后按预期输出.
但是,当简单地通过创建一个主电子文件来使用电子运行它时,它只会输出一个空白的白色屏幕,并在控制台中显示以下错误-
Uncaught TypeError: this.util.TextEncoder 不是构造函数在新的<匿名>(main.js:2)
它指向的行来自一个看起来像这样的缩小代码-
...SOME_CODE...&&Me().setPlatform("node",new class{构造函数(){this.util=n(758),this.textEncoder=new this.util.TextEncoder}...SOME_MORE_CODE...
我认为电子只是没有顶栏的铬,但这似乎是错误的.有人可以帮我吗我正在使用以下版本-
<块引用>nodejs v12.16.3"、electron11.1.1"、tfjs2.8.2"
查看chrome和electron的截图-
IN CHROME(点击放大)
...................................................
IN ELECTRON(点击放大)
解决方案
我以前有
wasm.setWasmPaths("./wasm/");tf.setBackend("wasm").then(() => {//一些简单的香草js代码});
在我的主代码中,我已将文件夹从 wasm(dist/) 复制到项目的文件夹中.
从我的项目文件夹中删除相同的内容并将代码更改为 -
wasm.setWasmPaths("../node_modules/@tensorflow/tfjs-backend-wasm/dist/");//或从 ./开始,如果您的主文件与 node_modules 位于同一文件夹中tf.setBackend("wasm").then(() => {//...});
我是怎么回到这里的?
首先感谢@edkeveked 的努力并指出我在 Electron App (Nodejs) 中加载 TensorflowJS 时出错p>
我通过创建一个 electron hello world 项目然后添加 tfjs 和 tfjs-backend-wasm 得到了解决方案.新项目工作正常,但是即使将 node_modules 从新项目移动到旧项目也不适用于旧项目.但是一旦我更改了 wasm 路径,它就可以正常工作.
更新:
现在我已经多次遇到这个问题,每次都是通过创建一个新文件夹来解决的,首先安装电子并先创建一个简单的电子应用程序,然后安装其他依赖项并将旧代码复制到新文件夹中.(警告:不要'不复制节点模块文件夹)
好像是tfjs或者electron的bug
I am creating a body segmentation app using tensorflow bodypix model. It works fine in the browser. I am using webpack to use its modules(see below)
import * as wasm from "@tensorflow/tfjs-backend-wasm";
import * as tf from "@tensorflow/tfjs-core";
import * as bodyPix from "@tensorflow-models/body-pix";
wasm.setWasmPaths("./wasm/");
tf.setBackend("wasm").then(() => {
//some simple vanilla js code
});
//some more vanilla js code...
It works exactly fine in chrome and giving output as expected after running npx webpack
.
However when irun it with electron simply by creating a main electron file it outputs nothing but a blank white screen with the following error in console-
Uncaught TypeError: this.util.TextEncoder is not a constructor
at new <anonymous> (main.js:2)
the line where it is pointing is from a minified codew which looks like this-
...SOME_CODE...&&Me().setPlatform("node",new class{
constructor(){this.util=n(758),this.textEncoder=new this.util.TextEncoder}...SOME_MORE_CODE...
i thought that electron is simply chrome without top bars, but this seems wrong. can someone help me here i am using following versions-
"nodejs v12.16.3", "electron11.1.1", "tfjs2.8.2"
see the screen shot of chrome and electron-
IN CHROME(click to enlarge)
................................................
IN ELECTRON(click to enlarge)
THE SOLUTION
i previously have
wasm.setWasmPaths("./wasm/");
tf.setBackend("wasm").then(() => {
//some simple vanilla js code
});
in my main code, and i have copied the folder from wasm(dist/) to project's folder.
Deleting the same from my project's folder and changing the code to -
wasm.setWasmPaths("../node_modules/@tensorflow/tfjs-backend-wasm/dist/"); //or start from ./ if your main file is in same folder as node_modules
tf.setBackend("wasm").then(() => {
//...
});
How i recahed here?
at first thanks to @edkeveked for his effort and pointing me toError loading TensorflowJS in Electron App (Nodejs)
i got the solution by creating an electron hello world project and then adding tfjs, then tfjs-backend-wasm. the new project is working correctly but however even moving the node_modules from new project to older one is not working for the older. but as soon i changed the wasm path, it worked giving no error.
Update:
now I have encountered the problem several times and everytime it's solved by creating a new folder, first installing electron and creating a simple electron app first, and then installing other dependencies and copying old code in the new folder.(warning: don't copy the node modules folder)
It seems to be a bug in tfjs or electron
这篇关于this.util.TextEncoder 不是仅在电子应用程序中的构造函数(适用于 chrome)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!