Skip to content

Commit 3bc9d27

Browse files
committed
updated documentation
1 parent 5504eea commit 3bc9d27

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ In this tutorial we go trough how to handle directories and how we can run the *
2121
[Files and directories part II - use custom configuration](/Tutorials/file-handling2.md)
2222

2323
We extend the tutorial from part I and look at how to add your custom configuration and use it in your Command class.
24+
25+
[Setup a project that uses Microsoft Azure CognitiveServices](/Tutorials/cognitive-services.md)
26+
27+
This example requires a Microsoft Azure account, no credits needed though the image recognition service is free to try out in a small scale. In this tutorial you will see how you can use environment variables in your **Command** class.

Tutorials/cognitive-services.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Use ComputerVision with PowerCommands
2+
## Before you start
3+
You need an Azure account, you need to add a new Cognitive Service of the type Computer Vision here:
4+
5+
https://portal.azure.com/#view/Microsoft_Azure_ProjectOxford/CognitiveServicesHub/~/overview
6+
7+
Setup two environment variables for the endpoint and the key, look in the PowerCommandsConfiguration.yaml file and there you se the details.
8+
9+
```
10+
environment:
11+
variables:
12+
- name: powercommands-computer-vision-endpoint
13+
environmentVariableTarget: User
14+
- name: powercommands-computer-vision
15+
environmentVariableTarget: User
16+
```
17+
## Computer vision command
18+
So the first PowerCommand I added is the ```computervision``` command named **ComputerVisionCommand**, just run it and of course check out the code.
19+
20+
```
21+
ComputerVision
22+
```
23+
24+
Read more about it here: https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/image-analysis-client-library?tabs=visual-studio%2C3-2&pivots=programming-language-csharp
25+
26+
## The Code
27+
```
28+
[PowerCommandDesign( description: "Run Microsoft.Azure.CognitiveServices.Vision.ComputerVision services",
29+
useAsync: true,
30+
example: "computervision")]
31+
public class ComputerVisionCommand : CommandBase<PowerCommandsConfiguration>
32+
{
33+
public ComputerVisionCommand(string identifier, PowerCommandsConfiguration configuration) : base(identifier, configuration) { }
34+
public override async Task<RunResult> RunAsync()
35+
{
36+
var client = Authenticate(Configuration.Environment.GetValue("powercommands-computer-vision-endpoint"),
37+
Configuration.Environment.GetValue("powercommands-computer-vision"));
38+
await AnalyzeImageUrl(client, "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png");
39+
return Ok();
40+
}
41+
public static ComputerVisionClient Authenticate(string endpoint, string key) => new(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint };
42+
public async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl)
43+
{
44+
WriteLine("----------------------------------------------------------");
45+
WriteLine("ANALYZE IMAGE - URL");
46+
Console.WriteLine();
47+
48+
// Creating a list that defines the features to be extracted from the image.
49+
var features = new List<VisualFeatureTypes?> { VisualFeatureTypes.Tags };
50+
51+
WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}...");
52+
Console.WriteLine();
53+
// Analyze the URL image
54+
var results = await client.AnalyzeImageAsync(imageUrl, visualFeatures: features);
55+
56+
// Image tags and their confidence score
57+
WriteLine("Tags:");
58+
foreach (var tag in results.Tags)
59+
{
60+
WriteLine($"{tag.Name} {tag.Confidence}");
61+
}
62+
Console.WriteLine();
63+
}
64+
}
65+
```
66+
Note that I am using an hardcoded URL to the image in this example.
67+
68+
You can look at the source code [here](/src/Tutorials.AzureCognitiveServices/).

0 commit comments

Comments
 (0)