infinitered / nsfwjs

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

I really need to run this locally for batch processing local images any guides tutorials? #778

Open FurkanGozukara opened 1 year ago

FurkanGozukara commented 1 year ago

I couldn't make it work

The website is working great that you put here : https://nsfwjs.com/

I need to give a folder input

It needs to process all and move images that are classified as sexy Porn Drawing Hentai into another folder and neutral into another folder

it needs to use inception v3 most accurate or if there is any more accurate one

I tried both below and not working either

const fs = require('fs');
const tf = require('@tensorflow/tfjs-node');
const nsfwjs = require('nsfwjs');

async function classifyImageWithInceptionV3(imagePath) {
  const imageBuffer = fs.readFileSync(imagePath);
  const image = tf.node.decodeImage(imageBuffer);

  // Load InceptionV3 model from TensorFlow.js
  const inceptionV3 = await tf.loadLayersModel('G:\\nsfwjs\\tf2-preview_inception_v3_classification_4\\saved_model.pb');
  const processedImage = tf.image.resizeBilinear(image, [299, 299]).toFloat().div(255.0).expandDims();

  const predictions = await inceptionV3.predict(processedImage).data();
  return predictions;
}

function moveImage(imagePath, category) {
  const destinationFolder = `./${category}`;
  fs.renameSync(imagePath, `${destinationFolder}/${imagePath}`);
}

async function scanImages(inputFolder) {
  const imageFiles = fs.readdirSync(inputFolder);

  for (const imageFile of imageFiles) {
    const imagePath = `${inputFolder}/${imageFile}`;
    const predictions = await classifyImageWithInceptionV3(imagePath);

    const maxProbability = Math.max(...predictions);
    const maxIndex = predictions.indexOf(maxProbability);

    // Move to the folder with the highest probability
    const categories = ['Drawing', 'Hentai', 'Porn', 'Sexy'];
    moveImage(imagePath, categories[maxIndex]);
  }
}

const inputFolder = 'G:\\nsfwjs\\test'; // Replace with your input folder path
scanImages(inputFolder);

const fs = require('fs');
const tf = require('@tensorflow/tfjs-node');
const nsfw = require('nsfwjs');

async function fn() {
  const imagePath = 'G:\\private-detector\\a.jpg'; // Path to your local image file
  const imageData = fs.readFileSync(imagePath);

  const modelPath = 'G:\\nsfwjs\\tf2-preview_inception_v3_classification_4\\saved_model.pb'; // Path to your local model
  const model = await nsfw.load(modelPath);

  const image = await tf.node.decodeImage(imageData, 3);
  const predictions = await model.classify(image);
  image.dispose();

  console.log(predictions);
}

fn();
GantMan commented 1 year ago

I'll see if we can find a resource to make a few examples.

TBH the best accuracy would be a voting classifier between all 3 models imo. That's something I wanted to work back into the code but I haven't had time.

FurkanGozukara commented 1 year ago

I'll see if we can find a resource to make a few examples.

TBH the best accuracy would be a voting classifier between all 3 models imo. That's something I wanted to work back into the code but I haven't had time.

I made this repo work actually and processed with them

https://github.com/GantMan/nsfw_model

is that same?

I had to do a lot of research to make it work. its install totally outdated

i even made a working fork myself : https://github.com/FurkanGozukara/nsfw_model