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 programming 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.
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.
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.
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