![| Embarcadero RAD Studio Delphi C++Builder Blogs using AI API the hard way](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/pexels-pixabay-35797-1957368-scaled.jpg?resize=1140%2C694&ssl=1)
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.
![DenseCap sample | Embarcadero RAD Studio Delphi C++Builder Blogs DenseCap sample](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/DenseCap-1-8336747.jpg?resize=750%2C409&ssl=1)
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:
![image2446403 | Embarcadero RAD Studio Delphi C++Builder Blogs image 2446403](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-2446403.png?resize=748%2C319&ssl=1)
Our aim is a CLR C++ exporting two functions:
1 2 |
__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
![image6530465 | Embarcadero RAD Studio Delphi C++Builder Blogs Using a REST AI API the really hard way screen shot 1](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-6530465.png?resize=692%2C341&ssl=1)
Right click on the project, select “Manage Nuget Packages”:
![image2499983 | Embarcadero RAD Studio Delphi C++Builder Blogs Using a REST AI API the really hard way screen shot 2](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-2499983.png?resize=427%2C269&ssl=1)
Type DeepAI. Press Install.
![image6205994 | Embarcadero RAD Studio Delphi C++Builder Blogs Using a REST AI API the really hard way screen shot 3](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-6205994.png?resize=750%2C152&ssl=1)
Selected package appears together with packages it depends on. Press OK.
![image1540667 | Embarcadero RAD Studio Delphi C++Builder Blogs Using a REST AI API the really hard way screen shot 3](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-1540667.png?resize=482%2C296&ssl=1)
Create two static functions, for file and URL.
![image1438984 | Embarcadero RAD Studio Delphi C++Builder Blogs image 1438984](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-1438984.png?resize=749%2C350&ssl=1)
Build the project.
C++ CLR wrapper – project DeepAIClrWrapper
Add a new C++ project – CLR Class Library.
![image8209912 | Embarcadero RAD Studio Delphi C++Builder Blogs Using a REST AI API the really hard way screen shot CLR wrapper](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-8209912.png?resize=691%2C295&ssl=1)
Right click References, add a reference to the recently created DeepAICsWrapper
![image6161861 | Embarcadero RAD Studio Delphi C++Builder Blogs Using a REST AI API the really hard way screen shot wrapper](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-6161861.png?resize=750%2C318&ssl=1)
Required functions are in DeepAIClrWrapper.h, leaving cpp empty.
![image8777801 | Embarcadero RAD Studio Delphi C++Builder Blogs image 8777801](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-8777801.png?resize=750%2C500&ssl=1)
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.
![image6209188 | Embarcadero RAD Studio Delphi C++Builder Blogs image 6209188](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-6209188.png?resize=750%2C764&ssl=1)
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:
1 2 3 4 |
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
![image9072319 | Embarcadero RAD Studio Delphi C++Builder Blogs Using a REST AI API the really hard way screen shot JSON results](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-9072319.png?resize=750%2C525&ssl=1)
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
![image8663833 | Embarcadero RAD Studio Delphi C++Builder Blogs image 8663833](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-8663833.png?resize=722%2C420&ssl=1)
URL processing:
![image4034604 | Embarcadero RAD Studio Delphi C++Builder Blogs image 4034604](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/image-4034604.png?resize=748%2C422&ssl=1)
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?
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition