Using pre-trained classification models

As an alternative to training your own models you can use one of the pre-trained models available. SentiSight.ai currently offers three pre-trained classification models - General classification, Places classification, and NSFW classification. You can see the list of categories included in General classification model here, and categories included in Places classification here. NSFW classification simply tells you whether or not an image contains adult content.

There are two main options to use a pre-trained model: via SentiSight.ai web platform or via REST API.

Using pre-trained models via SentiSight.ai web platform

To use a pre-trained model press "Pre-trained models", choose one of the three classification models*, and select images to upload from your PC.

You will see the results on the browser screen. There are three options to download the results:

  • As a .zip file with images grouped into folders by the predicted label
  • In a .json format
  • In a .csv format

* - Note that Similarity Search is also listed as a "pre-trained" model, but it is not a classification model. Consult this guide on how to use Similarity Search

Using pre-trained models via SentiSight.ai REST API

To begin using a pre-trained model via REST API you will need these details:

  • API token (available under "User profile" menu tab)
  • Model name (either "General", "Places" or "NSFW')

For image classification use this endpoint: https://platform.sentisight.ai/api/pm-predict/{your_model_name}/


Using an image from your computer

Set the "X-Auth-token" header to your API token string and set "Content-Type" header to "application/octet-stream". Set the body to your image file.

For more details, see the code samples below.


TOKEN="your_token"
MODEL="your_model_name" # "General", "Places" or "NSFW"
IMAGE_FILENAME="your_image_path"
curl -H "X-Auth-token: $TOKEN" --data-binary @"$IMAGE_FILENAME" \
  -H "Content-Type: application/octet-stream" \
  -X POST "https://platform.sentisight.ai/api/pm-predict/$MODEL"
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;

public class App
{
   public static void main( String[] args ) throws IOException
   {
       if (args.length < 4) {
           System.out.println("Usage: java -jar sample.jar api_token file");
       }
       String token = args[0];
       String imageFilename = args[1];
       
       String modelName = "General" // Change to "General", "Places" or "NSFW"
       
       byte[] bytes = Files.readAllBytes(new File(imageFilename).toPath());
       
       URL url = new URL("https://platform.sentisight.ai/api/pm-predict/" + modelName);
       HttpURLConnection connection = (HttpURLConnection)url.openConnection();        
       connection.setRequestProperty("Content-Type", "application/octet-stream");
       connection.setRequestProperty("X-Auth-token", token);
       connection.setRequestMethod("POST");
       connection.setDoOutput(true);
       DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(bytes);
        wr.flush();
        wr.close();
        
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String output;
        StringBuffer response = new StringBuffer();

        while ((output = in.readLine()) != null) {
            System.out.println(output);
            response.append(output);
        }
        in.close();
   }
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
	<head>
		<meta charset="utf-8">
		<title>Sample</title>
		<script type="text/javascript">
			const baseApiURL = 'https://platform.sentisight.ai/api/';
			var token = '';
			var predictionId;
			var results;

			function predict() {
				token = document.getElementById('tokenfield').value;
				const modelName = "General" // Change to "General", "Places" or "NSFW"
				var input = document.getElementById('upload');
				const file = input.files[0];
				var fr = new FileReader();
				fr.onload = function() {
					results = JSON.parse(apiPostRequest('pm-predict/' + modelName, fr.result));
					console.log(results);
				}
				fr.readAsArrayBuffer(file);
			}

			function apiPostRequest(request, body) {
				var xmlHttp = new XMLHttpRequest();
				xmlHttp.open( "POST",  baseApiURL + request, false );
				xmlHttp.setRequestHeader('Content-Type', 'application/octet-stream');
				xmlHttp.setRequestHeader('X-Auth-token', token);
				xmlHttp.send(body);
				console.log(xmlHttp.responseText);
				return xmlHttp.responseText;
			}
		</script>
	</head>
	<body>
		Token: <input id="tokenfield" type="text" name="" value="">
		<br>
		Upload image: <input id="upload" type="file" name="" value="">
		<br>
		<button type="button" onclick="predict()">Predict</button>
		<br>
	</body>
</html>
import requests

token = "your_token"
model = "your_model_name" # "General", "Places" or "NSFW"
image_filename = "your_image_path"

headers = {"X-Auth-token": token, "Content-Type": "application/octet-stream"}

with open(image_filename, 'rb') as handle:
    r = requests.post('https://platform.sentisight.ai/api/pm-predict/{}/{}/'.format(model), headers=headers, data=handle)

if r.status_code == 200:
    print(r.text)
else:
    print('Error occured with REST API.')
    print('Status code: {}'.format(r.status_code))
    print('Error message: ' + r.text)

Using an image URL

Using the REST API by providing an image URL is very similar to the previous case of using REST API by uploading an image. The only differences are that you need to set the "Content-Type" header to "text/plain" and set the body to your image URL.

For more details, see the code samples below.


TOKEN="your_token"
MODEL="your_model_name" # "General", "Places" or "NSFW"
IMAGE_URL="your_image_url"
curl -H "X-Auth-token: $TOKEN" --data "$IMAGE_URL" \
  -H "Content-Type: text/plain" \
  -X POST "https://platform.sentisight.ai/api/pm-predict/$MODEL"
package sentisight.api.predict.sample;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class App
{
   public static void main( String[] args ) throws IOException
   {
       if (args.length < 4) {
           System.out.println("Usage: java -jar sample.jar api_token url");
       }
       String token = args[0];
       String image_url = args[1];

       String modelName = "General" // Change to "General", "Places" or "NSFW"
       
       URL url = new URL("https://platform.sentisight.ai/api/pm-predict/" + modelName);
       HttpURLConnection connection = (HttpURLConnection)url.openConnection();        
       connection.setRequestProperty("Content-Type", "text/plain");
       connection.setRequestProperty("X-Auth-token", token);
       connection.setRequestMethod("POST");
       connection.setDoOutput(true);
       DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(image_url);
        wr.flush();
        wr.close();
        
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String output;
        StringBuffer response = new StringBuffer();

        while ((output = in.readLine()) != null) {
            System.out.println(output);
            response.append(output);
        }
        System.out.println(connection.getResponseCode());
        in.close();
   }
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
	<head>
		<meta charset="utf-8">
		<title>Sample</title>
		<script type="text/javascript">
			const baseApiURL = 'https://platform.sentisight.ai/api/';
			var token = '';
			var predictionId;
			var results;

			function predict() {
				token = document.getElementById('tokenfield').value;
				const modelName = "General" // Change to "General", "Places" or "NSFW"
				const url = document.getElementById('url').value;
				results = JSON.parse(apiPostRequest('pm-predict/' + modelName, url));
				console.log(results);
			}

			function apiPostRequest(request, body) {
				var xmlHttp = new XMLHttpRequest();
				xmlHttp.open( "POST",  baseApiURL + request, false );
				xmlHttp.setRequestHeader('Content-Type', 'text/plain');
				xmlHttp.setRequestHeader('X-Auth-token', token);
				xmlHttp.send(body);
				console.log(xmlHttp.responseText);
				return xmlHttp.responseText;
			}
		</script>
	</head>
	<body>
		Token: <input id="tokenfield" type="text" name="" value="">
		<br>
		Model name: <input id="model" type="text" name="" value="">
		<br>
		Url: <input id="url" type="text" name="" value="">
		<br>
		<button type="button" onclick="predict()">Predict</button>
		<br>
	</body>
</html>
import requests

token = "your_token"
model = "your_model_name" # "General", "Places" or "NSFW"
image_url = "http://your-image-url.png"

headers = {"X-Auth-token": token, "Content-Type": "text/plain"}

r = requests.post('https://platform.sentisight.ai/api/pm-predict/{}/{}/'.format(model), headers=headers, data = image_url)

if r.status_code == 200:
    print(r.text)
else:
    print('Error occured with REST API.')
    print('Status code: {}'.format(r.status_code))
    print('Error message: ' + r.text)