|
| 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