Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An NSFW Image Classification REST API for effortless Content Moderation built with Node.js, Tensorflow, and Parse Server

License

NotificationsYou must be signed in to change notification settings

SashiDo/content-moderation-image-api

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.

Examples & Demos

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 SourceImage SourceImage Source
Classification ResultClassification ResultClassification 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 DemoSexy DemoDrawing Demo

How it works

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.

Classification map

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

File Structure

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

Installation & Configuration

Requirements:

  • Node.JS >= 10.2.1

  • Mongo DB

Download the project

Clone the repo:

git clone https://github.com/SashiDo/content-moderation-image-api.gitcd content-moderation-image-api

Set Environment Variables

Copy the env.example to .env file and set the environment variables for your local environment with your favorite editor:

cp env.example .env

Place 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

Install Dependencies

As this is a full-featured example, all dependencies are present to the packege.json. You only need to run:

npm install

Start the project

npm run dev

If 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 :)

API Usage Examples

The project contains two approaches for classifying images - for direct communication through the Parse SDK using Cloud Code and from an Express route.

Classify from the Express endpoint

curl http://localhost:1337/api/image/classify?url=https://nsfw-demo.sashido.io/sexy.png

Classify from a Cloud Code function via Parse SDKs

You can invoke thensfwImageClassify function from the client-side or using the Parse Server REST API:

Android SDK Example

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.

iOS SDK Example

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.

REST API Example

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/nsfwImageClassify

More 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.

Deployment on Production

1. Environment Variables Setup

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 URLSizeShape SizeAccuracy
https://ml.files-sashido.cloud/models/nsfw_inception_v3/Huge29993%
https://ml.files-sashido.cloud/models/nsfw_mobilenet_v2/90/2.6 MB22490%
https://ml.files-sashido.cloud/models/nsfw_mobilenet_v2/93/4.2 MB22493%

Please notethe Inception_v3 model used for this projects has high RAM/CPU consumption. While the two mobilenet models are far more lightweight.

Choose the model and set the following environment variables for your live server:

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

2. Code Deployment

Deployment on SashiDo

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

Deployment on other providers

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.

What's next?

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!

Contribution

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. :)

License

Copyright © 2020, CloudStrap AD. SeeLICENSE for further details.

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp