Git Product home page Git Product logo

Comments (1)

caycewilliams avatar caycewilliams commented on June 11, 2024 2

@morteza102030
I had the same problem with accuracy, on a node nestjs api I was able to do the following setup and get a more accurate classification as shown on the website.

For it to work well I was missing two key parts:

  1. Convert images to jpeg using jpeg-js and return as tf.Tensor3D for classifcation
  2. Use the MobileNet V2 (90% accurate) model on https://nsfwjs.com/. This is what nsfwjs.load() uses

model.json download: https://nsfwjs.com/quant_nsfw_mobilenet/model.json
group1-shard1of1 download: https://nsfwjs.com/quant_nsfw_mobilenet/group1-shard1of1

If using them locally, make sure to put them in a static directory (for nestjs, you need @nestjs/serve-static, and place them in dist/public/model)

Like so:

public/
  model/
    model.json
    group1-shard1of1
import * as nsfwjs from 'nsfwjs'
import * as tf from '@tensorflow/tfjs'
import * as jpeg from 'jpeg-js'
const sharp = require('sharp')

//I used @UploadedFile from '@nestjs/common' to handle image uploads.
interface MulterFile {
  fieldname: string
  originalname: string
  encoding: string
  mimetype: string
  buffer: Buffer
  size: number
}
async isCensorableImage(file: MulterFile) {

        const imageToTensor = async (rawImageData: ArrayBuffer): Promise<tf.Tensor3D | ImageData> => {
            rawImageData = await sharp(rawImageData).raw().jpeg().toBuffer()
            const decoded = jpeg.decode(rawImageData); //This is key for the prediction to work well
            const { width, height, data } = decoded
            const buffer = new Uint8Array(width * height * 3);
            let offset = 0;
            for (let i = 0; i < buffer.length; i += 3) {
                buffer[i] = data[offset];
                buffer[i + 1] = data[offset + 1];
                buffer[i + 2] = data[offset + 2];

                offset += 4;
            }

            return tf.tensor3d(buffer, [height, width, 3]);
        }
        await tf.ready();
        tf.env().set('PROD', true) //Disable logs
        const nsfwModel = await nsfwjs.load('http://localhost:8000/model/', { size: 224 }); // Or nsfwjs.load()
        const imageTensor = await imageToTensor(file.buffer);
        const predictionsArray = await nsfwModel.classify(imageTensor)
        console.log(predictionsArray)
        
        return true; //Your logic here checking the predictionsArray
    }

Using node v20.5.1

from nsfwjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.