The AI API “Fast Style” is a form of graphical style transfer which uses a neural network to combine the artistic styles of one image with another to render a new unique image. You can think if it like taking a photograph and combining it with a Manet painting to produce a unique photo after the style of Manet.
To make art, we will use the help of DeepAi.org a platform that you can use to make a lot of artificial intelligence projects. One of them is “fast style transfer”. We will connect with their API using Delphi.
REST and Delphi makes the API easy to use
Delphi has a tool called REST Debugger that makes it easy to connect with a REST API. After using it and successfully make the connection, you can copy the components into your Delphi application. Let’s dive into the DeepAi API and see how to build a desktop application to make art.
How to connect with DeepAi API?
From the Fast Style documentation, you can see that you will need to send three parameters to the API endpoint. Two links or files with images (where one is the image to render and the other, the style you want to apply), and your API key.
How do I use Delphi to Connect with DeepAi API REST endpoints ?
First of all, let’s use the REST Debugger found in RAD Studio Delphi. The REST Debugger is a visual tool to connect with any API, make a call, and get a response. In your Delphi IDE,. To use it, in the IDE menu, go to Tools > REST Debugger.
Change the Method to POST and insert the API Endpoint ( https://api.deepai.org/api/fast-style ) in the URL field. Then, go to the Parameters Tab. To add the image links, click on the Add button. If you want to send a link directly from a web link, on Kind, select BODY. If you want to send a file of your hard drive, select FILE instead. In the name, write ‘content’ and put the link of the image in the value. For your style image, just change the name to ‘style’. Lastly, click on add again e now, select HEADER on Kind and, on name, write api-key and on value, put your API key value.
Sending a REST request to the DeepAI API
Now, click on Send Request and after some time, you should receive a 200 response and, on Tabular Data Tab, you will see the output_url, with the link of the image that the artificial intelligence created:
Congrats, now you can send and get data from an Artificial Intelligence API and use it as you want.
How do I build a Windows desktop application to send and get images from the DeepAi API?
The application we are gonna build here will get the images from your computer, show and send them to the deepAi API. Also, it will show the image that the API returns and save it into the application folder. So, with a form already created, paste the components copied from the REST Debugger.
Right-click on the RESTRequest1 component and execute it. After right-clicking on the FDMemTable1 component, go to Fields Editor > Add All Fields. I added more components to make the app. Your form should look similar to this:
Connecting a REST request with the API
Now, let’s go to the code. To connect with the API you can use:
RESTRequest1.Params.Value := mainImgFile;
RESTRequest1.Params.Value := styleImgFile;
To process the result and download the image, we created the GetImg procedure. There you can do something like this:
MS := TMemoryStream.Create;
IdHTTP1 := TIdHTTP.Create(nil);
IMG := TJPEGImage.Create;
MS.Position := 0;
MS.SaveToFile(resourcesDirectory + 'output.jpg');
Here’s an example of the final DeepAI Fast Style API results from our Delphi app
You can check all the code here: https://github.com/brunomileto/deepai_api