Comments (9)
Thanx for your awesome job!
Below a working eg with tfjs-node based on your test:
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)
})()
from nsfwjs.
Something like a way to pass some options to configure lib using tfjs-node and tfjs-node-gpu would be much apreciated I think. BTW i will make a backend demo as soon as i have the time.
from nsfwjs.
Hey @AZOPCORP - so I've recently solved how to do this! (I think)
I made a test, and for that to work it had to run 100% in node. This test runs a classification on the logo without accessing a browser.
https://github.com/infinitered/nsfwjs/blob/master/__tests__/regressionCheck.ts
I hope the setup code in this test works for you. I'm sure it could be better wrapped in the actual lib though. If you do use this lib on a backend, please contribute back any improvements 👍
from nsfwjs.
very cool!
Maybe we should put an example in the demo folder?
from nsfwjs.
AZOPCORP, I am getting this Error: Request for file://.model/model.json failed due to error: TypeError: Only HTTP(S) protocols are supported.
from nsfwjs.
Closing because question moved to https://github.com/infinitered/nsfwjs/wiki/FAQ:-NSFW-JS
from nsfwjs.
For information, if you are still wondering how to run it on Node.js, this guy's code works https://github.com/mishazawa/nums
When you do npm install nsfwjs
and use the following it doesn't work immediately.
const nsfwjs = require('nsfw/dist')
He basically forked the code to lib/nsfwjs/index.js
and made minor changes to make it runnable. (his repo doesn't have the classifyGif
function though)
It would be nice and not too much work to have this published to npmjs.org.
Similarly you can use @tensorflow-models/toxicity
pretty easily on server side using just
const toxicity = require('@tensorflow-models/toxicity')
const sentences = ['I love C++']
toxicity.load(0.9).then(model =>
model.classify(sentences).then(predictions => ...)
)
What I would like to have from a developer point of view is to be able to use it out of the box the same way,
const image = ...
nsfwjs.load().then(model => // Instead of nsfwjs.load('file://..../model/')
model.classify(image).then(predictions => ...)
)
from nsfwjs.
I wish he had contributed back with a Pull Request. @mycaule - would you be willing to take a shot?
If not, this is something I could get around to at some point.
from nsfwjs.
Ok I will try to do this week, the workflow between NPM and your build might be something I can't test though.
For the fetching of this line, @tensorflow-models
managed that by hosting the files at tfhub.dev
Lines 21 to 22 in 0497856
it might save you some S3 costs.
const mobilenet = require('@tensorflow-models/mobilenet')
mobilenet.load().then(model => model.classify(...).then(predictions => ...)
// Downloads a file from tfhub in the background
// https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/classification/1/model.json?tfjs-format=file
See #224
from nsfwjs.
Related Issues (20)
- [Node.JS] Memory leak - Server reaches 100% memory after 5 or 6 days HOT 5
- 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 3
- The accuracy is much less than the site HOT 1
- 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.