|
How to Scan for NSFW Images in Python |
9/10/2025 - Brian O'Neill |
Failing to properly flag NSFW content can create real problems for enterprises. Inadvertently exposing employees to illicit video or imagery on a corporate network can constitute regulatory violations or lead to independent lawsuits, and failing to screen and remove NSFW content from client-facing applications can cause indelible reputation damage among customers. It’s critically important to scan for NSFW content before allowing image and video to be distributed or displayed in any employee- or user-accessible area. Thankfully, there’s an API for that. Cloudmersive’s AI-powered Image and Video APIs both represent developer-friendly NSFW classification solutions. Like all Cloudmersive APIs, both can be used with a single API key subscription, and both can be easily implemented into production applications with complementary, ready-to-run code examples. In this article, we’ll take a closer look at how NSFW image detection works with Cloudmersive’s Image API (we’ll cover the Video API solution in another article), and we’ll walk through an end-to-end test case where we install the Image API client library in Google Colab and scan a few example images. We’ll share screenshots from our Colab test case along with code examples you can copy directly from this page. Understanding Cloudmersive NSFW ClassificationCloudmersive’s NSFW Image API investigates images for NSFW content using Deep Learning AI. It assigns each image a Cloudmersive generally considers images with scores below 0.2 safe to view and images with scores above 0.8 unsafe to view. Scores between 0.3 and 0.7 are typically assigned to images with some degree of raciness which are not necessarily unsafe. Discretion, of course, is up to the individual or enterprise implementing this solution; the scoring system allows for a wide range of conditional actions to be taken based on NSFW scan results. Each score is accompanied by a Finding Python Code Examples for the NSFW APIPython code examples for calling the NSFW API can be found on the Image API page of the Cloudmersive API Console. We can navigate to this page from the API Console tab in our Cloudmersive Management Portal (CMP).
We’ll scroll down to the NSFW path, where we’ll find the “Not safe for work NSFW racy content classification” tab. Clicking this tab will reveal a window which contains brief documentation describing the API, along with the option to scan a test file without writing any code. We’ll also find a detailed API response model in this window to help us extract important information from our API response object. At the bottom of the window, we’ll select the Python programming language tab to bring up Python code examples. These examples have taken the bulk of our work away. The Image API client library imports are already written in, the configuration and file input snippets are set to placeholder strings, and our API instance is called and printed within a Try/Except block.
We’ll find the pip command we need to install Image API resources by clicking on the As promised, we’ve included these code examples in this article. You can copy and paste directly from the code snippets below:
Testing the NSFW API in ColabWe’ll now select all the code examples we need (including the pip command), copy them to our clipboard, and paste them in our Python file (or code cell if we’re using Colab or Jupyter Notebooks). Next, we’ll copy the pip install command and run it in our terminal or code cell. Now that we have the Image API library installed in our environment, we’ll configure our API key by replacing the We’ll now move down to our next code snippet and replace the Test Case 1: Man Wearing Gray SuitIn our first test case, we’ll scan a stock image of a man wearing a gray suit. This image is rendered on the righthand side of the page in the below screenshot. This control test returns an extremely low score of 0.12. This constitutes a Test Case 2: Man Wearing Unbuttoned ShirtIn our second test case, we’ll scan a stock image of a man sitting on a chair with his shirt unbuttoned. This image is rendered on the righthand side of the page in the below screenshot. This test returns a high score of 0.70, constituting a Test Case 3: Woman Wearing Pink DressIn our third and final test case, we’ll scan a stock image of a woman wearing a pink dress. This image is rendered on the righthand side of the page in the below screenshot. This test returns a low score of 0.22, constituting a ConclusionThe Cloudmersive NSFW API is an effective Deep Learning AI tool for scoring and classifying images as NSFW. It’s easy to use in any Python application thanks to ready-to-run code examples sourced from the Cloudmersive API console. If you have any questions about implementing the NSFW API for enterprise use-cases, please feel free to contact a member of our team. |
Sign Up Now or