Skip to content

Commit bbe0262

Browse files
committed
finish draft
1 parent f2dc326 commit bbe0262

File tree

1 file changed

+25
-89
lines changed

1 file changed

+25
-89
lines changed

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

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -48,108 +48,44 @@ Once your model has been published, you can retrieve the required information by
4848

4949
This guide assumes that you already constructed a **[CustomVisionPredictionClient](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.customvisionpredictionclient?view=azure-dotnet-preview)** object, named `predictionClient`, with a 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-
In this guide, you'll use a local image, so download an image you'd like to use.
52-
53-
TBD: this method has since been replaced but the ref docs don't reflect that.
54-
The **[PredictImageAsync](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.customvisionpredictionclientextensions.predictimageasync?view=azure-dotnet-preview#Microsoft_Azure_CognitiveServices_Vision_CustomVision_Prediction_CustomVisionPredictionClientExtensions_PredictImageAsync_Microsoft_Azure_CognitiveServices_Vision_CustomVision_Prediction_ICustomVisionPredictionClient_System_Guid_System_IO_Stream_System_Nullable_System_Guid__System_String_System_Threading_CancellationToken_)** method.
51+
In this guide, you'll use a local image, so download an image you'd like to use. The following code gets a local path from the user and gets the bytestream of the file at that path.
5552

5653
```csharp
57-
// Make a prediction against the new project
58-
Console.WriteLine("Making a prediction:");
59-
var result = predictionApi.ClassifyImage(project.Id, publishedModelName, testImage);
54+
Console.Write("Enter image file path: ");
55+
string imageFilePath = Console.ReadLine();
56+
byte[] byteData = GetImageAsByteArray(imageFilePath);
6057
```
61-
## Determine how to process the data
62-
63-
## Get results from the service
64-
65-
[If this is a separate API call, show it here. Then show a sample response value, and explain any parts of the response that aren't intuitive. Explain error cases if they're relevant enough]
66-
67-
68-
---
69-
70-
71-
1. In Visual Studio, create a new C# console application.
72-
73-
1. Use the following code as the body of the __Program.cs__ file.
74-
75-
```csharp
76-
using System;
77-
using System.IO;
78-
using System.Net.Http;
79-
using System.Net.Http.Headers;
80-
using System.Threading.Tasks;
81-
82-
namespace CVSPredictionSample
83-
{
84-
public static class Program
85-
{
86-
public static void Main()
87-
{
88-
Console.Write("Enter image file path: ");
89-
string imageFilePath = Console.ReadLine();
90-
91-
MakePredictionRequest(imageFilePath).Wait();
92-
93-
Console.WriteLine("\n\nHit ENTER to exit...");
94-
Console.ReadLine();
95-
}
9658

97-
public static async Task MakePredictionRequest(string imageFilePath)
98-
{
99-
var client = new HttpClient();
59+
Include the following helper method:
10060

101-
// Request headers - replace this example key with your valid Prediction-Key.
102-
client.DefaultRequestHeaders.Add("Prediction-Key", "<Your prediction key>");
103-
104-
// Prediction URL - replace this example URL with your valid Prediction URL.
105-
string url = "<Your prediction URL>";
106-
107-
HttpResponseMessage response;
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+
```
10869

109-
// Request body. Try this sample with a locally stored image.
110-
byte[] byteData = GetImageAsByteArray(imageFilePath);
70+
The **[ClassifyImageAsync](https://docs.microsoft.com/en-us/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.
11171

112-
using (var content = new ByteArrayContent(byteData))
113-
{
114-
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
115-
response = await client.PostAsync(url, content);
116-
Console.WriteLine(await response.Content.ReadAsStringAsync());
117-
}
118-
}
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+
```
11977

120-
private static byte[] GetImageAsByteArray(string imageFilePath)
121-
{
122-
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
123-
BinaryReader binaryReader = new BinaryReader(fileStream);
124-
return binaryReader.ReadBytes((int)fileStream.Length);
125-
}
126-
}
127-
}
128-
```
78+
## Determine how to process the data
12979

130-
1. Change the following information:
131-
* Set the `namespace` field to the name of your project.
132-
* Replace the placeholder `<Your prediction key>` with the key value you retrieved earlier.
133-
* Replace the placeholder `<Your prediction URL>` with the URL you retrieved earlier.
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/en-us/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.customvisionpredictionclient?view=azure-dotnet)** class). For example, 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.
13481

135-
## Run the application
82+
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.
13683

137-
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.
84+
The **-WithHttpMessages** methods return the raw HTTP response of the API call.
13885

139-
```json
140-
{
141-
"id":"7796df8e-acbc-45fc-90b4-1b0c81b73639",
142-
"project":"8622c779-471c-4b6e-842c-67a11deffd7b",
143-
"iteration":"59ec199d-f3fb-443a-b708-4bca79e1b7f7",
144-
"created":"2019-03-20T16:47:31.322Z",
145-
"predictions":[
146-
{"tagId":"d9cb3fa5-1ff3-4e98-8d47-2ef42d7fb373","tagName":"cat", "probability":1.0},
147-
{"tagId":"9a8d63fb-b6ed-4462-bcff-77ff72084d99","tagName":"dog", "probability":0.1087869}
148-
]
149-
}
150-
```
151-
---
86+
## Get results from the service
15287

88+
The results are returned in the form of an **[ImagePrediction](https://docs.microsoft.com/en-us/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/en-us/dotnet/api/microsoft.azure.cognitiveservices.vision.customvision.prediction.models.predictionmodel?view=azure-dotnet)** objects, which each represent 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.
15389

15490
## Next steps
15591

0 commit comments

Comments
 (0)