Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++CodeDelphiRAD Studio

How To Use A REST AI API The Really Difficult Way

using AI API the hard way

REST AI APIs are really easy to use with RAD Studio

Look, we have been spoiling you a little too much on this site. We keep showing you how few lines of code it is to call APIs on public REST servers. We showed you comparison tests which proved that the skilled Delphi developers in the test were nearly 7 times faster than some competing technologies. We’ve published a total smorgasbord of articles about how totally easy it is to use Windows IDE to perform artificial intelligence, machine learning, and deep learning, along with some nifty demonstrations integrating image processing in your apps. We really mean it when we say RAD Studio Delphi makes difficult tasks easier.

We know though, you might not believe us. So here’s how to include artificial intelligence into your apps by using a REST API the really difficult way.

DenseCap sample

What can be done with DenseCap Artificial Intelligence?

DenseCap detects objects in an image. It creates a JSON object containing coordinates of detected objects and readable captions for them. In the left side of title picture, you see the image processed. The middle part shows starting lines of the JSON object text representation.

The DenseCap page contains the product description. The mathematical background is described in this PDF document.

For demo purposes, we chose the simplest – draw the found objects on initial image. You see it in the right part of title picture.

An important practical usage – process a large image library and store extracted data in DB. This DB can be used to search images or fragments by caption texts.

How do I use the DenseCap AI API in my applications?

A DenseCap wrapper is available as a C# Nuget package. Available also C# source and JavaScript source in GitHub, but they are a bit obsolete.

The DenseCap C# Nuget package does not expose COM interface. Therefore, we should create a wrapper DLL in Visual Studio. C# usage for two tasks, image file and image UR, proposed by DenseCap developers, is following:

image 2446403

Our aim is a CLR C++ exporting two functions:

These functions return strings – JSON object textual representations. For further processing in Delphi, these strings can be converted in JSON again.

Unfortunately, currently Visual Studio CLR C++ does not support Nuget import. So, two wrappers required, C# importing Nuget and CLR C++ exporting desired functions.

A complete solution available at the following link: https://github.com/checkdigits/Embarcadero_DenseCap2 All binaries required for Delphi are placed in the “Release: folder.. To work with Delphi, you do not need Visual Studio, simply download files from this folder. And now, you can skip chapters about Visual Studio, and go directly to the Delphi part.

C# wrapper – project DeepAICsWrapper

Using a REST AI API the really hard way screen shot 1

Right click on the project, select “Manage Nuget Packages”:

Using a REST AI API the really hard way screen shot 2

Type DeepAI. Press Install.

Using a REST AI API the really hard way screen shot 3

Selected package appears together with packages it depends on. Press OK.

Using a REST AI API the really hard way screen shot 3

Create two static functions, for file and URL.

image 1438984

Build the project.

C++ CLR wrapper – project DeepAIClrWrapper

Add a new C++ project – CLR Class Library.

Using a REST AI API the really hard way screen shot CLR wrapper

Right click References, add a reference to the recently created DeepAICsWrapper

Using a REST AI API the really hard way screen shot wrapper

Required functions are in DeepAIClrWrapper.h, leaving cpp empty.

image 8777801

Build the project.

Using the DenseCap Deep.Ai AI API in C++

For C++ testing, I wrote a console application.

The application starts with a key request. To get a permanent key, you should sign up in DeepAI page. It is free.

image 6209188

Using the DenseCap Deep.Ai AI API in Delphi

The test for Delphi can be found here: https://github.com/checkdigits/Embarcadero_DenseCap2 in the “DelphiTest” folder. You should also download Release folder, it contains some required DLLs. If you have not Visual C++ Redistributable, probably you should install it.

The Release directory is required also, if you write your own program. Your executable should run in it (or it should be in the path).

The two exposed DenseCap functions in Delphi are defined as following:

The Delphi test is implemented as a VCL form

Using a REST AI API the really hard way screen shot JSON results

On the start, application key is filled with a demo key “quickstart-QUdJIGlzIGNvbWluZy4uLi4K”. It works only some times. To get a permanent key, you should sign up in DeepAI page. It is free.

The functionality is simple. Fill file or URL edit box, press corresponding “Process” and receive result in the lower part.

The programmer should be careful with BSTR processing. BSTRs passed to DenseCap wrapper should be allocated using SysAllocString. All BSTRs, arguments and the received result, should be freed using SysFreeString.

Here are the results after the file has been processing by the DeepCap artificial intelligence

image 8663833

URL processing:

image 4034604

We did it!

So that’s “all” there is to it. A few hundred lines of C# code, a lot of C++ code and a little bit of Delphi code along with a collection of DLLs and we’re then using the Deep.Ai DenseCap REST API.

Or, you know, we could have done it the easy and super-fast way in a handful of lines of Delphi code with the RAD Studio REST Debugger and some help from our friends.


RAD Studio makes connecting to REST servers and including artificial intelligence APIs an extremely easy task with just a few lines of code. Why not download a trial today?

Download RAD Studio Free Trial

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES