gitmyhub

nsfwjs

TypeScript ★ 8.9k updated 3mo ago

NSFW detection on the client-side via TensorFlow.js

A TypeScript library that detects explicit or adult content in images entirely inside the browser using TensorFlow.js, no images are sent to a server, keeping user data private.

TypeScriptTensorFlow.jsNode.jsWebAssemblysetup: easycomplexity 2/5

NSFWJS is a TypeScript library that checks whether an image contains adult or explicit content, and it does this work entirely inside the user's web browser using TensorFlow.js. No image is sent to a server. The library runs a machine-learning model locally, so user photos stay private and the detection happens as fast as the device can run it.

The library sorts any image into one of five categories: Drawing (safe artwork, including anime), Hentai (explicit drawings), Neutral (ordinary safe-for-work photos), Porn (explicit photographic content), and Sexy (suggestive but not explicitly pornographic). It returns a confidence percentage for each category, so the calling application can decide how to act on the result. Accuracy is around 90 percent with the smaller model and around 93 percent with the mid-sized one.

Three pre-trained models ship with the package: MobileNetV2 (the default, small and fast), MobileNetV2Mid (a graph-based mid-sized model), and InceptionV3 (a larger model expecting 299 by 299 pixel input). You load one with a single function call, classify an image element, and get back an array of predictions. The library can also run in Node.js for server-side processing, and it supports optional WebAssembly and WebGPU backends for faster inference on hardware that can take advantage of them.

To avoid downloading the model on every page load, the library supports caching it in the browser's IndexedDB storage. Developers who want a smaller app bundle can host the model files themselves instead of relying on the bundled base64 copies, which reduces the download by roughly a third. For teams who need tree-shaking, a separate core entry point lets you import only the model definitions you actually use.

The project is open source and maintained by Infinite Red. It works in standard browsers, in React Native via a community package, and in Node.js applications. The README includes working code examples for each environment.

Where it fits