Is this API the fastest, easiest way to add cost-effective image analysis to your apps?

To add cost-effective image analysis to your applications, this article will walk you through Azure API, how to obtain a subscription key to access Azure API, and how to make requests to Azure REST API using an IDE software with REST Client Feature.

What is an Azure API?

Azure is a cloud computing platform offered by Microsoft with more than 200 available services. Azure provides various client libraries and a REST API to access those services. There’s a free tier to get you up and running which allows you to access several services free many of which remain free as long as you don’t exceed some quite generous monthly limits. We are going to use the “Cognitive Search” service to perform Image analysis. The Cognitive Search service is an always-free tier so you should never need to pay for it.

How do we get a subscription key to access the Azure API?

To access the Azure REST API, we need a subscription key. A subscription key is a key assigned to a ‘resource’. So we need to create a resource first. To create a resource, you need an active Azure Subscription. Please visit this link to create a subscription:

https://azure.microsoft.com/free/cognitive-services/

Once you have the Azure subscription set up you have to create a Computer Vision Resource. Make sure you have selected the correct geographical region because you cannot change this later. Visit this link to create the resource:

https://portal.azure.com/#create/Microsoft.CognitiveServicesComputerVision

To get the Subscription key of your resource, open the resource and go to “Keys and Endpoints”. There you can see two keys and the location which needs to make our REST API call.

How to make a request to Azure REST API using Delphi REST Client?

Calling the REST API from Delphi Client is easy. You just need to set few parameters and you can execute it even in design time. Let make a DEMO application. First drag and drop these components in to a forum.

TRESTClient

TRESTRequest

TRESTResponse

Also, place some input controls to get the API URL, subscription key and the image URL. Place a TButton to start the request. The content type of the REST Client must be “application/json“. Now set the client and response properties of the TRESTRequest component to the client and response components we placed earlier. Also set the method to “rmPOST“.

The code for the Start button looks like this:

procedure TfrmMain.btnStartClick(Sender: TObject);
 var
   lparam : Trestrequestparameter;
   imgProcessed: bool;
   jsonObj, jsonStatus: TJSONObject;
 begin
   memResponse.Lines.Clear;
   RESTClient.BaseURL := edAPIURL.Text;
   RESTRequest.Method:=rmpost;
   imgProcessed := false;
   try
     RESTRequest.Params.Clear;
     RESTResponse.RootElement := '';
     lparam := RESTRequest.Params.AddItem;
     lparam.name := 'Ocp-Apim-Subscription-Key';
     lparam.Value := edSubKey.Text;
     lparam.ContentType := ctNone;
     lparam.Kind := pkHTTPHEADER;
     //This one is Important otherwise the '==' will get url encoded
     lparam.Options := [poDoNotEncode];
     lparam := RESTRequest.Params.AddItem; 
     lparam.name := 'data'; 
     lparam.Value := '{"url":"' + edImgURL.Text + '"}'; 
     lparam.ContentType := ctAPPLICATION_JSON; 
     lparam.Kind := pkGETorPOST; 
     RESTRequest.Execute; 
     if not RESTResponse.Status.Success then   showmessage(RESTResponse.StatusText + ' ' +  inttostr(RESTResponse.StatusCode)) 
else 
begin   
    memResponse.Lines.Add(RESTResponse.JSONText); 
end;
finally
end;
end;

This code will add new parameter to the TRESTRequest and set the subscription key. The subscription key must pass as an HTTP header and the name should be “Ocp-Apim-Subscription-Key“. The value parameter is the subscription key we copied earlier. Also it’s important to include the option “poDoNotEncode“, otherwise it will encode the key. Also the base URL of the TRESTClient needs to be set. When we enter the base URL of the Azure Client, we must consider the region of our resource. It should be the subdomain of the URL. The base URL will look like this.

https://eastus.api.cognitive.microsoft.com/vision/v3.2/analyze?visualFeatures=Description

Replace “eastus” with your region or it will not work. Also there are plenty of parameters you can pass by this URL. Please visit this link to find all parameters.

https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/vision-api-how-to-topics/howtocallvisionapi

In this demo we passed “visualFeatures=Description” which will describe our image in one sentence. Also we have to pass our image URL in JSON format. To do that, add new parameter to the TRESTRequest and set the content type to “ctAPPLICATION_JSON” , set name to “url” and the value in this format:

{"url":"IMAGE_URL"}

Our final application should look like this.

image analysis API in action

You can download the DEMO application from GitHub: https://github.com/checkdigits/ImageAnalysisClient_example


Well, try it for yourself and see – is it the fastest way to add image analysis to your Windows, macOS and mobile applications?