Getting Started with Java

Prerequisites

You must have registered here to generate your API credentials. Angus Java SDK need Java7 runtime.

1. Download Angus SDK library

You can download the library from github: here.

2. Configure your SDK

You must configure your sdk with the keys you received by creating a stream here here. These keys are used to authenticate the requests you are about to send.

Your API credentials can be retrieved by clicking on “Show details” on your console interface as shown below:

../_images/console_cred.png

In a terminal, type:

$ java -jar angus-sdk-java-0.0.2-jar-with-dependencies.jar
Please copy/paste your client_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Please copy/paste your access_token: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Fill in the “client_id” prompt with the “login” given on the interface and the “access_token” prompt with the “password” given on the interface.

You could explore all options by typing:

$ java -jar angus-sdk-java-0.0.2-jar-with-dependencies.jar --help

3. Access your sensor stream

Angus.ai API is specifically designed to process a video stream. This section will show you a way to access the stream of a webcam plugged to your computer by using webcam-capture.

Note that the following code sample can be adapted to process a video file instead.

Note also that webcam-capture is not an absolute pre-requisite, the following code sample can easily be adapted to be used with any other way of retrieving successive frames from a video stream. If you need assistance, please contact us at contact@angus.ai

Prerequisite

  • you have a working web cam plugged into your PC

We use here webcam-capture to get the camera images.

Get the zip files with dependencies here and unzip it.

Copy this code snippet in a file named DisplayCamera.java.

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;

public class DisplayCamera {

    public static void main(String[] args) throws IOException {

        // get default webcam and open it
        Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(WebcamResolution.QVGA.getSize());
        webcam.open();

        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setSize(320, 240);
        JLabel lbl = new JLabel();
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            // get image
            BufferedImage image = webcam.getImage();

            ImageIcon icon = new ImageIcon(image);
            lbl.setIcon(icon);
        }

    }
}

Compile it (change path to webcam-capture dependency accordingly):

$ javac -cp "angus-sdk-java-0.0.2-jar-with-dependencies.jar:webcam-capture-0.3.10.jar:." DisplayCamera.java

Check that your web cam video stream is correctly displayed on your screen. Make sure that the path to webcam-capture dependencies (jar file and libs directory) is correct:

$ java -cp "angus-sdk-java-0.0.2-jar-with-dependencies.jar:webcam-capture-0.3.10.jar:libs/*:." DisplayCamera
../_images/gwenn_onwebcam.png

4. Send this stream to Angus scene analysis API

We can now extend this code to have Angus process this stream.

Note that we use the method enable_session() and disable_session() to let the service know that the successive images received are from the same stream. This allows overall better performances as previous calls are used to refine the results.

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.TimeZone;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.json.simple.JSONObject;

import ai.angus.sdk.Job;
import ai.angus.sdk.ProcessException;
import ai.angus.sdk.Root;
import ai.angus.sdk.Service;
import ai.angus.sdk.impl.ConfigurationImpl;
import ai.angus.sdk.impl.File;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;

public class StreamSceneAnalysis {

    public static void main(String[] args) throws IOException, ProcessException {
        ConfigurationImpl conf = new ConfigurationImpl();

        Root root = conf.connect();
        Service service = root.getServices().getService(
                "scene_analysis", 1);

        // get default webcam and open it
        Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(WebcamResolution.QVGA.getSize());
        webcam.open();

        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setSize(320, 240);
        JLabel lbl = new JLabel();
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
        df.setTimeZone(tz);

        service.enableSession();
        while (true) {
            // get image
            BufferedImage image = webcam.getImage();

            ImageIO.write(image, "PNG", new java.io.File("/tmp/tmp.png"));
            JSONObject params = new JSONObject();
            params.put("image", new File("/tmp/tmp.png"));
            params.put("timestamp", df.format(new Date()));
            Job job = service.process(params);
            System.out.println(job.getResult().toJSONString());

            ImageIcon icon = new ImageIcon(image);
            lbl.setIcon(icon);
        }
        service.disableSession();
    }
}

You should see API output on standard output.

5. Congratulations!

You have made your first call to Angus.ai. The API can be used to infer age, emotion, counting people in field of view, etc...

What to do next?

Congratulations!