项目作者: CarlosSardo

项目描述 :
Azure Cognitive Services Samples
高级语言: C#
项目地址: git://github.com/CarlosSardo/CognitiveServicesSamples.git
创建时间: 2017-09-04T13:01:55Z
项目社区:https://github.com/CarlosSardo/CognitiveServicesSamples

开源协议:MIT License

下载


Azure Cognitive Services Samples

A simple .NET Core 2.0 (c# v7.1) code sample to invoke Azure’s Custom Vision Service Prediction API using Refit

Getting started

  • Visual Studio 2017 (v15.3.3), .NET Core 2.0 installed
  • Head to the Custom Vision Service quickstarts and follow the basic steps to build a classifier that you’d like.
  • Get your Prediction API Host and Key in https://customvision.ai > Performance > Prediction URL
  • In the repo source, replace YOUR-PREDICTION-API-BASE-HOST in Program.cs with the Base API Host

    1. class Program
    2. {
    3. static async Task Main(string[] args)
    4. {
    5. if (args.Length == 0)
    6. {
    7. return;
    8. }
    9. string imageUrl = args[0];
    10. var data = new Dictionary<string, object> { { "url", imageUrl }, };
    11. // Example: "https://southcentralus.api.cognitive.microsoft.com/customvision"
    12. var customVisionPredictionApi = RestService.For<ICustomVisionPredictionApi>("<YOUR-PREDICTION-API-BASE-HOST>");
    13. var result = await customVisionPredictionApi.PredictImageByUrlAsync(data);
    14. Console.WriteLine($"Project: {result.Project}, Id: {result.Id}, Created: {result.Created}, Iteration: {result.Iteration}");
    15. foreach (var prediction in result.Predictions)
    16. {
    17. Console.WriteLine($"Tag Id: {prediction.TagId}, Tag: {prediction.Tag}, Probability: {prediction.Probability}");
    18. }
    19. Console.ReadKey();
    20. }
    21. }
  • And replace YOUR-PREDICTION-API-RESOURCE-ENDPOINT and YOUR-PREDICTION-KEY in ICustomVisionPredictionApi.cs

  1. public interface ICustomVisionPredictionApi
  2. {
  3. // Example: [Post("/v1.0/Prediction/ff76e214-c6ab-4eb2-bb2c-d34c3ca65bcc/url")]
  4. [Post("<YOUR-PREDICTION-API-RESOURCE-ENDPOINT>")]
  5. // Example: [Headers("Prediction-Key: a63298872abc4e7deabcd78beec819a2", "Content-Type: application/json; charset=UTF-8")]
  6. [Headers("Prediction-Key: <YOUR-PREDICTION-KEY>", "Content-Type: application/json; charset=UTF-8")]
  7. Task<PredictionResponse> PredictImageByUrlAsync([Body(BodySerializationMethod.Json)] Dictionary<string, object> content);
  8. }
  • Go to the command line, to the directory where you cloned the repo:
    1. > dotnet restore
    2. > dotnet build src/CognitiveServicesSamples.sln
    3. > dotnet run --project src/CustomVision/CustomVision.csproj https://website-example.com/images/image.jpg
  • And enjoy the console output (should be something like this):
    1. Project: ed6edb1d-335b-486c-9404-64e5e0fdf319, Id: cbf78812-cbdc-48e4-bbf2-49e5b6a835fe, Created: 4-9-2017 14:28:32, Iteration: b82b484d-d62d-4932-b10a-f2e12966ad74
    2. Tag Id: 2da388ba-1f63-4e5f-9e23-c5ed9c05bdce, Tag: 1040, Probability: 0,9996263
    3. Tag Id: 3634a6bc-58b3-4be1-83b9-9de1654f4841, Tag: 1099, Probability: 2,27343E-14