.Net Core Console Application that leverages the Azure Cognitive Services Vision SDK to recognize objects in a jpg image file.
The application will loop recursively through all directories in the path specified in the configuration. It will then send them individually to the API to have them analyzed. The result is stored as json in a file created with the same name as the image in the same folder.
The main purpose of this repository is not the tool itself, but the demonstration of several ways to call the rate limited api using multithreading techniques. When calls have to be throttled, backpressure has to be applied to the producer in a producer/consumer scenario. There are several implementations of the producer/consumer scenario in this repository:
The current implementations are based on the S1 pricing tier that allows for 10 calls per second to the computer vision api.
Technique | Description |
---|---|
BlockingCollection | Blocking Collections supports limiting the concurrency by creating a bounded instance. |
TPL DataFlow | TPL Dataflow is a good fit for this task since it allows you to create a pipeline for the process and apply concurrencly limits per step in the pipeline. |
PLinq | Though Parallel Linq supports limiting the concurrency using WithDegreeOfParallelism , it does not work well with the async-await pattern since it does not support Task or Task<T> . |
Tasks | Using Task or Task<T> does not support limiting concurrency. |
Channels | Channels are somewhat similar to blocking collections but have some advantages. They support limiting the concurrency by creating a bounded instance. |
Reactive Extensions | Reactive Extensions is the best fit for this particular task since it can throttle based on time. |
Use the template below to provision an Azure Cognitive Services Resource. This is a required step for the application to function. An already existing resource can be used as well.
This template deploys an Cognitive Services Computer Vision API. In the outputs section it will show the Keys and the Endpoint.
The configuration can be set using the commandline or the appsettings.json file.
Key | Description | Default |
---|---|---|
SubscriptionKey | The key of the Azure Cognitive Services Resource. (documentation) | Empty |
Region | The region where the Azure Cognitive Services Resource is located. | Empty |
ImagesPath | Full path to the local drive where the jpg files are located. | Empty |
ConfidenceThreshold | The minimum confidence score for an object to be included in the result set. | 0.75 |
Implementation | Name of the type that implements the service that will process the images. One of those. | ReactiveExtensionsService |
For each processed image file a json file with the same name as the image file is created with the result of the analysis. An example output looks like this:
{
"tags": [
"mountain",
"outdoor",
"sky",
"nature"
],
"description": "a view of a large mountain in the background"
}