DeepAI is a prominently popular platform for artificial intelligence. They provide research, guides, news, and several AI APIs.
You can learn more about their APIs from this link: https://deepai.org/apis
The range and depth of the APIs offered by DeepAI are quite astounding. They include APIs which allow for image colorization, facial recognition, nudity detection, sentiment, and emotion detection, text summarization, pose detection, text/caption generation, OCR, text to image, signature verification, toonify (to turn images into cartoon-like drawings), content moderation, anonymization and MANY more – the complete list is pretty staggering! Chances are DeepAI has something to satisfy your needs for adding and using artificial intelligence to integrate it with IDE Software for Windows, desktop, and mobile apps.
We’re going to focus on one API in particular – the Super Resolution API.
Table of Contents
What is Super Resolution API?
In this article, we are going to implement the “Super Resolution API” using Delphi. This API uses machine learning to clean, sharp and upscale photos with out losing the original content. It can correct blurry images to some accepted level. This is not a simple mathematical algorithm to upscale image, but a machine learning which identify the content and make it better without losing original content. So it’s always better than an image processing application in your PC.
How to get DeepAI API key?
It’s easy to get the API key. You don’t need an Credit card. You will get $5 USD API credits free at registration which equivalent to 10,000 API requests. After that you can pay by $0.50 USD per 1000 requests. You can signup with google account or a regular signup. Then go to Dashboard and you can find the API key. You can use that key for any API.
How to send a request to Super Resolution API from Delphi?
There are two types of requests depending on,
- Send image as URL
- Send image as file
Parameters are the same for both requests.
- api-key: API key as header parameter
- image: Image to process
Code to send the API request using REST components are like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
RESTRequest.Params.Clear; RESTResponse.RootElement := ''; lparam := RESTRequest.Params.AddItem; lparam.name := 'api-key'; lparam.Value := [YOUR API KEY]; lparam.ContentType := ctNone; lparam.Kind := pkHTTPHEADER; lparam.Options := [poDoNotEncode]; lparam := RESTRequest.Params.AddItem; lparam.name := 'image'; lparam.Value := [PATH TO IMAGE]; lparam.ContentType := ctNone; lparam.Kind := pkFile; lparam.Options := [poDoNotEncode]; RESTRequest.Execute; |
You can send images in most of image file types, but the response always will be a JPG file.
How to process the response from the Super Resolution API?
If the request is success, you will get a response in JSON like this:
1 2 3 4 |
{ "id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX", "output_url": "https://api.deepai.org/job-view-file/XXXXXXXXXXXXXXXXXXXXXXXXXXXX/outputs/output.jpg" } |
ID is the unique id of your processed resource. “output_url” is the URL of your processed image. We can parse this JSON in Delphi like this and get the image as TPicture:
1 2 3 4 5 6 7 8 |
jsonObj := RESTResponse.JSONValue as TJSONObject; MS := TMemoryStream.Create; http.Get(jsonObj.Values['output_url'].Value, MS); MS.Position := 0; Picture := TPicture.Create; Picture.LoadFromStream(MS); imgImproved.Picture := Picture; MS.Free; |
It can take a while to process the image and send the response. So it’s recommended to use a thread to prevent from blocking the GUI.
You can download this demo application to see how it works. Just grab the API key form DeepAI, upload an image and start processing.
You can download the example code here: https://github.com/checkdigits/SuperResolutionAPI_example
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition