Today AI technologies boast high popularity and demand in many spheres of our life. For example, AI is used for face identification on smartphones. On social networks, we have the possibility to add different effects on photos (AR, collages, etc.). AI tools allow users to identify different objects. They are used in video surveillance systems for identifying cars, people, animals, and all sorts of other useful capabilities.
In this article, we will analyze a popular multifunctional AI service, DeepAI, and show you how easy it is to use it in your cross platform apps. With the DeepAI service, users can enjoy all the capabilities and benefits of AI via a simple and straight-forward API. One DeepAI feature allows colorizing your old black-and-white photos. It can also identify some undesirable items in the photo (like weapons, etc) and provides many other possibilities.
We will show how you can easily use the DeepAI service in your Delphi FMX cross platform apps. To work with images, we suggest using the Image Colorization API.
Table of Contents
What does the DeepAI Image Colorization API do?
This service for colorizing black-and-white photos is ensured by the API for work with images.
For working via the API, it is necessary to send a
POST request to the DeepAI server and to provide the path to the file of a black-and-white image and an API key. It is also required to use a link for connecting to the DeepAI Image Colorization API service.
Get valuable insights on how clean and sharp photos are when implementing an image upscale using DeepAI’s Super Resolution API.
How do I get a DeepAI API key?
For getting an API key, you have to sign up. This process is rather simple. It is necessary to click SIGNUP.
Then you have to enter the user name, email, and password.
After that, you will receive a link for account verification on your email. You will just have to follow the link.
You need to copy the received API key and save it for further use in the FMX Delphi app.
You will also have to add bank card details (card number, the date of expiration, CVV) for correct interaction with the service (the tab Billing). DeepAI offers a free trial limit of 5 dollars.
How to use DeepAI in a Delphi FMX cross platform App
As an example, we will send a black-and-white photo from our Delphi FMX app with the use of the
POST request to the server and will get a response in the JSON format that keeps the URL link leading to the processed colorful image.
In Delphi, for working with the DeepAI server, we can use some standard components
TNetHttpClient. We have to assign the value of the current TNetHttpClient component (NetHttpClient1) to the Client field in the TNetHttpRequest component. We also need to assign the value “POST” to the MethodString field.
How do I display the DeepAI image colorization results in a Delphi cross platform app?
We will use the standard component TImage for showing the original black-and-white image in the FMX app.
For showing the processed colorful image to a user with the help of the REST of the DeepAI service, we also have to use the standard component TImage.
For showing the JSON reply from the server in the Delphi FMX app, we will use the standard component TMemo (the tab Standard).
For sending the parameters via the POST request to the server, such as the API key, the path to our black-and-white image on the computer, we should use the following variables.
Sending the DeepAI API request to the server
The variable (object) of the TName ValuePair header class will be used for sending our API key REST to the DeepAI server.
The LMultiPartFormData object of the TMultipartFormData class is used for sending our black-and-white image following its path on the computer. A line variable path saves the path to our image file.
The path to the image differs on MSWindows and Android. That’s why conditional compilation is applied for these platforms.
During the compilation for MSWindows, our image file is stored on the C disk (c:Einstein.jpeg).
On Android, the image file will be stored in the internal memory of the smartphone in the Pictures folder.
For loading the original black-and-white image to the TImage component, the LoadFromFile method is used. As a parameter, it takes a variable path.
The line variable path stores the name and the path to the image file.
For executing the POST request and sending all the necessary data (a path to the image file, an API key) to the server, we need to use the POST method of the NetHTTPRequest1 component.
The method receives a link for connecting to the server, the LMultiPartFormData object that stores the path to the image file, the LMS object of the TMemoryStream class and the header variable with the API key.
The LMS object is used for saving the reply received from the server in a JSON format.
The received JSON reply from the server will be displayed in TMemo with the help of the LoadFromStream method.
The JSON response from the server includes the URL link to the file of the processed colorful image (theoutput_url parameter).
How to parse the DeepAI API JSON results into an image?
For loading a colorful image, it is necessary to get the URL link from the JSON reply from the DeepAI server via parsing.
For reply parsing, we should use the JSON object of the TJSONObject class.
As a result of parsing the JSON reply from the server, we will get the URL link and save it in the line variable ColorizedImagePath.
For downloading a colorful image from the server, we need to use the GET method of the TNetHttpClient component.
The GET method accepts the URL link of the colorful image in the server and the object of the TMemoryStream LResponse class. In our case, LResponse gets the colorful image from the server for its further display in TImage.
For loading and displaying a colorful image, we will use the method of the TImage LoadFromStream component.
Is there a Delphi example of using DeepAI to colorize images?
Here is some example Delphi program code of sending a request to DEEPAI and then loading the results into an image.
procedure TForm1.Button1Click(Sender: TObject);
LMultipartFormData := TMultipartFormData.Create;
header := TNameValuePair.Create(‘api-key’,
path := TPath.Combine(TPath.GetSharedPicturesPath, ‘Einstein.jpeg’);
LMultipartFormData.AddFile(‘image’, path, ‘image/*’);
path := ‘c:Einstein.jpeg’;
LMultipartFormData.AddFile(‘image’, path, ‘application/octet-stream’);
LMS := TMemoryStream.Create;
LMultipartFormData, LMS, [header]);
Json := TJSONObject.ParseJSONValue(Memo1.Text) as TJSONObject;
ColorizedImagePath := Json.GetValue(‘output_url’).Value;
procedure TForm1.Button2Click(Sender: TObject);
Label2.Text := ColorizedImagePath;
if NetHTTPClient1.Tag = NOT_BUSY then
NetHTTPClient1.Tag := BUSY;
LResponse := TMemoryStream.Create;
NetHTTPClient1.Tag := NOT_BUSY;
Are there any special considerations for Delphi Android apps when using DeepAI?
For the correct functioning of the Delphi FMX app on the Android platform, we need to install permissions for access to smartphone memory and internet connection.
Check out how these speech services by Google could be vital for the success of your business operations.
Let’s test the work of our Embarcadero Delphi FMX app on the MSWindows and Android platforms. Let’s send a black-and-white picture and get a processed colorful image from the DeepAI server.
What does the Delphi Android FMX DeepAI cross platform example results look like?
Let’s test the work of the FMX app on an Android device.
Watch step by step guide on the Softacom Youtube channel
This article was written by Softacom, one of our excellent Embarcadero Tech Partners.
You can read more about Softacom’s AI implementation services by clicking this link: https://softacom.com/services/artificial_intelligence_implementation_with_delphi/
Why not download a free trial of Embarcadero Delphi today and try the DeepAI example for yourself?