- Notifications
You must be signed in to change notification settings - Fork22
An NSFW Image Classification REST API for effortless Content Moderation built with Node.js, Tensorflow, and Parse Server
License
SashiDo/content-moderation-image-api
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Machine Learning has already matured to the point where it should be a vital part of projects of all sizes. Advances in computer processing power, storage, data tools, web, etc made machine learning technologies to become more and more affordable. This and the constant strive for innovation, led SashiDo's team to create afully-functional Content Moderation Service with React based Admin Panel built with Open-Source tools and libraries only. The result is a simple and elegant product, which is easy to maintain, can be integrated into any Node.JS project and hosted anywhere. One at a time, we will share all three layers of the Content moderation service - API, Automation Engine and beautiful Admin Panel. The content moderation REST API is just the first chunk.
These are the examples and the demos of what you'll have in your tools set after you deploy and integrate this repo in your projects. We've prepared examples only for some of the classes. For the other classes we think you should experiment by yourself ...you know what I mean ;).
| Image Source | Image Source | Image Source |
|---|---|---|
![]() | ![]() | ![]() |
| Classification Result | Classification Result | Classification Result |
[{ "className": "Neutral", "probability": 0.93821}, { "className": "Drawing", "probability": 0.05473}, { "className": "Sexy", "probability": 0.00532}, { "className": "Hentai", "probability": 0.00087}, { "className": "Porn", "probability": 0.00085}] | [{ "className": "Sexy", "probability": 0.99394}, { "className": "Neutral", "probability": 0.00432}, { "className": "Porn", "probability": 0.00164}, { "className": "Drawing", "probability": 0.00006}, { "className": "Hentai", "probability": 0.00001}] | [{ "className": "Drawing", "probability": 0.96063}, { "className": "Neutral", "probability": 0.03902}, { "className": "Hentai", "probability": 0.00032}, { "className": "Sexy", "probability": 0.00001}, { "className": "Porn", "probability": 0.00005}] |
| Neutral Demo | Sexy Demo | Drawing Demo |
This REST API is built in Node.JS with Mongo DB and Parse Server. Classifying images may be invoked from an Express route or a Cloud Code function ( Parse Server lovers, you're welcome :) )
We have implementedNSFW.JS classification, which usesTensorFlowJS pretrained models.
Pass the API an image and receive a JSON response, which holds the predictions on how likely this image falls into each of the following classes:
Drawing - Harmless art, or picture of art
Hentai - Pornographic art, unsuitable for most work environments
Neutral - General, inoffensive content
Porn - Indecent content and actions, often involving genitalia
Sexy - Unseemly provocative content, can include nipples
The REST API is built on top of Parse Server. You can use it in a standard Express app, but keep in mind that the file structure of the repo is Parse specific. The code is organized in asrc folder andsrc/cloud/main.js is the root file for the service. For more information about how the project is building on the Local env and in Production, take a look at thepackage.json
Node.JS >= 10.2.1
Mongo DB
Clone the repo:
git clone https://github.com/SashiDo/content-moderation-image-api.gitcd content-moderation-image-apiCopy the env.example to .env file and set the environment variables for your local environment with your favorite editor:
cp env.example .envPlace your MongoDB URI. If your app is hosted at SashiDo, you can use the database URI of your SashiDo project. Find the connection string from the app'sDashboard -> App -> App Settings -> Security & Keys
As this is a full-featured example, all dependencies are present to the packege.json. You only need to run:
npm installnpm run devIf everything is okay you should see an output similar to this one:
[nodemon] 2.0.4...[nodemon] starting `node index index.js`✨ Built in 2.55s.node-pre-gyp ......Running on http://localhost:1337⠙ Building index.js...The NSFW Model was loaded successfuly!✨ Built in 16.41s.If you see the output above, you are ready to play with the API :)
The project contains two approaches for classifying images - for direct communication through the Parse SDK using Cloud Code and from an Express route.
curl http://localhost:1337/api/image/classify?url=https://nsfw-demo.sashido.io/sexy.pngYou can invoke thensfwImageClassify function from the client-side or using the Parse Server REST API:
HashMap<String,String>params =newHashMap<String,String>();params.put("url","https://nsfw-demo.sashido.io/sexy.png");ParseCloud.callFunctionInBackground("nsfwImageClassify",params,newFunctionCallback<Object>() {voiddone(Objectpredictions,ParseExceptione) {if (e ==null) {// prediction } }});
More information about how to work with the Android SDK can be found in theofficial docs.
PFCloud.callFunctionInBackground("nsfwImageClassify", withParameters:["url":"https://nsfw-demo.sashido.io/sexy.png"]){(predictions, error)inif !error{ // prediction}}
More information about how to work with the Parse iOS SDK can be found in theofficial docs.
curl -X POST \ -H "X-Parse-Application-Id: myAppId" \ -H "X-Parse-REST-API-Key: myRestKey" \ -H "Content-Type: application/json" \ -d '{ "url": "https://nsfw-demo.sashido.io/sexy.png" }' \ http://localhost:1337/1/functions/nsfwImageClassifyMore information about how to work with the Parse REST API can be found in theofficial docs.
SashiDo users can test all Cloud Code functions from our super-friendlyAPI Console that’s built in the Dashboard. Moreover, it gives you the option to export ay request to cURL.
For production, you need to set theNSFW model URL and theNSFW Model Shape size. SashiDo stores three NSFW models, each one you can set easily using the following URLs:
| Model URL | Size | Shape Size | Accuracy |
|---|---|---|---|
| https://ml.files-sashido.cloud/models/nsfw_inception_v3/ | Huge | 299 | 93% |
| https://ml.files-sashido.cloud/models/nsfw_mobilenet_v2/90/ | 2.6 MB | 224 | 90% |
| https://ml.files-sashido.cloud/models/nsfw_mobilenet_v2/93/ | 4.2 MB | 224 | 93% |
Please notethe Inception_v3 model used for this projects has high RAM/CPU consumption. While the two mobilenet models are far more lightweight.
TF_MODEL_URL = MODEL_URLTF_MODEL_INPUT_SHAPE_SIZE = MODEL_SHAPE_SIZE# ExampleTF_MODEL_URL="https://ml.files-sashido.cloud/models/nsfw_mobilenet_v2/93/"TF_MODEL_INPUT_SHAPE_SIZE=224
This is probably the simplest way to deploy the code in production. At SashiDo we have implemented an automatic git deployment process following theThe Twelve Factor App principle.
Connect your SashiDo app with GitHub, checkhere for more details how to start using GitHub with SashiDo.
Next, the code can be easily deployed with two simple commands for adding a remote branch and pushing the code.
git remote add production git@github.com:parsegroundapps/<your-pg-app-your-app-repo>.gitgit push -f production master
Basically, you need to follow the same steps as for SashiDo Deployment. Simply follow the requirements of your hosting provider when setting environment variables for production and deploying the code.
To get a further insight into the project and what inspired us to build this service, check out our blog post on the topic here.
The REST API is a part of the Content Moderation service, which also offers:
Automation Engine that will automatically delete inappropriate images. Set the params and reduce manual work to the bare minimum. - Coming Soon!
Admin Panel where all images in need of moderation are stacked up in a beautiful interface, which allows you to make decisions with just a click. - Coming Soon!
Thanks for looking at this section. We’re open to any cool ideas, so if you have one and are willing to share - fork the repo, apply changes and open a pull request. :)
Copyright © 2020, CloudStrap AD. SeeLICENSE for further details.
About
An NSFW Image Classification REST API for effortless Content Moderation built with Node.js, Tensorflow, and Parse Server
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.


