Skip to content

Commit 78dcf9c

Browse files
author
Larry Franks
committed
writing rest api tutorial
1 parent bbf093e commit 78dcf9c

File tree

1 file changed

+148
-13
lines changed

1 file changed

+148
-13
lines changed

articles/cognitive-services/Custom-Vision-Service/rest-api-tutorial.md

Lines changed: 148 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Create a Custom Vision Service REST API tutorial - Azure Cognitive Services | Microsoft Docs
2+
title: Use the a Custom Vision Service REST API - Azure Cognitive Services | Microsoft Docs
33
description: Use the REST API to create, train, test, and export a custom vision model.
44
services: cognitive-services
55
author: blackmist
@@ -13,24 +13,25 @@ ms.author: larryfr
1313
---
1414
# Tutorial: Use the Custom Vision REST API
1515

16-
Learn how to use the Custom Vision REST API to create, train, score, and export a model.
16+
Learn how to use the Custom Vision REST API to create, train, test, and export a model.
1717

18-
The information in this document demonstrates how to use a REST client to work with the REST API for training the Custom Vision service. The examples show how to use the API using the [curl]() utility from a bash environment, or Windows PowerShell.
18+
The information in this document demonstrates how to use a REST client to work with the REST API for training the Custom Vision service. The examples show how to use the API using the `curl` utility from a bash environment and `Invoke-WebRequest` from Windows PowerShell.
1919

2020
> [!div class="checklist"]
2121
> * Get keys
2222
> * Create a project
2323
> * Create tags
2424
> * Add images
25-
> * Use your own model
25+
> * Train and test the model
26+
> * Export the model
2627
2728
## Prerequisites
2829

2930
* A basic familiarity with Representational State Transfer (REST). This document does not go into details on things like HTTP verbs, JSON, or other things commonly used with REST.
3031

31-
* Either a bash (Bourne Again Shell) with the [curl]() utility or Windows PowerShell 3.0 (or greater).
32+
* Either a bash (Bourne Again Shell) with the [curl](https://curl.haxx.se) utility or Windows PowerShell 3.0 (or greater).
3233

33-
* An Custom Vision account. For more information, see the [Build a classifier](getting-started-build-a-classifier.md) document.
34+
* A Custom Vision account. For more information, see the [Build a classifier](getting-started-build-a-classifier.md) document.
3435

3536
## Get keys
3637

@@ -97,7 +98,7 @@ For more information on this request, see [CreateProject](https://southcentralus
9798

9899
### Specific domains
99100

100-
To create a project for a specific domain, you can provide the __domain Id__ as an optional paramter. The following examples show how to retrieve a list of available domains:
101+
To create a project for a specific domain, you can provide the __domain ID__ as an optional parameter. The following examples show how to retrieve a list of available domains:
101102

102103
```bash
103104
curl -X GET "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/domains" -H "Training-Key: $TRAININGKEY" --data-ascii ""
@@ -158,9 +159,7 @@ $resp.Content
158159

159160
## Create tags
160161

161-
When tagging images, you use a tag Id. To get a tag Id, you must create a tag or get the information for an existing tag.
162-
163-
The following example demonstrates how to create a new tag named `cat`. Replace `{projectId}` with the ID of your project. Use the `name=` parameter to specify the name of the tag:
162+
To tag images, you must use a tag ID. The following example demonstrates how to create a new tag named `cat` and get a tag ID. Replace `{projectId}` with the ID of your project. Use the `name=` parameter to specify the name of the tag:
164163

165164
```bash
166165
curl -X POST "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/tags?name=cat" -H "Training-Key: $TRAININGKEY" --data-ascii ""
@@ -174,15 +173,15 @@ $resp = Invoke-WebRequest -Method 'POST' `
174173
$resp.Content
175174
```
176175

177-
The response to the request is similar to the following:
176+
The response to the request is similar to the JSON document:
178177

179178
```json
180179
{"id":"ed6f7ab6-5132-47ad-8649-3ec42ee62d43","name":"cat","description":null,"imageCount":0}
181180
```
182181

183182
Save the `id` value, as it is used when tagging images.
184183

185-
For more information on this requst, see [CreateTag](https://southcentralus.dev.cognitive.microsoft.com/docs/services/d0e77c63c39c4259a298830c15188310/operations/5a59953940d86a0f3c7a829d).
184+
For more information on this request, see [CreateTag](https://southcentralus.dev.cognitive.microsoft.com/docs/services/d0e77c63c39c4259a298830c15188310/operations/5a59953940d86a0f3c7a829d).
186185

187186
## Add images
188187

@@ -201,7 +200,7 @@ $resp = Invoke-WebRequest -Method 'POST' `
201200
$resp.Content
202201
```
203202

204-
The response to the request is similar to the following:
203+
The response to the request is similar to the following JSON document:
205204

206205
```json
207206
{
@@ -244,3 +243,139 @@ The response to the request is similar to the following:
244243

245244
For more information on this request, see [CreateImagesFromUrls](https://southcentralus.dev.cognitive.microsoft.com/docs/services/d0e77c63c39c4259a298830c15188310/operations/5a59953940d86a0f3c7a8287).
246245

246+
## Train the model
247+
248+
The following examples demonstrate how to train the model. Replace `{projectId}` with the ID of your project:
249+
250+
```bash
251+
curl -X POST "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/train" -H "Training-Key: $TRAININGKEY" -H "Content-Type: application/json" --data-ascii ""
252+
```
253+
254+
```powershell
255+
$resp = Invoke-WebRequest -Method 'POST' `
256+
-Uri "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/train" `
257+
-UseBasicParsing `
258+
-Headers @{ "Training-Key"="$trainingKey"; "Content-Type"="application/json" }
259+
$resp.Content
260+
```
261+
262+
The response to the request is similar to the following JSON document:
263+
264+
```json
265+
{
266+
"id": "23de09d6-42a1-413e-839e-8db6ee6d3496",
267+
"name": "Iteration 1",
268+
"isDefault": false,
269+
"status": "Training",
270+
"created": "2018-08-10T17:39:02.5766667",
271+
"lastModified": "2018-08-16T17:15:07.5250661",
272+
"projectId": "45d1b19b-69b8-4b22-8e7e-d1ca37504686",
273+
"exportable": false,
274+
"domainId": null
275+
}
276+
```
277+
278+
Save the `id` value, as it is used to test and export the model.
279+
280+
For more information, see [TrainProject](https://southcentralus.dev.cognitive.microsoft.com/docs/services/d0e77c63c39c4259a298830c15188310/operations/5a59953940d86a0f3c7a8294).
281+
282+
## Test the model
283+
284+
The following examples demonstrate how to perform a test of the model. Replace `{projectId}` with the ID of your project. Replace `{iterationId}` with the ID returned from training the model. Replace `https://linktotestimage` with the path to the test image.
285+
286+
```bash
287+
curl -X POST "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/quicktest/url?iterationId={iterationId}" -H "Training-Key: $TRAININGKEY" -H "Content-Type: application/json" --data-ascii '{"url":"https://linktotestimage"}'
288+
```
289+
290+
```powershell
291+
$resp = Invoke-WebRequest -Method 'POST' `
292+
-Uri "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/quicktest/url?iterationId={iterationId}" `
293+
-UseBasicParsing `
294+
-Headers @{ "Training-Key"="$trainingKey"; "Content-Type"="application/json" } `
295+
-Body '{"url":"https://linktotestimage"}'
296+
$resp.Content
297+
```
298+
299+
The response to the request is similar to the following JSON document:
300+
301+
```json
302+
{
303+
"id": "369b010b-2a92-4f48-a918-4c1a0af91888",
304+
"project": "45d1b19b-69b8-4b22-8e7e-d1ca37504686",
305+
"iteration": "23de09d6-42a1-413e-839e-8db6ee6d3496",
306+
"created": "2018-08-16T17:39:20.7944508Z",
307+
"predictions": [
308+
{
309+
"probability": 0.8390652,
310+
"tagId": "ed6f7ab6-5132-47ad-8649-3ec42ee62d43",
311+
"tagName": "cat"
312+
}
313+
]
314+
}
315+
```
316+
317+
For more information, see [QuickTestImageUrl](https://southcentralus.dev.cognitive.microsoft.com/docs/services/d0e77c63c39c4259a298830c15188310/operations/5a59953940d86a0f3c7a828d).
318+
319+
## Export the model
320+
321+
Exporting a model is a two-step process. First you must specify the model format, and then request the URL for the exported model.
322+
323+
### Request a model export
324+
325+
The following examples demonstrate how to export a `coreml` model. Replace `{projectId}` with the ID of your project. Replace `{iterationId}` with the ID returned from training the model.
326+
327+
```bash
328+
curl -X POST "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/iterations/{iterationId}/export?platform=coreml" -H "Training-Key: $TRAININGKEY" -H "Content-Type: application/json" --data-ascii ''
329+
```
330+
331+
```powershell
332+
$resp = Invoke-WebRequest -Method 'POST' `
333+
-Uri "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/iterations/{iterationId}/export?platform=coreml" `
334+
-UseBasicParsing `
335+
-Headers @{ "Training-Key"="$trainingKey"; "Content-Type"="application/json" }
336+
$resp.Content
337+
```
338+
339+
The response to the request is similar to the following JSON document:
340+
341+
```json
342+
{
343+
"platform": "CoreML",
344+
"status": "Exporting",
345+
"downloadUri": null,
346+
"flavor": null
347+
}
348+
```
349+
350+
For more information, see [ExportIteration](https://southcentralus.dev.cognitive.microsoft.com/docs/services/d0e77c63c39c4259a298830c15188310/operations/5a59953940d86a0f3c7a829b).
351+
352+
### Download the exported model
353+
354+
The following examples demonstrate how to retrieve the URL of the exported model. Replace `{projectId}` with the ID of your project. Replace `{iterationId}` with the ID returned from training the model.
355+
356+
```bash
357+
curl -X GET "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/iterations/{iterationId}/export" -H "Training-Key: $TRAININGKEY" -H "Content-Type: application/json" --data-ascii ''
358+
```
359+
360+
```powershell
361+
$resp = Invoke-WebRequest -Method 'GET' `
362+
-Uri "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Training/projects/{projectId}/iterations/{iterationId}/export" `
363+
-UseBasicParsing `
364+
-Headers @{ "Training-Key"="$trainingKey"; "Content-Type"="application/json" }
365+
$resp.Content
366+
```
367+
368+
The response to the request is similar to the following JSON document:
369+
370+
```json
371+
[
372+
{
373+
"platform": "CoreML",
374+
"status": "Done",
375+
"downloadUri": "https://linktoexportedmodel",
376+
"flavor": null
377+
}
378+
]
379+
```
380+
381+
For more information, see [GetExports](https://southcentralus.dev.cognitive.microsoft.com/docs/services/d0e77c63c39c4259a298830c15188310/operations/5a59953940d86a0f3c7a829a).

0 commit comments

Comments
 (0)