問題描述
我正在使用 tensorflow bodypix 模型創(chuàng)建一個(gè)身體分割應(yīng)用程序.它在瀏覽器中運(yùn)行良好.我正在使用 webpack 來(lái)使用它的模塊(見下文)
import * as wasm from "@tensorflow/tfjs-backend-wasm";從@tensorflow/tfjs-core"導(dǎo)入 * as tf;從@tensorflow-models/body-pix"導(dǎo)入*作為bodyPix;wasm.setWasmPaths("./wasm/");tf.setBackend("wasm").then(() => {//一些簡(jiǎn)單的香草js代碼});//更多的香草js代碼...
它在 chrome 中運(yùn)行良好,運(yùn)行 npx webpack
后按預(yù)期輸出.
但是,當(dāng)簡(jiǎn)單地通過創(chuàng)建一個(gè)主電子文件來(lái)使用電子運(yùn)行它時(shí),它只會(huì)輸出一個(gè)空白的白色屏幕,并在控制臺(tái)中顯示以下錯(cuò)誤-
Uncaught TypeError: this.util.TextEncoder 不是構(gòu)造函數(shù)在新的<匿名>(main.js:2)
它指向的行來(lái)自一個(gè)看起來(lái)像這樣的縮小代碼-
...SOME_CODE...&&Me().setPlatform("node",new class{構(gòu)造函數(shù)(){this.util=n(758),this.textEncoder=new this.util.TextEncoder}...SOME_MORE_CODE...
我認(rèn)為電子只是沒有頂欄的鉻,但這似乎是錯(cuò)誤的.有人可以幫我嗎我正在使用以下版本-
<塊引用>nodejs v12.16.3"、electron11.1.1"、tfjs2.8.2"
查看chrome和electron的截圖-
IN CHROME(點(diǎn)擊放大)
...................................................
IN ELECTRON(點(diǎn)擊放大)
解決方案
我以前有
wasm.setWasmPaths("./wasm/");tf.setBackend("wasm").then(() => {//一些簡(jiǎn)單的香草js代碼});
在我的主代碼中,我已將文件夾從 wasm(dist/) 復(fù)制到項(xiàng)目的文件夾中.
從我的項(xiàng)目文件夾中刪除相同的內(nèi)容并將代碼更改為 -
wasm.setWasmPaths("../node_modules/@tensorflow/tfjs-backend-wasm/dist/");//或從 ./開始,如果您的主文件與 node_modules 位于同一文件夾中tf.setBackend("wasm").then(() => {//...});
我是怎么回到這里的?
首先感謝@edkeveked 的努力并指出我在 Electron App (Nodejs) 中加載 TensorflowJS 時(shí)出錯(cuò)p>
我通過創(chuàng)建一個(gè) electron hello world 項(xiàng)目然后添加 tfjs 和 tfjs-backend-wasm 得到了解決方案.新項(xiàng)目工作正常,但是即使將 node_modules 從新項(xiàng)目移動(dòng)到舊項(xiàng)目也不適用于舊項(xiàng)目.但是一旦我更改了 wasm 路徑,它就可以正常工作.
更新:
現(xiàn)在我已經(jīng)多次遇到這個(gè)問題,每次都是通過創(chuàng)建一個(gè)新文件夾來(lái)解決的,首先安裝電子并先創(chuàng)建一個(gè)簡(jiǎn)單的電子應(yīng)用程序,然后安裝其他依賴項(xiàng)并將舊代碼復(fù)制到新文件夾中.(警告:不要'不復(fù)制節(jié)點(diǎn)模塊文件夾)
好像是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
這篇關(guān)于this.util.TextEncoder 不是僅在電子應(yīng)用程序中的構(gòu)造函數(shù)(適用于 chrome)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!