资讯动态

node + nsfwjs 流程记录

发布时间:2026/9/5 15:08:33 来源:尧图企业网站定制
nsfwjs是一个免费开源的图片鉴别插件本地打包尺寸过大不符合预期所以大多数人选择在服务端部署。下面是node版本部署流程笔记仅供参考node服务这里采用的是express框架Express - Node.js web application frameworknpm install express --savensfwjs代码库地址GitHub - infinitered/nsfwjs: NSFW detection on the client-side via TensorFlow.jshttps://github.com/infinitered/nsfwjs建议用管理员权限启动cmd第一步环境准备npm install --global windows-build-tools或npm install -g --production windows-build-tools以上环境准备我个人尝试了多次才执行成功若遇到失败可重试不要着急第二步依赖安装npm install nsfwjs注意下面这个安装需要翻墙里面部分引用资源在外网。大家遇到最多的问题就是这个包安装或者执行失败可尝试用cnpmnpm install tensorflow/tfjs-nodetensorflow/tfjs-node和tensorflow/tfjs两个包是互斥的删除node_modules后重试你可能会用到的包npm i tensorflow/tfjs-node-gpu相关文档tfjs/README.md at master · tensorflow/tfjs · GitHub快速删除庞大的node_modules指令rd /s /q node_modules扩展文档中推荐的另一个模式sharpOutput options - sharp - High performance Node.js image processing (pixelplumbing.com)const getMetadata async (img) { const image await sharp(img).jpeg().metadata(); const { data, info } await sharp(img) .toFormat(jpeg) .raw() .toBuffer({ resolveWithObject: true }); const numChannels 3 const numPixels image.width * image.height const values new Int32Array(numPixels * numChannels) for (let i 0; i numPixels; i) for (let c 0; c numChannels; c) values[i * numChannels c] data[i * 4 c] return tf.tensor3d(values, [image.height, image.width, numChannels], int32) } app.post(/nsfw, upload.single(image), async (req, res) { if (!req.file) res.status(400).send(Missing image multipart/form-data) else { try { const image await getMetadata(req.file.buffer) // const image await convert(req.file.buffer) const predictions await _model.classify(image) image.dispose() res.json(predictions) } catch (error) { res.status(400).send(error) } } })也可以将模型下载到项目中模型发布仓库GitHub - GantMan/nsfw_model: Keras model of NSFW detector

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价