Comments (1)
@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:
- Convert images to jpeg using jpeg-js and return as tf.Tensor3D for classifcation
- 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)
- [Node.JS] Memory leak - Server reaches 100% memory after 5 or 6 days HOT 6
- type error for `type: 'graph'` for `.load()`
- Trying to fetch image from axios.
- Getting `Uncaught ReferenceError` when using `nsfwjs.load()` HOT 1
- Android Client HOT 2
- [False Positive]: a little bit funny HOT 2
- Which model is the best atm? Resources hardware is not a problem. inception_v4?
- I really need to run this locally for batch processing local images any guides tutorials? HOT 2
- How to use the Inception V3 model in the npm package?
- when adding a frame to an nsfw picture...
- Problem detecting adult content from camera images
- Cant import nsfw in react Project HOT 2
- Logs spammed by tfjs-node launched twice
- Not work local model load HOT 4
- Broken with the recent file on next.js HOT 4
- Error connecting to cloudfront.net since this morning HOT 12
- Publish to npm out of sync HOT 3
- `console.warn` when `BASE_PATH` is used on load
- Remove GIF Support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nsfwjs.