Annotating Previews

In this tutorial, we will walk you through how to annotate images and search through annotated images. Code samples with be shown using Java via the Android Platform. We will need a library for making asynchronous network requests, so we will use Android AsyncHttp. http://loopj.com/android-async-http/.

The first step is to log in to the Eagle Eye system, through the authentication and authorization API calls. AsyncHttpClient is part of the async-http library and is used as the entry point for making network requests. It is good practice to have no more than one instance of the AsyncHttpClient class as it will save authentication cookies, which we want for subsequent API requests. To log in, make an /aaa/authenticate request followed by a /aaa/authorize request.

[java]

AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

public void authenticate(String username, String password) {
String url_aaa_authenticate = “https://login.eagleeyenetworks.com/g/aaa/authenticate”;

RequestParams rp = new RequestParams();
rp.put(“username”, username);
rp.put(“password”, password);

asyncHttpClient.post(url_aaa_authenticate, rp, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

String token = “”;
try {
token = (String) response.get(“token”);
} catch (JSONException e) {
e.printStackTrace();
}

authorize(token);
}

@Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable error) {}
});
}

public void authorize(String token) {
String url_aaa_authorize = “https://login.eagleeyenetworks.com/g/aaa/authorize”;

RequestParams rp = new RequestParams();
rp.put(“token”, token);

asyncHttpClient.post(url_aaa_authorize, rp, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {}

@Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable error) {}
});
}[/java]

Now that we are logged in, we will select a camera to annotate by retrieving a list of all camera devices. We will filter our /device/list API call so that we only get devices of type ‘camera’ by using the ’t’ parameter. Then we will pull the first camera from the returned JSONArray (The chosen camera is arbitrary for this example).

[java] public void deviceList() {
String url_device_list = “https://login.eagleeyenetworks.com/g/device/list”;

RequestParams rp = new RequestParams();
rp.put(“t”, “camera”);

asyncHttpClient.get(url_device_list, rp, new JsonHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {

try {
if(response.length() > 0) {
JSONArray cameraAsJSONArray = (JSONArray) response.get(0);

String cameraId = cameraAsJSONArray.getString(1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable error) {
}
});
}[/java]

Once we have the camera of interest, we are ready to annotate. Annotating requires a known device_id and timestamp, so for the purpose of this tutorial, we will annotate the last 6 images on the server using the /asset/list/image API call. We will set the start_timestamp to now and the count to -6, which will retrieve data for the last 6 image entries.

[java] public void assetListImage(String id) {

String url_asset_list_image = String.format(“https://login.eagleeyenetworks.com/asset/list/image?c=%s&start_timestamp=now&count=-6&asset_class=all”, id);

asyncHttpClient.get(url_asset_list_image, new JsonHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {

if(response.length() > 0) {
try {
JSONObject imageDataAsJson = response.getJSONObject(0);
String timestamp = imageDataAsJson.getString(“s”);

} catch (JSONException e) {
e.printStackTrace();
}

}
}

@Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable error) {}
});
}[/java]

Now that we have data for all the images, we can extract the timestamp from them to send to the ‘annotations’ API. We will annotate each image with json data.

{
“info”: “These images are used in the Eagle Eye annotations example, and represent the last 6 images from the now timestamp”
}

Now we have all the information needed to make the API calls. We will iterate through all the images and and annotate each one. This API is a little trickier to set up due to it being a ‘PUT’ request which cannot be URL encoded. Below shows a working Android way of sending the request. You will notice that the ‘Context’ parameter is required, which is Android specific.

[java] public void annotationPut(Context context, String device_id, String timestamp) {
String url_annotation = “https://login.eagleeyenetworks.com/g/annotation”;

RequestParams rp = new RequestParams();
JSONObject requestParamsAsJson = new JSONObject();
try {
requestParamsAsJson.put(“device_id”, device_id);
requestParamsAsJson.put(“timestamp”, timestamp);

JSONObject dataAsJson = new JSONObject();
dataAsJson.put(“info”, “These images are used in the Eagle Eye annotations example, and represent the last 6 images from the now timestamp”);
requestParamsAsJson.put(“data”, dataAsJson);

rp.put(“device_id”, device_id);
rp.put(“timestamp”, timestamp);
rp.put(“data”, new JSONObject());

} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

StringEntity stringEntity = null;
try {
stringEntity = new StringEntity(requestParamsAsJson.toString());
stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, “application/json”));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}

asyncHttpClient.put(context, url_annotation, stringEntity, “application/json”, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {}
});
}[/java]

Now that our images have the desired annotations, we can come back at anytime and retrieve these images using the /search/annotations API. This API takes in a ‘value’ parameter which is the search parameter and a time range.

[java] public void searchAnnotations(String value, String start_timestamp) {
String url_search_annotations = “https://login.eagleeyenetworks.com/g/search/annotations”;

String startTimestampAdjusted = UtilDateTime.getTimestampMinusXDays(start_timestamp, 1);

RequestParams rp = new RequestParams();
rp.put(“value”, value);
rp.put(“start_timestamp”, startTimestampAdjusted);

asyncHttpClient.get(url_search_annotations, rp, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {

}

@Override
public void onFailure(int statusCode, Header[] headers, String response, Throwable error) {
}
});
}[/java]

We have now finished the entire flow of annotating an image. Use this knowledge and make your Eagle Eye app amazing.