简体   繁体   English

在 Chrome 扩展程序中运行 tensorflow

[英]Running tensorflow in Chrome extension

I am having problem with implementing pretrained tensorflow.js toxicity model in my chrome extension, I used webpack to bundle my node modules.我在我的 chrome 扩展中实现预训练的 tensorflow.js 毒性 model 时遇到问题,我使用 webpack 来捆绑我的节点模块。

The error i am getting我得到的错误

popup.html:1 Unchecked runtime.lastError: Cannot access contents of the page. Extension manifest must request permission to access the respective host.

engine.js:443 Uncaught (in promise) Error: No backend found in registry.
    at Engine.getSortedBackends (engine.js:443)
    at Engine.initializeBackendsAndReturnBest (engine.js:454)
    at Engine.get (engine.js:1405)
    at Engine.makeTensor (engine.js:860)
    at makeTensor (tensor_ops.js:121)
    at tensor (tensor_ops.js:80)
    at Module.decodeWeights (io_utils.js:308)
    at GraphModel.loadSync (graph_model.js:165)
    at GraphModel._callee$ (graph_model.js:129)
    at tryCatch (runtime.js:68)

I tried fixing the backend error via this solution, but it leads into another error Handpose tfjs Error - No backend found in registry我尝试通过此解决方案修复后端错误,但它导致另一个错误Handpose tfjs Error - No backend found in registry

My manifest.json我的清单.json

    {
    "name": "TensorFlow toxicity recognition",
    "version": "0.1",
    "description": "TensorFlow chrome extension analyzing text toxicity",
    "author": "Petr Kolouch",
    "browser_action": {
      "default_popup": "popup.html"
  },
    "permissions": [
      "activeTab"
    ],
    "options_page": "popup.html",
    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
    "manifest_version": 2
  }

My index.js我的 index.js

const toxicity = require('@tensorflow-models/toxicity');
       
    const threshold = 0.9;
    
    chrome.tabs.executeScript( {
        code: "window.getSelection().toString();"
    }, (selection) => {
    
    toxicity.load(threshold).then(model => {
    
      model.classify(selection).then(predictions => {
    
        console.log(predictions);
        document.getElementById("results").innerHTML = predictions;
    
            });
        });
    });
    

My popup.html我的弹出窗口.html

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="main.js"></script>
</head>
<body>

    <div id="results">

    </div>
    
</body>
</html>

My webpack.config.js我的 webpack.config.js

const path = require('path');
module.exports = {
  mode: 'development',
  target: 'web',
  entry: ['babel-polyfill', './src/index.js'],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js'
  },
  module: {
    rules: [
      {
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: ['@babel/plugin-proposal-class-properties']
          }
        }
      }
    ]
  }
};

My package.json我的 package.json

{
  "name": "tfjsdemo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
    "mode": "development",
    "scripts": {
      "build": "webpack --config webpack.config.js"
    },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.10.3",
    "@babel/plugin-proposal-class-properties": "^7.10.1",
    "@babel/preset-env": "^7.10.3",
    "babel-loader": "^8.1.0",
    "babel-polyfill": "^6.26.0",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.12"
  },
  "dependencies": {
    "@tensorflow-models/toxicity": "^1.2.2",
    "@tensorflow/tfjs": "^2.0.1"
  }
}

Thanks for your help!谢谢你的帮助!

To resolve the registry issue, only installing @tensorflow/tfjs-core to your dependencies will do.要解决注册表问题,只需将@tensorflow/tfjs-core安装到您的依赖项即可。 Also, import the registry to your js code:此外,将注册表导入到您的 js 代码中:
import '@tensorflow/tfjs-backend-webgl';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM