Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FurkanGozukara opened this issue Aug 13, 2023 · 2 comments

Comments

@FurkanGozukara
Copy link

FurkanGozukara commented Aug 13, 2023

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
Copy link
Member

GantMan commented Aug 14, 2023

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
Copy link
Author

FurkanGozukara commented Aug 15, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants