Content Moderation API featured

You might have an application with huge amount of user generated content coming daily and need to moderate content automatically. It’s a pain to moderate content manually because it take lots of man hours. How about using artificial intelligence for that? It will instantly detect inappropriate content prevent form going public.

What is the Content Moderation API by DeepAI?

DeepAI is a very popular source among artificial intelligence content seekers. It provide researches, guides, news and APIs of artificial intelligence. You can easily grab an API key by creating an account and access all of their artificial intelligence APIs. Just visit this link, create an account and then go to the dashboard and you will have your API key.

https://deepai.org/

Is it free to use Deep.ai automated content moderation?

When you register, you will have $5 USD free credit which is equivalent to 10,000 requests. No credit card required for the registration. After the registration, you only need to pay $0.50 USD per 1000 requests or there are some enterprise packages you can discuss with them.

What Deep.ai artificial intelligence APIs are available to use in my Delphi application?

They provide numerous amount of APIs. You can check their APIs form this list. Most of them also have a live demo in their product page.

https://deepai.org/apis

One of their powerful API is the “Content Moderation API”. This API give you the all details to moderate the content. Once you supply and image to the API, it will provide you “Not safe for work” score, description about inappropriate content, the rectangle area of the inappropriate content, and the confidence score which is one if 100% confidence. This API can detect adult content, hate symbols, guns and offensive words.

How do I use the Content Moderation API?

We can access all of the DeepAI APIs using a REST client including this content moderation API. A simple curl request for this API is like this:

curl \
    --data 'image=YOUR_IMAGE_URL' \
    -H 'api-key:API_KEY' \
    https://api.deepai.org/api/content-moderation 

It’s self-explanatory and you have provide the image URL or local image file as “image” and the API key as a header value. Once you send the request, server will send a JSON response and you can parse it to have all moderation information about the image. Sample moderation would be like this:

{
    "id": "UNIQUE_ID",
    "output": {
        "detections": [{
                "confidence": "0.83",
                "bounding_box": [
                    118,
                    89,
                    21,
                    29
                ],
                "name": "Female Breast - Exposed"
            },
            {
                "confidence": "0.92",
                "bounding_box": [
                    142,
                    92,
                    27,
                    27
                ],
                "name": "Female Breast - Exposed"
            },
            {
                "confidence": "0.54",
                "bounding_box": [
                    168,
                    153,
                    19,
                    30
                ],
                "name": "Male Genitalia - Exposed"
            }
        ],
        "nsfw_score": 0.9997197985649109
    }
}

You have to parse the JSON array inside the JSON to get the results.

How do I access the Deep.ai Content Moderation API from Delphi?

We can use REST client component to access the this API and easily build a demo application. Let’s make a demo application which will draw a rectangle if one of the restricted content found. First add a TRESTClient, TRESTRequest and TRESTResponse components in to a form. Set the base URL of the client to the following:

https://api.deepai.org/api/content-moderation

Set the method of RESTRequest to rmPOST because we going to securely post the data. Then it’s the coding time. We need to set the image, API key, get the response and JSON parse in the code. In this example we will browse a local file and send the request. Once we parsed the response, we draw a rectangle in the canvas of the TImage.

Here is a Delphi code example for using Deep.ai with a REST API

procedure TfrmMain.btnModerateClick(Sender: TObject);
var
  lparam : Trestrequestparameter;
  jsonObj: TJSONObject;
  JSonValue  : TJSonValue;
  JSONArray, JSONArrayBound: TJSONArray;
  strJson: string;
  canv: TCanvas;
  i, posTop, posLeft, posWidth, posHeight: Integer;
begin
  lblWait.Visible := true;
  Application.ProcessMessages;
  RESTRequest.Params.Clear;
  RESTResponse.RootElement := '';
  lparam := RESTRequest.Params.AddItem;
  lparam.name := 'api-key';
  lparam.Value := edtAPIKey.Text;
  lparam.ContentType := ctNone;
  lparam.Kind := pkHTTPHEADER;
  lparam.Options := [poDoNotEncode];

  lparam := RESTRequest.Params.AddItem;
  lparam.name := 'image';
  lparam.Value := strImgPath;
  lparam.ContentType := ctNone;
  lparam.Kind := pkFile;
  lparam.Options := [poDoNotEncode];

  RESTRequest.Execute;
  if not RESTResponse.Status.Success then
    showmessage(RESTResponse.StatusText + ' ' +
      inttostr(RESTResponse.StatusCode))
  else
  begin
    canv := imgModerated.Canvas;
    canv.Pen.Color := clRed;
    canv.Pen.Width := 5;

    jsonObj := RESTResponse.JSONValue as TJSONObject;
    JSonValue := jsonObj.Get('output').JsonValue;
    JSONArray := JSonValue.GetValue<TJSONArray>('detections');

    for I := 0 to JSONArray.Count -1 do
    begin
      JSONArrayBound := JSONArray.Items[I].GetValue<TJSONArray>('bounding_box');
      posTop :=  strtoint(JSONArrayBound.Items[1].Value);
      posLeft :=  strtoint(JSONArrayBound.Items[0].Value);
      posWidth :=  strtoint(JSONArrayBound.Items[3].Value);
      posHeight :=  strtoint(JSONArrayBound.Items[2].Value);

      canv.MoveTo(posLeft,posTop);
      canv.LineTo(posLeft + posWidth, posTop);

      canv.MoveTo(posLeft + posWidth, posTop);
      canv.LineTo(posLeft + posWidth, posTop + posHeight);

      canv.MoveTo(posLeft + posWidth, posTop + posHeight);
      canv.LineTo(posLeft, posTop + posHeight);

      canv.MoveTo(posLeft, posTop + posHeight);
      canv.LineTo(posLeft,posTop);

      canv.TextOut(posLeft,posTop, JSONArray.Items[I].GetValue<string>('name'));
    end;
  end;
  lblWait.Visible := false;
end;

Parsing JSON results from Deep.ai

In this code we add two parameters, “api-key” and “image”, execute the request, and if success, parse the JSON value. Then if any inappropriate content found, it will draw a red rectangle in the canvas of the TImage. Final result is like this:

Content Moderation API

You can also download the demo application from this link and use with your API key.

close

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition