3_TestCLI
Estimated Time: 20-25 minutes
Exploring Cosmos DB
Azure Cosmos DB is our resilient NoSQL PaaS solution and is incredibly useful for storing loosely structured data like we have with our image metadata results. There are other possible choices (Azure Table Storage, SQL Server), but Cosmos DB gives us the flexibility to evolve our schema freely (like adding data for new services), query it easily, and can be quickly integrated into Azure Search (which we’ll do in a later lab).
Lab 3.1 (optional): Understanding CosmosDBHelper
Cosmos DB is not a focus of this workshop, but if you’re interested in what’s going on - here are some highlights from the code we will be using:
- Navigate to the
CosmosDBHelper.csclass in theImageStorageLibrary. Review the code and the comments. Many of the implementations used can be found in the Getting Started guide. - Go to
TestCLI’sUtil.csand review theImageMetadataclass (code and comments). This is where we turn theImageInsightswe retrieve from Cognitive Services into appropriate Metadata to be stored into Cosmos DB. - Finally, look in
Program.csinTestCLIand atProcessDirectoryAsync. First, we check if the image and metadata have already been uploaded - we can useCosmosDBHelperto find the document by ID and to returnnullif the document doesn’t exist. Next, if we’ve setforceUpdateor the image hasn’t been processed before, we’ll call the Cognitive Services usingImageProcessorfrom theProcessingLibraryand retrieve theImageInsights, which we add to our currentImageMetadata.
Once all of that is complete, we can store our image - first the actual image into Blob Storage using our BlobStorageHelper instance, and then the ImageMetadata into Cosmos DB using our CosmosDBHelper instance. If the document already existed (based on our previous check), we should update the existing document. Otherwise, we should be creating a new one.
Lab 3.2: Loading Images using TestCLI
We will implement the main processing and storage code as a command-line/console application because this allows you to concentrate on the processing code without having to worry about event loops, forms, or any other UX related distractions. Feel free to add your own UX later.
Once you’ve set your Cognitive Services API keys, your Azure Blob Storage Connection String, and your Cosmos DB Endpoint URI and Key in your TestCLI’s settings.json, you can run the TestCLI.
Run TestCLI, then open Command Prompt and navigate to “C:\LearnAI-Bootcamp\lab01.1-computer_vision\resources\code\Starting-ImageProcessing\TestCLI\bin\Debug” folder (Hint: use the “cd” command to change directories). Then enter TestCLI.exe. You should get the following result:
> TestCLI.exe
Usage: [options]
Options:
-force Use to force update even if file has already been added.
-settings The settings file (optional, will use embedded resource settings.json if not set)
-process The directory to process
-query The query to run
-? | -h | --help Show help information
By default, it will load your settings from settings.json (it builds it into the .exe), but you can provide your own using the -settings flag. To load images (and their metadata from Cognitive Services) into your cloud storage, you can just tell TestCLI to -process your image directory as follows:
> TestCLI.exe -process c:\learnai-bootcamp\lab01.1-computer_vision\resources\sample_images
Once it’s done processing, you can query against your Cosmos DB directly using TestCLI as follows:
> TestCLI.exe -query "select * from images"
Take some time to look through the sample_images (you can find them in resources/sample_images) and compare the images to the results in your application.
Continue to 4_Challenge_and_Closing
Back to README