問題描述
我正在使用 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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!