Using pre-trained models

Using pre-trained models

As an alternative to training your own models you can use one of the pre-trained models available. SentiSight.ai currently offers five pre-trained models - General classification, Places classification, NSFW classification, Object detection and Text recognition. You can see the list of categories included in General classification model here, categories included in Places classification here and categories included in Object detection model here. NSFW classification has only two categories "safe" and "unsafe" that simply tells you whether or not an image contains adult content. Text recognition model marks the location of the text in an image with bounding boxes similarly as in object detection and returns parsed text pieces.

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 models in the list, and select images to upload from your computer. Note that Image Similarity Search is also listed as a "pre-trained" model in the web interface, but its usage is different. Consult this guide on how to use Image Similarity Search

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

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-classification", "Places-classification", "NSFW-classification', "Object-detection" or "Text-recognition")

Use this endpoint for predictions: https://platform.sentisight.ai/api/pm-predict/{your_model_name}/

If you are using the Text recognition model, you also need to add "allowed_lang" parameter to the URL (see the section below).

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.

Classification and Object detection samples


TOKEN="your_token"
MODEL="your_model_name" # "General-classification", "Places-classification", "NSFW-classification" or "Object-detection"
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-classification" // Change to "General-classification", "Places-classification", "NSFW-classification" or "Object-detection"
       
       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/';
        let token = '';
        let predictionId;
        let results;
        let resultOutput;

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

        function apiPostRequest(request, body) {
            const 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><br><br>
<p id="output">Your results will go here!</p>
</body>
</html>
import requests

token = "your_token"
model = "your_model_name" # "General-classification", "Places-classification", "NSFW-classification" or "Object-detection"
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)

Text recognition samples

Using the text recognition (sometimes also call "Optical Character Recognition" or "OCR") model is very similar to the other models, the main difference is that you need to add "allowed_lang" parameter to the URL. Specify the code names of the languages you want to use, separating them with a comma (see code samples below). You can find the supported languages and their codes here. We recommend using English together with other languages as some of its characters often appear in the foreign text.

The REST API query will return a list of the detected text pieces with their confidence scores. To get a continuous parsed text you can simply join the results sequentially.


TOKEN="your_token"
MODEL="Text-recognition"
IMAGE_FILENAME="your_image_path"
ALLOWED_LANGUAGES="de,en"
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?allowed_lang=$ALLOWED_LANGUAGES"
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
   {
       
       String modelName = "Text-recognition";
       String allowedLanguages="de,en";
       
       byte[] bytes = Files.readAllBytes(new File(imageFilename).toPath());
       
       URL url = new URL("https://platform.sentisight.ai/api/pm-predict/" + modelName +"?allowed_lang=" + allowedLanguages);
       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/';
        let token = '';
        let allowedLanguages='de,en';
        let predictionId;
        let results;
        let resultOutput;

        function predict() {
            token = document.getElementById('tokenfield').value;
            const modelName = "Text-recognition" // Change to "General-classification", "Places-classification", "NSFW-classification" or "Object-detection"
            const input = document.getElementById('upload');
            resultOutput = document.getElementById('output');
            const file = input.files[0];
            const fr = new FileReader();
            fr.onload = function() {
                results = apiPostRequest('pm-predict/' + modelName + '?allowed_lang=' + allowedLanguages, fr.result);
                let parsedResults = JSON.parse(results);
                resultOutput.innerText = results
                console.log(parsedResults);
            }
            fr.readAsArrayBuffer(file);
        }

        function apiPostRequest(request, body) {
            const 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><br><br>
<p id="output">Your results will go here!</p>
</body>
</html>
import requests

token = "your_token"
model = "Text-recognition"
image_filename = "your_image_path"
allowed_languages="de,en"

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/{}?allowed_lang={}'.format(model, allowed_languages), 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-classification", "Places-classification", "NSFW-classification" or "Object-detection"
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-classification" // Change to "General-classification", "Places-classification", "NSFW-classification" or "Object-detection"
       
       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/';
        let token = '';
        let predictionId;
        let results;
        let resultOutput;

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

        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>
Url: <input id="url" type="text" name="" value="">
<br>
<button type="button" onclick="predict()">Predict</button>
<br><br><br>
<p id="output">Your results will go here!</p>
</body>
</html>
import requests

token = "your_token"
model = "your_model_name" # "General-classification", "Places-classification", "NSFW-classification" or "Object-detection"
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)