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.
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.
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.
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.
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.
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.
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).
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.
Program code of Send Request To DEEPAI and Load Colorized Image button processor.
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;
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.
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.
Let’s test the work of the FMX app on an Android device.
Watch step by step guide on our youtube channel