{"id":663,"date":"2023-04-13T14:07:00","date_gmt":"2023-04-13T14:07:00","guid":{"rendered":"http:\/\/draith.com\/?p=663"},"modified":"2024-02-16T02:23:11","modified_gmt":"2024-02-16T02:23:11","slug":"setting-up-and-accessing-azure-cognitive-services-with-powershell","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=663","title":{"rendered":"Setting Up and Accessing Azure Cognitive Services with PowerShell"},"content":{"rendered":"\n<p>Alright folks &#8211; we&#8217;re going to dive into how you can leverage Azure Cognitive Services  with PowerShell to not only set up AI services but also to interact with  them.  Let&#8217;s go!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Prerequisites<\/h4>\n\n\n\n<p>Before we begin, ensure you have the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An Azure subscription. <\/li>\n\n\n\n<li>PowerShell 7.x or higher installed on your system.<\/li>\n\n\n\n<li>Azure PowerShell module. Install it by running <code>Install-Module -Name Az -AllowClobber<\/code> in your PowerShell session.<\/li>\n<\/ul>\n\n\n\n<p>Use Connect-AZAccount to get into your subscription, then run this to create a new RG and Cognitive Services resource:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$resourceGroupName = &quot;&lt;YourResourceGroupName&gt;&quot;\n$location = &quot;EastUS&quot;\n$cognitiveServicesName = &quot;&lt;YourCognitiveServicesName&gt;&quot;\n\n# Create a resource group if you haven't already\nNew-AzResourceGroup -Name $resourceGroupName -Location $location\n\n# Create Cognitive Services account\nNew-AzCognitiveServicesAccount -Name $cognitiveServicesName -ResourceGroupName $resourceGroupName -Type &quot;CognitiveServices&quot; -Location $location -SkuName &quot;S0&quot;\n<\/pre><\/div>\n\n\n\n<p>It&#8217;s that simple! <\/p>\n\n\n\n<p>To interact with Cognitive Services, you&#8217;ll need the access keys. Retrieve them with:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$key = (Get-AzCognitiveServicesAccountKey -ResourceGroupName $resourceGroupName -Name $cognitiveServicesName).Key1\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>With your Cognitive Services resource set up and your access keys in hand, you can now interact with various cognitive services. Let&#8217;s explore a couple of examples:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Text Analytics<\/h5>\n\n\n\n<p>To analyze text for sentiment, language, or key phrases, you&#8217;ll use the Text Analytics API. Here&#8217;s a basic example to detect the language of a given text:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$text = &quot;Hello, world!&quot;\n$uri = &quot;https:\/\/&lt;YourCognitiveServicesName&gt;.cognitiveservices.azure.com\/text\/analytics\/v3.1\/languages&quot;\n\n$body = @{\n    documents = @(\n        @{\n            id = &quot;1&quot;\n            text = $text\n        }\n    )\n} | ConvertTo-Json\n\n$response = Invoke-RestMethod -Uri $uri -Method Post -Body $body -Headers @{\n    &quot;Ocp-Apim-Subscription-Key&quot; = $key\n    &quot;Content-Type&quot; = &quot;application\/json&quot;\n}\n\n$response.documents.languages | Format-Table -Property name, confidenceScore<\/pre><\/div>\n\n\n\n<p>So this code will try and determine the language of the text submitted.  The output might look like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">Name           ConfidenceScore\n----           ---------------\nEnglish        0.99\n<\/pre><\/div>\n\n\n\n<p>Let&#8217;s try computer vision now:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Computer Vision<\/h5>\n\n\n\n<p>Azure&#8217;s Computer Vision service can analyze images and extract information about visual content. Here&#8217;s how you can use PowerShell to send an image to the Computer Vision API for analysis:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">$imageUrl = &quot;&lt;YourImageUrl&gt;&quot;\n$uri = &quot;https:\/\/&lt;YourCognitiveServicesName&gt;.cognitiveservices.azure.com\/vision\/v3.1\/analyze?visualFeatures=Description&quot;\n\n$body = @{\n    url = $imageUrl\n} | ConvertTo-Json\n\n$response = Invoke-RestMethod -Uri $uri -Method Post -Body $body -Headers @{\n    &quot;Ocp-Apim-Subscription-Key&quot; = $key\n    &quot;Content-Type&quot; = &quot;application\/json&quot;\n}\n\n$response.description.captions | Format-Table -Property text, confidence\n<\/pre><\/div>\n\n\n\n<p>This code is trying to describe the image, so the output might look like this &#8211; pardon the bad word wrap:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">Text                            Confidence\n----                            ----------\nA scenic view of a mountain range under a clear blue sky 0.98\n<\/pre><\/div>\n\n\n\n<p>To learn more about Cognitive Services &#8211; check out the <a href=\"https:\/\/docs.microsoft.com\/azure\/cognitive-services\/?wt.mc_id=AZ-MVP-5002833\" data-type=\"link\" data-id=\"https:\/\/docs.microsoft.com\/azure\/cognitive-services\/?wt.mc_id=AZ-MVP-5002833\">Docs<\/a>!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alright folks &#8211; we&#8217;re going to dive into how you can leverage Azure Cognitive Services with PowerShell to not only set up AI services but also to interact with them. Let&#8217;s go! Prerequisites Before we begin, ensure you have the following: Use Connect-AZAccount to get into your subscription, then run this to create a new &hellip; <a href=\"https:\/\/draith.com\/?p=663\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting Up and Accessing Azure Cognitive Services with PowerShell&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[26,4,16],"class_list":["post-663","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ai","tag-azure","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=663"}],"version-history":[{"count":1,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/663\/revisions"}],"predecessor-version":[{"id":664,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/663\/revisions\/664"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}