nsfwjs
2024-06-13 11:59:06
本地运行官方Demo :
1、下载源代码:
git clone https://github.com/infinitered/nsfwjs.git
2、cd到nsfwjs文件夹执行:
yarn prep
3、cd到 nsfwjs\examples\nsfw_demo 文件夹,执行:
yarn start
此时会自动启动一个服务,并且会自动打开默认浏览器访问http://localhost:3000/ 这里的页面和官方一致
Browserify 浏览器版本的:
nsfwjs\examples\minimal_demo\index.html 这里边没有 nsfwjs.min.js 从哪里来的?
我们直接使用命令获取
npm install nsfwjs
在这里:node_modules\nsfwjs\dist\browsernsfwjs.min.js 就有了 dist 文件夹就是我们需要的:
V3的调用示例:
<!-- Load TensorFlow.js. This is required --> <script src="https://unpkg.com/@tensorflow/tfjs@2.6.0" type="text/javascript"></script> <script src="./dist/models/inception_v3/model.min.js" type="text/javascript"></script> <script src="./dist/models/inception_v3/group1-shard1of6.min.js" type="text/javascript"></script> <script src="./dist/models/inception_v3/group1-shard2of6.min.js" type="text/javascript"></script> <script src="./dist/models/inception_v3/group1-shard3of6.min.js" type="text/javascript"></script> <script src="./dist/models/inception_v3/group1-shard4of6.min.js" type="text/javascript"></script> <script src="./dist/models/inception_v3/group1-shard5of6.min.js" type="text/javascript"></script> <script src="./dist/models/inception_v3/group1-shard6of6.min.js" type="text/javascript"></script> <script src="./dist/browser/nsfwjs.min.js" type="text/javascript"></script> <script> // const nsfwjs = require('nsfwjs') const img = new Image(); img.crossOrigin = "anonymous"; // some image here img.src = "1.jpg"; img.onload = function () { // Load the model. tf.setBackend("cpu"); //! https://github.com/tensorflow/tfjs/issues/1644 nsfwjs.load("InceptionV3").then((model) => { // Classify the image. model.classify(img).then((predictions) => { console.log("Predictions", predictions); }); }); }; </script> <img src="1.jpg" alt=""> <pre> Checkout console.log output for results! </pre>