-
Notifications
You must be signed in to change notification settings - Fork 544
FAQ: NSFW JS
Kevin VanGelder edited this page May 7, 2020
·
3 revisions
Originally asked in https://github.com/infinitered/nsfwjs/issues/50
Yes! We have several versions of this model, including the source code to make your own! The training code and several versions of the model are available here: https://github.com/GantMan/nsfw_model
Originally asked in https://github.com/infinitered/nsfwjs/issues/25
Yes! Untested, but the following code is apparently functional.
const tf =require('@tensorflow/tfjs-node')
const load=require('./dist/index').load
const fs = require('fs');
const jpeg = require('jpeg-js');
// Fix for JEST
const globalAny = global
globalAny.fetch = require('node-fetch')
const timeoutMS = 10000
const NUMBER_OF_CHANNELS = 3
const readImage = (path) => {
const buf = fs.readFileSync(path)
const pixels = jpeg.decode(buf, true)
return pixels
}
const imageByteArray = (image, numChannels) => {
const pixels = image.data
const numPixels = image.width * image.height;
const values = new Int32Array(numPixels * numChannels);
for (let i = 0; i < numPixels; i++) {
for (let channel = 0; channel < numChannels; ++channel) {
values[i * numChannels + channel] = pixels[i * 4 + channel];
}
}
return values
}
const imageToInput = (image, numChannels) => {
const values = imageByteArray(image, numChannels)
const outShape = [image.height, image.width, numChannels] ;
const input = tf.tensor3d(values, outShape, 'int32');
return input
}
(async()=>{
const model = await load('file://./model/')//moved model at root of folder
const logo = readImage(`./_art/nsfwjs_logo.jpg`)
const input = imageToInput(logo, NUMBER_OF_CHANNELS)
console.time('predict')
const predictions = await model.classify(input)
console.timeEnd('predict')
console.log(predictions)
})()
Yes! A proof of concept can be found here: https://shift.infinite.red/nsfw-js-for-react-native-a37c9ba45fe9
We have a free AI Demystified mini-course, a Beginning Machine Learning with TensorFlow.js course, and a Kickstarter for a Intro to Facial Machine Learning in Javascript course