NAV

Clearsite

shell ruby python objective_c

Introduction

Here you will find the code required to integrate Clearsite Content Filtering into your web application or API.

We’ve included the necessary commands to make calls to our API from cURL, Ruby, Python, PHP, Javascript and Java. If you need help making calls in another language, contacts us at clearsite.io/contact.

There are two versions of each API endpoint, which can be accessed by calling the following urls:

The accuracy algorithm is optimized for photos only. It will not detect cartoon pornography accurately. The scope algorithm, on the other hand is trained to detect pornographic content in any form. If accuracy/scope is not specified, the algorithm will default to scope.

Remote Images

Analyse Remote Image

# Using curb for the request (https://github.com/taf2/curb)
require 'curb'
# To parse the response string into a hash
require 'json'

# For clarity
imagepath = "http://test.com/image.jpg"
posturl = "https://api.clearsite.io/classify_url"
api_key = "yourapikey"

# Makes request, response object (as string) is stored in json_string
json_string = Curl.post(posturl, {:url => imagepath, :api_key => api_key}).body_str

# Parse JSON string into hash
json_object = JSON.parse(json_string.strip)

# We make sure the request succeeded
if json_object["success"] == true
  porn_chance = json_object["results"]["porn"]
  not_porn_chance = json_object["results"]["not_porn"]
  # Accept or reject image
  puts porn_chance
  if porn_chance < 0.5 # This value controls leniancy. 0.5 is an advisable default
    # Allow image
  else
    # Block image, etc
  end
else
  # Handle the error
  puts json_object["reason"]
end

# To make request
import requests
# To parse response into dictionary
import json

# For clarity
api_key = "yourapikey"
posturl = "http://api.clearsite.io/classify_url"
imageurl = "http://test.com/image.jpg"

# Makes request, returns response object
response_object = requests.post(posturl, data = {"api_key": api_key, "url": imageurl})

# Parses response object into dictionary
json_dictionary = json.loads(response_object.text)

# Confirm successful call
if json_dictionary["success"] is True:
  porn_chance = json_dictionary["results"]["porn"]
  not_porn_chance = json_dictionary["results"]["not_porn"]
  # Accept or reject image
  print porn_chance
  if porn_chance < 0.5: # This value controls leniancy. 0.5 is an advisable default
    # Allow image
  else:
    # Block image
else:
  # Handle error
  print json_dictionary["reason"]
curl -F api_key=yourapikey -F url=http://test.com/image.jpg https://api.clearsite.io/classify_url
// Code snippet one day

Once you have your API Key ready, you can analyse images at a remote URL using the code to the right.

The API takes a maximum image size of 8MB. If your image is larger than 8MB, the response will be a ‘413 Reuqest Entity Too Large’ error.

Allowed image extensions are: ’.png’, ’.bmp’, ’.jpg’, ’.jpe’, ’.jpeg’, and ’.gif’.

If successful, your call to our API will return a JSON object (as a string) structured like this:

{
  "success": True,
  "results":
  {
    "porn": 0.95952,
    "not_porn": 0.04048
  }
}

If unsuccessful, the JSON object will be formatted as such:

{
  "success": False,
  "reason": "Invalid image format."
}

Local Images

Analyse Local Image

# Using curb for the request (https://github.com/taf2/curb)
require 'curb'
# To parse the response string into a hash
require 'json'

# For clarity
imagepath = "/path/to/image.jpg"
posturl = "https://api.clearsite.io/classify_url"
api_key = "yourapikey"

# Makes request, response object (as string) is stored in json_string
c = Curl::Easy.new(posturl)
c.multipart_form_post = true
c.http_post(Curl::PostField.file('image', imagepath), Curl::PostField.content('api_key', api_key))

json_string = Curl.post(posturl, {:url => imagepath, :api_key => api_key}).body_str

# Parse JSON string into hash
json_object = JSON.parse(json_string.strip)

# We make sure the request succeeded
if json_object["success"] == true
  porn_chance = json_object["results"]["porn"]
  not_porn_chance = json_object["results"]["not_porn"]
  # Accept or reject image
  puts porn_chance
  if porn_chance < 0.5 # This value controls leniancy. 0.5 is an advisable default
    # Allow image
  else
    # Block image, etc
  end
else
  # Handle the error
  puts json_object["reason"]
end

# To make request
import requests
# To parse response into dictionary
import json

# For clarity
api_key = "yourapikey"
posturl = "http://api.clearsite.io/classify_upload"
imagepath = "/path/to/image.jpg"
files = {'image': open(imagepath, 'rb')}

# Makes request, returns response object
response_object = requests.post(posturl, data = {"api_key": api_key}, files=files)

# Parses response object into dictionary
json_dictionary = json.loads(response_object.text)

# Confirm successful call
if json_dictionary["success"] is True:
  porn_chance = json_dictionary["results"]["porn"]
  not_porn_chance = json_dictionary["results"]["not_porn"]
  # Accept or reject image
  print porn_chance
  if porn_chance < 0.5: # This value controls leniancy. 0.5 is an advisable default
    # Allow image
  else:
    # Block image
else:
  # Handle error
  print json_dictionary["reason"]
curl -F api_key=yourapikey -F image=@/path/to/image.jpg https://api.clearsite.io/classify_upload
// Code snippet one day

Once you have your API Key ready, you can analyse local images using the code to the right.

The API takes a maximum image size of 8MB. If your image is larger than 8MB, the response will be a ‘413 Reuqest Entity Too Large’ error.

Allowed image extensions are: ’.png’, ’.bmp’, ’.jpg’, ’.jpe’, ’.jpeg’, and ’.gif’.

If successful, your call to our API will return a JSON object (as a string) structured like this:

{
  "success": true,
  "results":
  {
    "porn": 0.95952,
    "not_porn": 0.04048
  }
}

If unsuccessful, the JSON object will be formatted as such:

{
  "success": false,
  "reason": "Invalid image format."
}

Base64 Encoded Images

Analyse Base64 Encoded Image

// Code snippet one day
// Code snippet one day
// Code snippet one day
- (IBAction)contentFiltering:(UIButton *)sender {

    // Generate required information (string 'post' and int 'postLength')
    UIImage* image = <UIIMAGE OBJECT>;
    NSString *api_key = @"<YOUR API KEY>";
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    NSString *image = [imageData base64EncodedStringWithOptions:0];
    NSString *post = [NSString stringWithFormat:@"api_key=%@&image=%@",api_key,image];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%i",(int)[postData length]];

    // Create request object
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"https://api.clearsite.io/classify_base64"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    // Create connection object
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    // When this is successful, it calls the method below (connection didReceiveData)
    if(conn) {
        NSLog(@"Connection Successful");
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

    // Parse response JSON object into dictionary
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSLog(@"%@", json);

    // Get chance from dictionary
    NSString *float_string = json[@"results"][@"porn"];
    CGFloat porn_chance = (CGFloat)[float_string floatValue];

}

On some platforms it’s easier to submit an image as a base64 encoded string.

If successful, your call to our API will return a JSON object (as a string) structured like this:

{
  "success": true,
  "results":
  {
    "porn": 0.95952,
    "not_porn": 0.04048
  }
}

If unsuccessful, the JSON object will be formatted as such:

{
  "success": false,
  "reason": "Invalid image format."
}

Authorization

When you sign up for Clearsite, you’re issued an API Key which can be found at your dashboard.

Your API Key should look something like the following:

286235408295559218449452302615809254284

Keep your API Key secret. With it, others will be able to spend your clearsite credits.