![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
nsfwjs-node
Advanced tools
Fork from https://github.com/infinitered/nsfwjs
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/
With async/await
support:
import * as nsfwjs from 'nsfwjs-node'
// Load model from my S3.
// See the section hosting the model files on your site.
const model = await nsfwjs.load()
// Classify the image
const logo = nsfwjs.readImage(filename);
const img = nsfwjs.imageToInput(logo, nsfwjs.NUMBER_OF_CHANNELS);
const predictions = await model.classify(img)
console.log('Predictions: ', predictions)
Without async/await
support:
import * as nsfwjs from 'nsfwjs-node'
const logo = nsfwjs.readImage(filename);
const img = nsfwjs.imageToInput(logo, nsfwjs.NUMBER_OF_CHANNELS);
// Load model from my S3.
// See the section hosting the model files on your site.
nsfwjs.load().then(function(model) {
model.classify(img).then(function(predictions) {
// Classify the image
console.log('Predictions: ', predictions)
})
})
load
the modelBefore you can classify any image, you'll need to load the model. You should use the optional first parameter and load the model from your website, as explained in the install directions.
const model = nsfwjs.load('/path/to/model/directory/')
If you're using a model that needs an image of dimension other than 224x224, you can pass the size in the options parameter.
const model = nsfwjs.load('/path/to/different/model/', {size: 299})
Parameters
model.json
folder.Returns
classify
an imageThis function can take any browser-based image elements (<img>
, <video>
, <canvas>
) and returns 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.classifyGif
This function can take a browser-based image element (<img>
) that is a GIF, and returns an array of prediction arrays. It breaks a GIF into its frames and runs classify
on each with a given configuration. This can take a while, as GIFs are frequently hundreds of frames.
// Returns all predictions of each GIF frame
const framePredictions = await model.classifyGif(img)
If you're looking to update the user on status (e.g. progress bar) or change the number of top results per frame, then you can utilize the configuration parameter.
Example of passing a configuration:
// returns top 1 prediction of each GIF frame, and logs the status to console
const myConfig = {
topk: 1,
setGifControl: (gifControl) => console.log(gifControl),
onFrame: ({ index, totalFrames, predictions }) =>
console.log(index, totalFrames, predictions)
}
const framePredictions = await classifyGif(img, myConfig)
Parameters
topk
- Number of results to return per frame (default all 5)setGifControl
- Function callback receives SuperGif object as an argument, allows a user to save it for later useonFrame
- Function callback on each frame - Param is an object with the following key/values:
index
- the current GIF frame that was classified (starting at 1)totalFrames
- the complete number of frames for this GIF (for progress calculations)predictions
- an array of length topk
, returning top results from classifyReturns
className
and probability
; sorted by probability and limited by topk config parameter.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
For script tags add . Then simply access the nsfwjs global variable. This requires that you've already imported TensorFlow.js as well.
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 nsfw_demo
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
.
A browserified version using nothing but promises and script tags is available in the minimal_demo
folder.
Please do not use the script tags hosted in this demo as a CDN. This can and should be hosted in your project along side the model files.
An FAQ page is available.
More about NSFWJS and TensorFlow.js - https://youtu.be/uzQwmZwy3yw
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 :heart:. It's MIT licensed, and we'll always do our best to help and quickly answer issues. If you'd like to get a hold of us, join our community slack.
Infinite Red offers premium training and support. Email us at hello@infinite.red to get in touch.
Thanks goes to these wonderful people (emoji key):
Gant Laborde 💬 📝 💻 💡 🤔 🚇 👀 ⚠️ | Jamon Holmgren 📖 🤔 💻 🖋 | Jeff Studenski 🎨 | Frank von Hoven III 📖 🤔 | Sandesh Soni 💻 | Sean Nam 📖 | Gilbert Emerson 💻 |
Oleksandr Kozlov 🚇 ⚠️ 💻 | Morgan 💻 🤔 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
Detect NSFW content client side
The npm package nsfwjs-node receives a total of 1 weekly downloads. As such, nsfwjs-node popularity was classified as not popular.
We found that nsfwjs-node demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.