infinitered / nsfwjs

NSFW detection on the client-side via TensorFlow.js
https://nsfwjs.com/
MIT License
8.05k stars 537 forks source link

Question #499

Open Natemo2625 opened 3 years ago

Natemo2625 commented 3 years ago

So let's say I wanted to use this with a discord bot that checks a url for nsfw content. Can I do that with this, and how could I do it?

GantMan commented 3 years ago

You'd have to have your bot pull the HTML/CSS and parse for JPG/PNG/GIF files. Once you parse the files you would loop through them and use that information to report.

GantMan commented 3 years ago

You're not the first person to ask about this, perhaps I should make this a feature of the library? Just thinking on this. Perhaps it's a useful enough skill.

Natemo2625 commented 3 years ago

Yeah, I have a command that screenshots whatever site you want, and if it's NSFW it returns This site is NSFW, but it doesn't check for images, just URLs

GantMan commented 3 years ago

Currently, to use this you'd have to parse the HTML/CSS. I'll mark this ticket as a feature request and revisit it next Hacktoberfest.

EvEloBE commented 3 years ago

I've just added this to my discord bot as well. This is how you can do it yourself.

const tf = require("@tensorflow/tfjs-node")
const nsfwjs = require("nsfwjs")
const fetch = require("node-fetch")

// Attachment has to contain image url, height and width
async function classifyImage(attachment) {
const model = await nsfwjs.load()

const imageBuffer = await fetch(attachment.url).then((res) => res.buffer());
const imageTensor = tf.node.decodeImage(imageBuffer)

return await model.classify(imageTensor)
}

This is a bit different from my implementation, but i think it should work.