CustomVisionAITrainingExamples.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

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