infinitered / nsfwjs
- понедельник, 25 февраля 2019 г. в 00:17:31
JavaScript
NSFW detection on the client-side via Tensorflow JS
A simple JavaScript library to help you quickly identify unseemly images; all in the client's browser. NSFWJS isn't perfect, but it's pretty accurate (~90% from our test set of 15,000 test images)... and it's getting more accurate all the time.
Why would this be useful? Check out the announcement blog post.
The library categorizes image probabilities in the following 5 classes:
Drawing
- safe for work drawings (including anime)Hentai
- hentai and pornographic drawingsNeutral
- safe for work neutral imagesPorn
- pornographic images, sexual actsSexy
- sexually explicit images, not pornographyThe demo is a continuous deployment source - Give it a go: http://nsfwjs.com/
import * as nsfwjs from 'nsfwjs'
const img = document.getElementById('img')
// Load model from my S3.
// See the section hosting the model files on your site.
const model = await nsfwjs.load()
// Classify the image
const predictions = await model.classify(img)
console.log('Predictions: ', predictions)
load
the modelBefore you can classify any images, you'll need to load the model. For many reasons, you should use the optional parameter and load the model from your website. Review how in the install directions.
const model = nsfwjs.load('/path/to/model/directory/')
Parameters
model.json
Returns
classify
an imageThis function can take any browser-based image elements (, , ) and return an array of most likely predictions and their confidence levels.
// Return top 3 guesses (instead of all 5)
const predictions = await model.classify(img, 3)
Parameters
Returns
className
and probability
. Array size is determined by the second parameter in the classify
function.NSFWJS is powered by Tensorflow.JS as a peer dependency. If your project does not already have TFJS you'll need to add it.
# peer dependency
$ yarn add @tensorflow/tfjs
# install NSFWJS
$ yarn add nsfwjs
The magic that powers NSFWJS is the NSFW detection model. By default, this node module is pulling from my S3, but I make no guarantees that I'll keep that download link available forever. It's best for the longevity of your project that you download and host your own version of the model files. You can then pass the relative URL to your hosted files in the load
function. If you can come up with a way to bundle the model into the NPM package, I'd love to see a PR to this repo!
The demo that powers https://nsfwjs.com/ is available in the example folder.
To run the demo, run yarn prep
which will copy the latest code into the demo. After that's done, you can cd
into the demo folder and run with yarn start
.
The model was trained in Keras over several days and 60+ Gigs of data. Be sure to check out the model code which was trained on data provided by Alexander Kim's nsfw_data_scraper.
NSFWJS, as open source, is free to use and always will be
Infinite Red offers premium training and support. Email us at hello@infinite.red to get in touch.