How to Detect NSFW Content in an Image in JavaScript

Cloudmersive
2 min readAug 25, 2021

--

The amount of websites and applications that allow users to upload images is significant, and that amount is steadily growing. Whether the image is a profile picture to represent them on the site or an image that they upload to edit, images and photos are a regular occurrence on the web. However, one of the issues that can arise from this capability is the presence of racy or inappropriate content in the images. To eliminate this issue, you can use the following API in JavaScript to scan an image and detect any ‘Not Safe for Work’ (NSFW) content.

Let’s begin by running this command to install jQuery:

bower install jquery

With the installation complete, we can call the function with the below example code:

var form = new FormData();
form.append("imageFile", fileInput.files[0], "file");
var settings = {
"url": "https://api.cloudmersive.com/image/nsfw/classify",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "multipart/form-data",
"Apikey": "YOUR-API-KEY-HERE"
},
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};
$.ajax(settings).done(function (response) {
console.log(response);
});

And just like that, you will be able to ensure that images associated with your application will not contain undesirable content. To retrieve your API key, head over to the Cloudmersive website to register for a free account; this provides 800 monthly calls across our library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet