ProjectWT
Published in

ProjectWT

Detecting Adult Porn Content with Azure Cognitive Services in .NET C#

Photo by Dainis Graveris on Unsplash

A client has been lately making me study and do more research into Azure cognitive services. We are working with text to speech, speech to text and also investigating OCR as well as image analysis.

Previously, I spent some time looking at the Text to Speech conversion capabilities.

Now, I am looking at using image analysis. The image analysis can do lots of stuff.

As usual, the full code, is available here.

The Image Analysis does in fact detect Adult and Porn. It also detects lot of other things. A sample output looks something like this.

Analyzing the image Gabrellia_nude.jpg...Summary:
a woman posing for a picture with confidence 0.5539060235023499
Categories:
abstract_nonphoto with confidence 0.31640625
people_ with confidence 0.35546875
Tags:
stomach 0.971166729927063
person 0.9686612486839294
chest 0.9619669914245605
art model 0.9266955852508545
human face 0.9123901724815369
abdomen 0.9048451781272888
nude photography 0.9032716751098633
undergarment 0.8903558254241943
muscle 0.8704043626785278
navel 0.8661021590232849
model 0.8634390830993652
woman 0.7494900822639465
underpants 0.7208518385887146
girl 0.5744295120239258
Faces:Adult:
Has adult content: True with confidence 0.9971476197242737
Has racy content: False with confidence 0.9980443120002747
Has gory content: False with confidence 0.0008260256145149469

So, how to make this work. .

Step One

First up, we will need some nude images, of course. Fortunately, you can find nude images on Wikipedia.

For racy images, I would recommend unsplash. Just search for sexy or bikini and you will get all the racy images are you want.

You can also get gory images to test, but I have no idea where to get them. I mean, I am sure Wikipedia has it, but they would be too scary for me.

Step Two

Setup your Azure Computer Vision key on the azure portal. There is a free tier, which is good enough.

I used the free tier while testing this code.

Step Three

Right then. Using the Cognitive Services API is fairly straightforward. Microsoft has made it very simple use them. The documentation is just amazing. So, is the GitHub issue section of their repositories where I always get prompt responses.

So, we start by getting the client.

public void Authenticate()
{
client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
{ Endpoint = endpoint };
}

public ImageConfiguration()
{
endpoint = "https://ocrdemo1forstudents1.cognitiveservices.azure.com/";
key = "c11703ea5fe04a9094ab94922c2dfea4";
}

Then, you pick the features you are trying to analyse.

List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>()
{
VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
VisualFeatureTypes.Objects
};

After this, it’s a simple matter of passing your file into a stream. Let Cognitive services do it’s magic.

ImageAnalysis results;
var file = File.OpenRead(urlFile); //get a stream
using (var stream = file)
{
results = await client.AnalyzeImageInStreamAsync(stream, visualFeatures: features);
}

You can filter the results. For example, to see adult content results, you can do something like this.

// Adult or racy content, if any.
Console.WriteLine("Adult:");
Console.WriteLine($"Has adult content: {results.Adult.IsAdultContent} with confidence {results.Adult.AdultScore}");
Console.WriteLine($"Has racy content: {results.Adult.IsRacyContent} with confidence {results.Adult.RacyScore}");
Console.WriteLine($"Has gory content: {results.Adult.IsGoryContent} with confidence {results.Adult.GoreScore}");
Console.WriteLine();

That is all there is to it.

I work as a full time freelance coding tutor. Hire me at UpWork or Fiverr or Stack Overflow. My personal website is here. Find more of my art at Behance and Unsplash. Also, I have a Podcast. Also, I am on substack.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jay

Jay

179 Followers

Freelance Tutor — Coding, Generative Art and Photoshop. Full Name : Vijayasimha BR