Skip to content

Commit 02ea344

Browse files
authored
Merge pull request #178552 from PatrickFarley/cusvis-updates
[cog svcs] Cusvis updates
2 parents 374b779 + 1bd8c30 commit 02ea344

File tree

1 file changed

+39
-77
lines changed

1 file changed

+39
-77
lines changed

articles/cognitive-services/Custom-Vision-Service/use-prediction-api.md

Lines changed: 39 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -14,118 +14,80 @@ ms.author: pafarley
1414
ms.custom: devx-track-csharp
1515
---
1616

17-
# Use your model with the prediction API
17+
# Call the prediction API
18+
19+
After you've trained your model, you can test images programmatically by submitting them to the prediction API endpoint. In this guide, you'll learn how to call the prediction API to score an image. You'll learn the different ways you can configure the behavior of this API to meet your needs.
1820

19-
After you've train your model, you can test it programmatically by submitting images to the Prediction API endpoint.
2021

2122
> [!NOTE]
22-
> This document demonstrates using C# to submit an image to the Prediction API. For more information and examples, see the [Prediction API reference](https://southcentralus.dev.cognitive.microsoft.com/docs/services/Custom_Vision_Prediction_3.0/operations/5c82db60bf6a2b11a8247c15).
23+
> This document demonstrates use of the .NET client library for C# to submit an image to the Prediction API. For more information and examples, see the [Prediction API reference](https://southcentralus.dev.cognitive.microsoft.com/docs/services/Custom_Vision_Prediction_3.0/operations/5c82db60bf6a2b11a8247c15).
24+
25+
## Setup
2326

24-
## Publish your trained iteration
27+
### Publish your trained iteration
2528

2629
From the [Custom Vision web page](https://customvision.ai), select your project and then select the __Performance__ tab.
2730

28-
To submit images to the Prediction API, you will first need to publish your iteration for prediction, which can be done by selecting __Publish__ and specifying a name for the published iteration. This will make your model accessible to the Prediction API of your Custom Vision Azure resource.
31+
To submit images to the Prediction API, you'll first need to publish your iteration for prediction, which can be done by selecting __Publish__ and specifying a name for the published iteration. This will make your model accessible to the Prediction API of your Custom Vision Azure resource.
2932

3033
![The performance tab is shown, with a red rectangle surrounding the Publish button.](./media/use-prediction-api/unpublished-iteration.png)
3134

3235
Once your model has been successfully published, you'll see a "Published" label appear next to your iteration in the left-hand sidebar, and its name will appear in the description of the iteration.
3336

3437
![The performance tab is shown, with a red rectangle surrounding the Published label and the name of the published iteration.](./media/use-prediction-api/published-iteration.png)
3538

36-
## Get the URL and prediction key
39+
### Get the URL and prediction key
3740

3841
Once your model has been published, you can retrieve the required information by selecting __Prediction URL__. This will open up a dialog with information for using the Prediction API, including the __Prediction URL__ and __Prediction-Key__.
3942

4043
![The performance tab is shown with a red rectangle surrounding the Prediction URL button.](./media/use-prediction-api/published-iteration-prediction-url.png)
4144

4245
![The performance tab is shown with a red rectangle surrounding the Prediction URL value for using an image file and the Prediction-Key value.](./media/use-prediction-api/prediction-api-info.png)
4346

47+
## Submit data to the service
4448

45-
In this guide, you will use a local image, so copy the URL under **If you have an image file** to a temporary location. Copy the corresponding __Prediction-Key__ value as well.
46-
47-
## Create the application
48-
49-
1. In Visual Studio, create a new C# console application.
49+
This guide assumes that you already constructed a **[CustomVisionPredictionClient](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.customvisionpredictionclient?view=azure-dotnet-preview)** object, named `predictionClient`, with your Custom Vision prediction key and endpoint URL. For instructions on how to set up this feature, follow one of the [quickstarts](quickstarts/image-classification.md).
5050

51-
1. Use the following code as the body of the __Program.cs__ file.
51+
In this guide, you'll use a local image, so download an image you'd like to submit to your trained model. The following code prompts the user to specify a local path and gets the bytestream of the file at that path.
5252

53-
```csharp
54-
using System;
55-
using System.IO;
56-
using System.Net.Http;
57-
using System.Net.Http.Headers;
58-
using System.Threading.Tasks;
59-
60-
namespace CVSPredictionSample
61-
{
62-
public static class Program
63-
{
64-
public static void Main()
65-
{
66-
Console.Write("Enter image file path: ");
67-
string imageFilePath = Console.ReadLine();
68-
69-
MakePredictionRequest(imageFilePath).Wait();
70-
71-
Console.WriteLine("\n\nHit ENTER to exit...");
72-
Console.ReadLine();
73-
}
53+
```csharp
54+
Console.Write("Enter image file path: ");
55+
string imageFilePath = Console.ReadLine();
56+
byte[] byteData = GetImageAsByteArray(imageFilePath);
57+
```
7458

75-
public static async Task MakePredictionRequest(string imageFilePath)
76-
{
77-
var client = new HttpClient();
59+
Include the following helper method:
7860

79-
// Request headers - replace this example key with your valid Prediction-Key.
80-
client.DefaultRequestHeaders.Add("Prediction-Key", "<Your prediction key>");
61+
```csharp
62+
private static byte[] GetImageAsByteArray(string imageFilePath)
63+
{
64+
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
65+
BinaryReader binaryReader = new BinaryReader(fileStream);
66+
return binaryReader.ReadBytes((int)fileStream.Length);
67+
}
68+
```
8169

82-
// Prediction URL - replace this example URL with your valid Prediction URL.
83-
string url = "<Your prediction URL>";
70+
The **[ClassifyImageAsync](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.customvisionpredictionclientextensions.classifyimageasync?view=azure-dotnet#Microsoft_Azure_CognitiveServices_Vision_CustomVision_Prediction_CustomVisionPredictionClientExtensions_ClassifyImageAsync_Microsoft_Azure_CognitiveServices_Vision_CustomVision_Prediction_ICustomVisionPredictionClient_System_Guid_System_String_System_IO_Stream_System_String_System_Threading_CancellationToken_)** method takes the project ID and the locally stored image, and scores the image against the given model.
8471

85-
HttpResponseMessage response;
72+
```csharp
73+
// Make a prediction against the new project
74+
Console.WriteLine("Making a prediction:");
75+
var result = predictionApi.ClassifyImageAsync(project.Id, publishedModelName, byteData);
76+
```
8677

87-
// Request body. Try this sample with a locally stored image.
88-
byte[] byteData = GetImageAsByteArray(imageFilePath);
78+
## Determine how to process the data
8979

90-
using (var content = new ByteArrayContent(byteData))
91-
{
92-
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
93-
response = await client.PostAsync(url, content);
94-
Console.WriteLine(await response.Content.ReadAsStringAsync());
95-
}
96-
}
80+
You can optionally configure how the service does the scoring operation by choosing alternate methods (see the methods of the **[CustomVisionPredictionClient](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.customvisionpredictionclient?view=azure-dotnet)** class).
9781

98-
private static byte[] GetImageAsByteArray(string imageFilePath)
99-
{
100-
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
101-
BinaryReader binaryReader = new BinaryReader(fileStream);
102-
return binaryReader.ReadBytes((int)fileStream.Length);
103-
}
104-
}
105-
}
106-
```
82+
You can use a non-async version of the method above for simplicity, but it may cause the program to lock up for a noticeable amount of time.
10783

108-
1. Change the following information:
109-
* Set the `namespace` field to the name of your project.
110-
* Replace the placeholder `<Your prediction key>` with the key value you retrieved earlier.
111-
* Replace the placeholder `<Your prediction URL>` with the URL you retrieved earlier.
84+
The **-WithNoStore** methods require that the service does not retain the prediction image after prediction is complete. Normally, the service retains these images so you have the option of adding them as training data for future iterations of your model.
11285

113-
## Run the application
86+
The **-WithHttpMessages** methods return the raw HTTP response of the API call.
11487

115-
When you run the application, you are prompted to enter a path to an image file in the console. The image is then submitted to the Prediction API, and the prediction results are returned as a JSON-formatted string. The following is an example response.
88+
## Get results from the service
11689

117-
```json
118-
{
119-
"id":"7796df8e-acbc-45fc-90b4-1b0c81b73639",
120-
"project":"8622c779-471c-4b6e-842c-67a11deffd7b",
121-
"iteration":"59ec199d-f3fb-443a-b708-4bca79e1b7f7",
122-
"created":"2019-03-20T16:47:31.322Z",
123-
"predictions":[
124-
{"tagId":"d9cb3fa5-1ff3-4e98-8d47-2ef42d7fb373","tagName":"cat", "probability":1.0},
125-
{"tagId":"9a8d63fb-b6ed-4462-bcff-77ff72084d99","tagName":"dog", "probability":0.1087869}
126-
]
127-
}
128-
```
90+
The service returns results in the form of an **[ImagePrediction](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.models.imageprediction?view=azure-dotnet)** object. The **Predictions** property contains a list of **[PredictionModel](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.models.predictionmodel?view=azure-dotnet)** objects, which each represents a single object prediction. They include the name of the label and the bounding box coordinates where the object was detected in the image. Your app can then parse this data to, for example, display the image with labeled object fields on a screen.
12991

13092
## Next steps
13193

0 commit comments

Comments
 (0)