Table of Contents
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.
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.
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?
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:
Our aim is a CLR C++ exporting two functions:
__declspec(dllexport) BSTR Densecap_ProcessFile(BSTR key, BSTR filename);
__declspec(dllexport) BSTR Densecap_ProcessUrl(BSTR key, BSTR url);
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
Right click on the project, select “Manage Nuget Packages”:
Type DeepAI. Press Install.
Selected package appears together with packages it depends on. Press OK.
Create two static functions, for file and URL.
Build the project.
C++ CLR wrapper – project DeepAIClrWrapper
Add a new C++ project – CLR Class Library.
Right click References, add a reference to the recently created DeepAICsWrapper
Required functions are in DeepAIClrWrapper.h, leaving cpp empty.
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.
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:
function Densecap_ProcessFile(key, filename: TBSTR): TBSTR; cdecl;
external 'DeepAIClrWrapper' name 'Densecap_ProcessFile';
function Densecap_ProcessUrl(key, filename: TBSTR): TBSTR; cdecl;
external 'DeepAIClrWrapper' name 'Densecap_ProcessUrl';
The Delphi test is implemented as a VCL form
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
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?