CustomVisionAITrainingExamples.ps1
<#PSScriptInfo .VERSION 1.0.0 .GUID 05f71140-66e2-46df-982b-55720ce7cbca .AUTHOR mikko@lavento.com .COMPANYNAME .COPYRIGHT .TAGS CustomVisionAI, CustomVision, Custom, Vision, CognitiveServices, Microsoft, Training .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION Microsoft Cognitive Services (Custom Vision AI) examples. Examples to upload training images to Microsoft Custom Vision AI API. Example to Create Tag, and use that to tag uploaded images Contents: 1- Create Tag API-sample 2- Get Tags API-sample 3- Upload local image using Base64 encoding API-sample 4- Upload local image using "direct" upload API-sample 5- Upload image from URL API-sample #> Param() #22.7.2019 M.Lavento #Microsoft Cognitive Services (Custom Vision AI) examples. #Examples to upload training images to Microsoft Custom Vision AI API #Example to Create Tag and use that to tag uploaded images #Contents: #1. Create Tag API-sample #2. Get Tags API-sample #3. Upload local image using Base64 encoding API-sample #4. Upload local image using "direct" upload API-sample #5. Upload image from URL API-sample #https://docs.microsoft.com/en-us/rest/api/cognitiveservices/customvisiontraining/createimagesfromdata/createimagesfromdata #URL in my case: https://northeurope.api.cognitive.microsoft.com/customvision/v3.0/Training/ #Trainingkey: <Use your own> #project ID: <Use your own> #####################Create Tag example############## ###Optional, but if you want to tag them, you have to have tagId $header = @{'Training-Key'= '<insert your key>'} $ProjectID = "<insert your project ID>" $APICreateTag = "https://northeurope.api.cognitive.microsoft.com/customvision/v3.0/training/projects/$ProjectID/tags" $Method = "POST" #your tag i.e. label you want to picture to be tagged $tagname = "castle" $TagUrl = $APICreateTag+"?name="+$tagname $properties = @{ Uri = $TagUrl Headers = $Header Method = $Method } #Call API $resultAPITag = Invoke-RestMethod @properties #Pick the tagId to variable be later used $TagId = $resultAPITag.id #####################Get tags example############### $header = @{'Training-Key'= '<insert your key>'} $ProjectID = "<insert your project ID>" $url = "https://northeurope.api.cognitive.microsoft.com/customvision/v3.0/training/projects/$ProjectID/tags/" $Method = "GET" $ContentType = "application/octet-stream" #your tag i.e. label you want to picture to be tagged $tagname = "castle" $properties = @{ Uri = $url Headers = $Header ContentType = $ContentType Method = $Method } #Call Api $resultAPITag = Invoke-RestMethod @properties #Pick the tagId named 'castle' be used later $TagId = ($resultAPITag | where {$_.name -like "*$tagname*"}).id #####################Upload Base64 encoded Image from Local File example############## $header = @{'Training-Key'= '<insert your key>'} $ProjectID = "<insert your project ID>" $APIFileBase64 = "https://northeurope.api.cognitive.microsoft.com/customvision/v3.0/training/projects/$ProjectID/images/files" $Method = "POST" $ContentType = "application/json" #Picture to be categorized $pic = Get-childitem "C:\Skriptit\AzureCustomVisionAI\TrainingImages\Tietoturvaspace.JPG" #Uncomment this if you didn't create Tag #$TagId = "" $tagToBeUsed = $TagId try { #Encoding to base64-image to be delivered to Custom Vision AI $encodedimage = [Convert]::ToBase64String([IO.File]::ReadAllBytes($pic)) } #try catch { Write-Host $Error Write-Warning "base64 encoding failed. Exiting" Exit } #catch $jsonbody = "{ 'images': [ { 'name': 'Castle.jpg', 'contents': '$encodedimage', 'tagIds': ['$tagToBeUsed'], 'regions': [] } ] }" $properties = @{ Uri = $APIFileBase64 Headers = $Header ContentType = $ContentType Method = $Method Body = $jsonbody } #Call API $resultAPI = Invoke-RestMethod @properties ########################Upload local file directly, not using Base64 encoding example############## $header = @{'Training-Key'= '<insert your key>'} $ProjectID = "<insert your project ID>" $ContentType = "application/octet-stream" $Method = "POST" $APIFileRaw = "https://northeurope.api.cognitive.microsoft.com/customvision/v3.0/training/projects/$ProjectID/images" #Comment this if you DID NOT create Tag $APIFileRaw = "https://northeurope.api.cognitive.microsoft.com/customvision/v3.0/training/projects/$ProjectID/images?tagIds=[$TagId]" #Picture to be categorized $pic = Get-childitem "C:\Skriptit\AzureCustomVisionAI\TrainingImages\Tietoturvaspace.JPG" $properties = @{ Uri = $APIFileRaw Headers = $Header ContentType = $ContentType Method = $Method Infile = $pic } $resultAPI = Invoke-RestMethod @properties #####################Upload Image from URL example############### $header = @{'Training-Key'= '<insert your key>'} $ProjectID = "<insert your project ID>" $APIurl = "https://northeurope.api.cognitive.microsoft.com/customvision/v3.0/training/projects/$ProjectID/images/urls" $Method = "POST" $ContentType = "application/json" $PicUrl ="https://crackshome.com/wp-content/uploads/2018/10/microsoft-security-essentials-mse-250x250.png" $jsonbod = @" { 'images': [ { 'URL': '$PicUrl' } ], 'tagIds': ['$tagToBeUsed'] } "@ $properties = @{ Uri = $APIurl Headers = $Header ContentType = $ContentType Method = $Method Body = $jsonbod } #Call API $resultAPI = Invoke-RestMethod @properties |