A few months ago my friend Bill was creating an application in Nodejs and he ran into a situation where He wanted to block the upload of nude/adult images. An adult content moderation using NodeJs sounds interesting. Well, just another day I was withstanding the same situation, I was thinking should I calculate skin pixel score or I should manually moderate each Image. Then I asked Bill and he suggested me to useCloudinary.
So I gave look to the Cloudinary and their services are really great. I used Cloudinary’s content moderation service, so here I would like to share a piece of code. In this article, we will write down a script which will block the upload of the Nude image in Nodejs. We will go step by step from creating a Cloudinary account to running the Nodejs server.
1. Creating Cloudinary Account
Go to theCloudinarywebsite signup and create an account, where you will get theCloud Name,API KeyandAPI Secretas shown in below Image.
2. Activating Cloudinary Add-on
To recognize and block the adult content, you have to activate Rekognition AI Moderation add on. So first Go toadd-ons pageand then you will see the first add on named asRekognition AI Moderation. Now click onRekognition AI Moderationadd on and select whatever plan suits you.
3. Building Nodejs server
Let’s create a package.json file by usingnpm init command
, and below is my package.json file.
package.json:
{ "name": "adult-content-moderation-using-nodejs", "version": "1.0.0", "description": "Adult content moderation using NodeJs", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Shashank Tiwari", "license": "MIT", "dependencies": { "body-parser": "^1.18.2", "cloudinary": "^1.9.1", "connect-multiparty": "^2.1.0", "cors": "^2.8.4", "express": "^4.16.2" } }
4. Script for Adult content moderation using NodeJs
You can use thenpm install
command to install all the dependencies or you can do the hard work. Create a server.js file in your root directory. Now copy the below code into the server.js and you are good to go.
Server.js:
/* * Adult content moderation using NodeJs * @author Shashank Tiwari */ 'use strict'; const express = require('express'); const http = require('http'); const multipart = require('connect-multiparty'); const cloudinary = require('cloudinary'); const cors = require('cors'); const bodyParser = require('body-parser'); class Server{ constructor(){ this.port = process.env.PORT || 81; this.host = `localhost`; this.app = express(); this.multipartMiddleware = multipart(); this.http = http.Server(this.app); } appConfig(){ this.app.use( bodyParser.json() ); this.app.use( cors() ); cloudinary.config({ cloud_name: '******************', api_key: '******************', api_secret: '******************' }); } includeRoutes(){ this.app.post('/upload', this.multipartMiddleware, function(request, response) { cloudinary.v2.uploader.upload(request.files.image.path,{ moderation: "aws_rek" }, function(error, result) { response.json(result); }); }); } appExecute(){ this.appConfig(); this.includeRoutes(); this.http.listen(this.port, this.host, () => { console.log(`Listening on http://${this.host}:${this.port}`); }); } } const app = new Server(); app.appExecute();
Explanation:In the above Server class, there are two methods that are important.
First method isappConfig()
and second method isincludeRoutes()
, let talk about both of them.
1.appConfig()
: In this method, I have setup the Cloudinary configuration.
2.includeRoutes()
: In this method, I have we have/upload
post URL to handle the upload request. Inside that, we will upload the image on the Cloudinary.
5. conclusion
That was really quick, isn’t it? So this is how I automated the image moderation for my application saving tons of hard work.please let me know your thoughts on this topic in below comment box.