Skip to content

Commit ea86c6b

Browse files
committed
change endpoint language and use region-based snippets
1 parent 4807899 commit ea86c6b

File tree

2 files changed

+23
-82
lines changed

2 files changed

+23
-82
lines changed

articles/cognitive-services/Custom-Vision-Service/csharp-tutorial-od.md

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,84 +36,58 @@ This Visual Studio project creates a new Custom Vision project named __My New Pr
3636

3737
## Understand the code
3838

39-
Open the _Program.cs_ file and inspect the code. Insert your subscription keys in the appropriate definitions in the **Main** method.
39+
Open the _Program.cs_ file and inspect the code. [Create environment variables](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account#configure-an-environment-variable-for-authentication) for your training and prediction keys named `CUSTOM_VISION_TRAINING_KEY` and `CUSTOM_VISION_PREDICTION_KEY`, respectively. The script will look for these.
4040

41-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?range=18-27)]
41+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_keys)]
4242

43-
The Endpoint parameter should point to the region where the Azure resource group containing the Custom Vision resources was created in. For this example, we assume the South Central US region and use:
43+
Also, get your Endpoint URL by selecting your Custom Vision resource in the Azure portal. Save it to an environment variable called `CUSTOM_VISION_ENDPOINT`. The script saves a reference to it at the root of your class.
4444

45-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?range=14-14)]
45+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_endpoint)]
4646

4747
### Create a new Custom Vision Service project
4848

4949
This next bit of code creates an object detection project. The created project will show up on the [Custom Vision website](https://customvision.ai/) that you visited earlier. See the [CreateProject](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.training.customvisiontrainingclientextensions.createproject?view=azure-dotnet#Microsoft_Azure_CognitiveServices_Vision_CustomVision_Training_CustomVisionTrainingClientExtensions_CreateProject_Microsoft_Azure_CognitiveServices_Vision_CustomVision_Training_ICustomVisionTrainingClient_System_String_System_String_System_Nullable_System_Guid__System_String_System_Collections_Generic_IList_System_String__) method to specify other options when you create your project (explained in the [Build a detector](get-started-build-detector.md) web portal guide).
5050

51-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?range=29-35)]
51+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_create)]
52+
5253

5354
### Add tags to the project
5455

55-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?range=37-39)]
56+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_tags)]
5657

5758
### Upload and tag images
5859

5960
When you tag images in object detection projects, you need to specify the region of each tagged object using normalized coordinates. The following code associates each of the sample images with its tagged region.
6061

61-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?range=41-84)]
62+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_upload_regions)]
6263

6364
Then, this map of associations is used to upload each sample image with its region coordinates. You can upload up to 64 images in a single batch.
6465

65-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?range=86-104)]
66+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_upload)]
6667

6768
At this point, all of the sample images have been uploaded, and each has a tag (**fork** or **scissors**) and an associated pixel rectangle for that tag.
6869

6970
### Train the project
7071

7172
This code creates the first training iteration in the project.
7273

73-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?range=106-117)]
74+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_train)]
7475

7576
### Publish the current iteration
7677

7778
The name given to the published iteration can be used to send prediction requests. An iteration is not available in the prediction endpoint until it is published.
7879

79-
```csharp
80-
// The iteration is now trained. Publish it to the prediction end point.
81-
var publishedModelName = "treeClassModel";
82-
var predictionResourceId = "<target prediction resource ID>";
83-
trainingApi.PublishIteration(project.Id, iteration.Id, publishedModelName, predictionResourceId);
84-
Console.WriteLine("Done!\n");
85-
```
80+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_publish)]
8681

8782
### Create a prediction endpoint
8883

89-
```csharp
90-
// Create a prediction endpoint, passing in the obtained prediction key
91-
CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
92-
{
93-
ApiKey = predictionKey,
94-
Endpoint = SouthCentralUsEndpoint
95-
};
96-
```
84+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_prediction_endpoint)]
9785

9886
### Use the prediction endpoint
9987

10088
This part of the script loads the test image, queries the model endpoint, and outputs prediction data to the console.
10189

102-
```csharp
103-
// Make a prediction against the new project
104-
Console.WriteLine("Making a prediction:");
105-
var imageFile = Path.Combine("Images", "test", "test_image.jpg");
106-
using (var stream = File.OpenRead(imageFile))
107-
{
108-
var result = endpoint.DetectImage(project.Id, publishedModelName, stream);
109-
110-
// Loop over each prediction and write out the results
111-
foreach (var c in result.Predictions)
112-
{
113-
Console.WriteLine($"\t{c.TagName}: {c.Probability:P1} [ {c.BoundingBox.Left}, {c.BoundingBox.Top}, {c.BoundingBox.Width}, {c.BoundingBox.Height} ]");
114-
}
115-
}
116-
```
90+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ObjectDetection/Program.cs?name=snippet_prediction)]
11791

11892
## Run the application
11993

articles/cognitive-services/Custom-Vision-Service/csharp-tutorial.md

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,82 +35,49 @@ This Visual Studio project creates a new Custom Vision project named __My New Pr
3535

3636
## Understand the code
3737

38-
Open the _Program.cs_ file and inspect the code. Insert your subscription keys in the appropriate definitions in the **Main** method.
38+
Open the _Program.cs_ file and inspect the code. [Create environment variables](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account#configure-an-environment-variable-for-authentication) for your training and prediction keys named `CUSTOM_VISION_TRAINING_KEY` and `CUSTOM_VISION_PREDICTION_KEY`, respectively. The script will look for these.
3939

40-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?range=21-30)]
40+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_keys)]
4141

42-
The Endpoint parameter should point to the region where the Azure resource group containing the Custom Vision resources was created in. For this example, we assume the South Central US region and use:
42+
Also, get your Endpoint URL by selecting your Custom Vision resource in the Azure portal. Save it to an environment variable called `CUSTOM_VISION_ENDPOINT`. The script saves a reference to it at the root of your class.
4343

44-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?range=14-14)]
44+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_endpoint)]
4545

4646
The following lines of code execute the primary functionality of the project.
4747

4848
### Create a new Custom Vision service project
4949

5050
The created project will show up on the [Custom Vision website](https://customvision.ai/) that you visited earlier. See the [CreateProject](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.training.customvisiontrainingclientextensions.createproject?view=azure-dotnet#Microsoft_Azure_CognitiveServices_Vision_CustomVision_Training_CustomVisionTrainingClientExtensions_CreateProject_Microsoft_Azure_CognitiveServices_Vision_CustomVision_Training_ICustomVisionTrainingClient_System_String_System_String_System_Nullable_System_Guid__System_String_System_Collections_Generic_IList_System_String__) method to specify other options when you create your project (explained in the [Build a classifier](getting-started-build-a-classifier.md) web portal guide).
5151

52-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?range=32-34)]
52+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_create)]
5353

5454
### Create tags in the project
5555

56-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?range=36-38)]
56+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_tags)]
5757

5858
### Upload and tag images
5959

6060
The images for this project are included. They are referenced in the **LoadImagesFromDisk** method in _Program.cs_. You can upload up to 64 images in a single batch.
6161

62-
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?range=40-55)]
62+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_upload)]
6363

6464
### Train the classifier and publish
6565

6666
This code creates the first iteration in the project and then publishes that iteration to the prediction endpoint. The name given to the published iteration can be used to send prediction requests. An iteration is not available in the prediction endpoint until it is published.
6767

68-
```csharp
69-
var iteration = trainingApi.TrainProject(project.Id);
70-
// The returned iteration will be in progress, and can be queried periodically to see when it has completed
71-
while (iteration.Status == "Training")
72-
{
73-
Thread.Sleep(1000);
74-
75-
// Re-query the iteration to get it's updated status
76-
iteration = trainingApi.GetIteration(project.Id, iteration.Id);
77-
}
78-
79-
// The iteration is now trained. Publish it to the prediction end point.
80-
var publishedModelName = "treeClassModel";
81-
var predictionResourceId = "<target prediction resource ID>";
82-
trainingApi.PublishIteration(project.Id, iteration.Id, publishedModelName, predictionResourceId);
83-
Console.WriteLine("Done!\n");
84-
```
68+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_train)]
8569

8670
### Set the prediction endpoint
8771

8872
The prediction endpoint is the reference that you can use to submit an image to the current model and get a classification prediction.
8973

90-
```csharp
91-
// Create a prediction endpoint, passing in obtained prediction key
92-
CustomVisionPredictionClient endpoint = new CustomVisionPredictionClient()
93-
{
94-
ApiKey = predictionKey,
95-
Endpoint = SouthCentralUsEndpoint
96-
};
97-
```
74+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_prediction_endpoint)]
9875

9976
### Submit an image to the default prediction endpoint
10077

10178
In this script, the test image is loaded in the **LoadImagesFromDisk** method, and the model's prediction output is to be displayed in the console. The value of the publishedModelName variable should correspond to the "Published as" value found on the Custom Vision portal's **Performance** tab.
10279

103-
```csharp
104-
// Make a prediction against the new project
105-
Console.WriteLine("Making a prediction:");
106-
var result = endpoint.ClassifyImage(project.Id, publishedModelName, testImage);
107-
108-
// Loop over each prediction and write out the results
109-
foreach (var c in result.Predictions)
110-
{
111-
Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}");
112-
}
113-
```
80+
[!code-csharp[](~/cognitive-services-dotnet-sdk-samples/CustomVision/ImageClassification/Program.cs?name=snippet_prediction)]
11481

11582
## Run the application
11683

0 commit comments

Comments
 (0)