![smartmockupsknpcy975 | Embarcadero RAD Studio Delphi C++Builder Blogs Google AI vision article hero image](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/smartmockups_knpcy975-6277211.jpg?resize=1140%2C694&ssl=1)
The AI Vision API from Google Cloud provides powerful pre-trained machine learning models that you can easily use in your desktop and mobile applications via REST or RPC API method calls. Assume you want your app to detect objects, locations, activities, animal species, and products, or you want it to detect not only faces but also their emotions. Or you may require the ability to read printed or handwritten text; all of this and much more is possible for free (up to the first 1000 units/month per feature) or at very low prices that are scalable to the use you make with no upfront commitments.
Table of Contents
“Detect Labels” – an ordinary name for some extraordinary AI
The option to “Detect Labels” is part of the AI Vision API that we can use to detect and extract information about entities in an image across a broad group of categories. With that information, we can identify general objects, locations, activities, animal species, products, and more.
We can use RAD Studio Delphi to easily set up a REST client library to take advantage of Google Cloud’s Vision API to empower our desktop and mobile applications. If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format.
Our RAD Studio and Delphi applications will be able to call the API and perform the detection on a local image file by sending the contents of the image file as a base64 encoded string in the body of the request. Alternatively, it can use an image file located in Google Cloud Storage, or on the Web, without the need to send the contents of the image file in the body of your request.
How do I set up Google’s Cloud Vision Label Detection AI API?
Make sure you refer to Google Cloud Vision API documentation; specifically the “Detect Label” section (https://cloud.google.com/vision/docs/labels), but, in general, this is what you need to do on the Google server-side:
- Visit https://cloud.google.com/vision and login with your Gmail account.
- Create or select a Google Cloud Platform (GCP) project.
- Enable the Vision API for that project.
- Enable the Billing for that project.
- Create an API Key credential.
AI vision in action!
Let’s take, for example, the image below, as a brief practice. Make sure you take a long look at the image and try to think about 5 to 10 things you notice in it. Focus on what calls your attention the most. To make it more fun write them down or say them out loud and then see if Google can guess correctly.
![pexelsphoto802024 | Embarcadero RAD Studio Delphi C++Builder Blogs pexels photo 802024](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/pexels-photo-802024-4339367.jpeg?resize=750%2C498&ssl=1)
How do I call the Google Vision AI API Label Detection endpoint?
All we need to do is to call the API URL via an HTTP POST method passing the request JSON body with type LABEL_DETECTION and source as the link to the image we want to analyze. We can do that using REST Client libraries available in several programming languages. A quick start guide is available on Google’s documentation found here: (https://cloud.google.com/vision/docs/quickstart-client-libraries).
Actually, at the bottom page of the Google Cloud Vision documentation Guide (https://cloud.google.com/vision/docs/labels) there is an option: “Try This API” allowing you to post the JSON request body as shown below and get a JSON response.
In this article, you’ll learn about this topic, “What does the Google Vision API Object Localization endpoint return?”
1 |
POST https://vision.googleapis.com/v1/images:annotate |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
{ "requests": [ { "features": [ { "maxResults": 5, "type": "LABEL_DETECTION" } ], "image": { "source": { "imageUri": "https://images.pexels.com/photos/802024/pexels-photo-802024.jpeg?cs=srgb&dl=pexels-jose-francisco-fernandez-saura-802024.jpg&fm=jpg" } } } ] } |
What does the Google Vision Machine Learning API Label Detection endpoint return?
After the call the result will be a list with a “Label” description, the confidence score (which ranges from 0-no confidence at all to 1-very high confidence), and a measure of how important/central a label is to the overall context of a page. The list of Labels is returned in English but you can always use Google Cloud Translation API to translate from English to the language of your preference.
The list in this case only contains 5 results because we have configured the parameter maxResults
to 5 in the JSON request. We can play with this parameter and make the list longer to see how much closer Google would get to the items you listed during our exercise. Later you can go on playing with other images and challenging your friends!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
{ "responses": [ { "labelAnnotations": [ { "mid": "/m/079cl", "description": "Skyscraper", "score": 0.93450314, "topicality": 0.93450314 }, { "mid": "/m/0cgh4", "description": "Building", "score": 0.92697555, "topicality": 0.92697555 }, { "mid": "/m/01knjb", "description": "Billboard", "score": 0.83142585, "topicality": 0.83142585 }, { "mid": "/m/0522cz4", "description": "Electronic signage", "score": 0.7856768, "topicality": 0.7856768 }, { "mid": "/m/034z7h", "description": "Cityscape", "score": 0.784356, "topicality": 0.784356 } ] } ] |
How do I connect my applications to Google Cloud Vision Label Detection API?
Once you have followed the basic steps to set up Label Detection API on Google’s side, make sure you go to the Console and in the “Credentials” menu item click on the “Create Credentials” button and add an API key. Copy this key as we will need it later.
![Captura de tela 20210417 202254 | Embarcadero RAD Studio Delphi C++Builder Blogs captura de tela 2021 04 17 20 22 54](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/Captura-de-tela-2021-04-17-20.22.54-6289231.png?resize=750%2C543&ssl=1)
RAD Studio Delphi and C++Builder make it very easy to connect to APIs because you can use the REST Debugger to automatically create the required REST components and then simply paste them into your app.
What components do I need to use?
In Delphi all the job is done using just 3 components to make the API call. They are the TRESTClient, TRESTRequest, and TRESTResponse. Once you connect the REST Debugger successfully and copy and paste the components you will notice that the API URL is set on the BaseURL property of TRESTClient. On the TRESTRequest component you will see that the request type is set to rmPOST, the ContentType is set to ctAPPLICATION_JSON, and that it contains one request body for the POST.
Next, run your RAD Studio Delphi and on the main menu click on Tools > REST Debugger. Configure the REST Debugger as follows marking the content-type as application/json
, and add the POST URL, the JSON request body, and the API key you created. Once you click the “Send Request” button you should see the JSON response, just like we demonstrated above.
![Captura de tela 20210417 202422 | Embarcadero RAD Studio Delphi C++Builder Blogs Powerful Vision AI Tools captura de tela 2021 04 17 20 24 22](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/Captura-de-tela-2021-04-17-20.24.22-2954697.png?resize=750%2C532&ssl=1)
![Captura de tela 20210417 202520 | Embarcadero RAD Studio Delphi C++Builder Blogs Powerful AI Vision Tools captura de tela 2021 04 17 20 25 20](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/Captura-de-tela-2021-04-17-20.25.20-8181409.png?resize=750%2C530&ssl=1)
How do I build a Windows desktop or Android/iOS mobile device application using the Google Cloud Vision AI API?
Now that you were able to successfully configure and test your API calls on the REST Debugger, just click the “Copy Components” button, go back to Delphi and create a new application project. Now paste the components on your application’s main form.
We can add some very simple code to a TButton OnClick event to make sure everything is configured correctly and we’re done. In five minutes we have made our very first call to the Google Vision machine learning API and we are able to receive JSON responses for whatever images on which we want to perform Label Detection. Please note that on the TRESTResponse component the RootElement is set to ‘responses[0].labelAnnotations
’. This means that the ‘labelAnnotations’ element in the JSON is specifically able to be pulled into an in-memory table (TFDMemTable).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
procedure TForm1.Button1Click(Sender: TObject); var APIkey: String; begin FDMemTable1.Active := False; RESTClient1.ResetToDefaults; RESTClient1.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,'; RESTClient1.AcceptCharset := 'UTF-8, *;q=0.8'; RESTClient1.BaseURL := 'https://vision.googleapis.com'; RESTClient1.HandleRedirects := True; RESTClient1.RaiseExceptionOn500 := False; //APIkey := 'put here your Google API key'; RESTRequest1.Resource := Format('v1/images:annotate?key=%s', [APIkey]); RESTRequest1.Client := RESTClient1; RESTRequest1.Response := RESTResponse1; RESTRequest1.SynchronizedEvents := False; RESTResponse1.ContentType := 'application/json'; RESTRequest1.Params[0].Value := Format( '{"requests": [{"features": [{"maxResults": %s,"type": "LABEL_DETECTION"}],"image": {'+ '"source": {"imageUri": "%s"}}}]}', [Edit2.text, Edit1.text]); RESTResponse1.RootElement := 'responses[0].labelAnnotations'; RESTRequest1.Execute; memo2.Lines := RESTResponse1.Headers; memo3.Lines.text := RESTResponse1.Content; end; |
The sample application features a TEdit as a destination to paste in the link to the image you want to analyze. Another TEdit is used for the maxResults parameter with a TMemo to display the JSON results of the REST API call. We have a TStringGrid component with which to navigate and display the data in a tabular way all of which demonstrates how to easily integrate the JSON response result with a TFDMemTable component. When the button is clicked the image is analyzed and the application presents the JSON response as text and as data in a grid. Now you have every thing you need in order to integrate with the response data and make your application process the information the way it better suits your needs.
![Captura de tela 20210419 223534 | Embarcadero RAD Studio Delphi C++Builder Blogs Google Vision AI Tools REST results](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/Captura-de-tela-2021-04-19-22.35.34-2419171-1024x576-5100472.jpg?resize=750%2C422&ssl=1)
A quick recap
In this blog post, we learned how to sign up for the Google Cloud Vision machine learning API in order to perform Label detection functions on images. We’ve seen how to use the RAD Studio REST Debugger to connect to the API’s endpoint and copy that code into a real application. Finally, we’ve seen how simple and quick it is to use RAD Studio Delphi to create a real Windows (and Linux, macOS, Android, or iOS) application that connects to the Google Cloud Vision API, performs Label Detection image analysis, and returns an in-memory dataset ready for us to use.
Ready to create and integrate the machine learning API for label detection on your own? You may use the Multiple Platform App Development tool which will help you develop applications on Windows and mobile platforms. Request a Free Trial here.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Great my friend!