• Blog
  • How to use the DeepAI service in the Delphi app

How to use the DeepAI service in the Delphi app

How to use the popular multifunctional DeepAI service in the Embarcadero Delphi FMX app via the DeepAI API

Publish date:
Discover more of what matters to you

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, etc.

In this article, we will analyze a popular multifunctional service – DeepAI. With this service, users can enjoy the capacities of AI via the API. It allows colorizing your old black-and-white photos. It can identify some undesirable items in the photo (like weapons, etc) and provides many other possibilities.

You can also check out our Delphi services.

We will show how to use the capacities of the DeepAI service in the Embarcadero Delphi FMX app via the DeepAI API. To work with images we offer to use the Image Colorization API.

Main Page Image Colorization API

This service for colorizing black-and-white photos is ensured by the API for work with images.

Description of Image Colorization API

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.

Description how to use Image Colorization API in your apps

For getting an API key, you have to sign up. This process is rather simple. It is necessary to click SIGNUP.

How to signup

Then you have to enter the user name, email, and password.

Signup dialog

After that, you will receive a link for account verification on your email. You will just have to follow the link.

DeepAI API key

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.

Billing information

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 Embarcadero Delphi for working with the DeepAI server, we should use standard components TNetHttpRequest and 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.

NetHttpRequest and NetHttpClient components’ settings

We will use the standard component TImage for showing the original black-and-white image in the FMX app.

TImage component for black and white image

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.

TImage component for black and color image

For showing the JSON reply from the server in the Delphi FMX app, we will use the standard component TMemo (the tab Standard).

TMemo component for keeping JSON response

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.

The variable (object) of the  TName ValuePair header class will be used for sending our API key REST to the DeepAI server.

TNameValue variable containing API key

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.

Black and white image path on Windows and Android

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.

TImage components LoadFromFile method

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.

TNetHttpRequest component’s POST method

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.

TNetHttpRequest component’s POST method input parameters description

The LMS object is used for saving the reply received from the server in a JSON format.

TMemoryStream variable

The received JSON reply from the server will be displayed in TMemo with the help of the LoadFromStream method.

TMemo component’s LoadFromStream method

The JSON response from the server includes the URL link to the file of the processed colorful image (theoutput_url parameter).

JSON response from DeepAI server with url link to color 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.

Parsing JSON response

For downloading a colorful image from the server, we need to use the GET method of the TNetHttpClient component.

TNetHttpClient GET method for color image downloading to TMemoryStream object LResponse

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.

TMemoryStream object description

For loading and displaying a colorful image, we will use the method of the TImage LoadFromStream component.

TImage component’s LoadFromStream method for color image showing to a user

Program code of Send Request To DEEPAI and Load Colorized Image button processor.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
procedure TForm1.Button1Click(Sender: TObject);
begin
TTask.Run(
procedure
var
LMultipartFormData: TMultipartFormData;
LMS: TMemoryStream;
header: TNameValuePair;
Json: TJSONObject;
begin
LMultipartFormData := TMultipartFormData.Create;
header := TNameValuePair.Create('api-key',
'5a84f646-474f-4b86-ab69-a7a5ef0ad537');
{$IFDEF ANDROID}
path := TPath.Combine(TPath.GetSharedPicturesPath, 'Einstein.jpeg');
LMultipartFormData.AddFile('image', path, 'image/*');
Image1.Bitmap.LoadFromFile(path);
{$ENDIF}
{$IFDEF MSWINDOWS}
path := 'c:\Einstein.jpeg';
LMultipartFormData.AddFile('image', path, 'application/octet-stream');
Image1.Bitmap.LoadFromFile(path);
{$ENDIF}
LMS := TMemoryStream.Create;
NetHTTPRequest1.Post('https://api.deepai.org/api/colorizer',
LMultipartFormData, LMS, [header]);
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.LoadFromStream(LMS);
TabControl1.GotoVisibleTab(1)
end);
Json := TJSONObject.ParseJSONValue(Memo1.Text) as TJSONObject;
TThread.Synchronize(nil,
procedure
begin
ColorizedImagePath := Json.GetValue('output_url').Value;
end);
LMS.Free;
LMultipartFormData.Free;
end);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Label2.Text := ColorizedImagePath;
if NetHTTPClient1.Tag = NOT_BUSY then
begin
NetHTTPClient1.Tag := BUSY;
TTask.Run(
procedure
var
LResponse: TMemoryStream;
begin
LResponse := TMemoryStream.Create;
try
NetHTTPClient1.Get(ColorizedImagePath, LResponse);
TThread.Synchronize(nil,
procedure
begin
Image2.Bitmap.LoadFromStream(LResponse);
end);
finally
LResponse.Free;
NetHTTPClient1.Tag := NOT_BUSY;
TabControl1.GotoVisibleTab(2)
end;
end);
end;
end;

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.

Access to internet permission on Android
Write to external storage permission on Android

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.

Original black and white image
JSON response from DeepAI
Color image from DeepAI server
Original black and white image 2
JSON response from DeepAI 2
Color image from DeepAI server 2
Original black and white image 3
JSON response from DeepAI 3
Color image from DeepAI server 3

Let’s test the work of the FMX app on an Android device.

JSON response from DeepAI Android device
Original black and white image Android device
Color image from DeepAI server Android device

Watch step by step guide on our youtube channel

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Subscribe to our newsletter and get amazing content right in your inbox.

You can unsubscribe from the newsletter at any time

This field is required
This field is required Invalid email address

You're almost there...

A confirmation was sent to your email

confirm your subscription, make sure to check
your promotions/spam folder