コード例 #1
0
        public async Task <IEnumerable <LabelConfidence> > Tags(byte[] image, CustomVisionSettings settings)
        {
            // Create a prediction endpoint, passing in obtained prediction key
            var endpoint = new PredictionEndpoint()
            {
                ApiKey = settings.CustomVisionPredictionKey
            };

            Cognitive.CustomVision.Prediction.Models.ImagePredictionResultModel result;

            using (var testImage = new MemoryStream(image))
            {
                // Make a prediction against the prediction project
                // IMPORTANT: the prediction project should have an iteration marked as default.
                // Otherwise, you need to specify an iteration ID
                result = await endpoint.PredictImageWithNoStoreAsync(settings.CustomVisionProjectId, testImage);
            }

            return(result.Predictions.Select(t => new LabelConfidence()
            {
                Label = t.Tag, Probability = (float)t.Probability
            })
                   .Where(c => c.Probability >= settings.Threshold)
                   .OrderByDescending(c => c.Probability));
        }
コード例 #2
0
        async Task Ok()
        {
            IsEnabled = false;
            try
            {
                var endpoint = new PredictionEndpoint
                {
                    ApiKey = KeyService.PK
                };

                if (Guid.TryParse(KeyService.PI, out var pId))
                {
                    var imagePath = "ObjectDetector.Images.single.png";
                    var assembly  = typeof(SettingsViewModel).GetTypeInfo().Assembly;

                    using (var stream = assembly.GetManifestResourceStream(imagePath))
                    {
                        await endpoint.PredictImageWithNoStoreAsync(pId, stream);
                    }

                    await KeyService.SetPredictionKey(KeyService.PK);

                    await KeyService.SetProjectId(KeyService.PI);

                    Analytics.TrackEvent("Updating keys");

                    await Application.Current.MainPage.Navigation.PopModalAsync();
                }
                else
                {
                    Analytics.TrackEvent("Failed updating keys", new Dictionary <string, string> {
                        { "Error", "The project Id is not a valid GUID" }
                    });
                    await Application.Current.MainPage.DisplayAlert("Error", "The project Id is not a valid GUID", "OK");
                }
            }
            catch (Exception ex)
            {
                Analytics.TrackEvent("Failed updating keys", new Dictionary <string, string> {
                    { "Error", "The project Id and prediction key don't match an existing project" }
                });
                Crashes.TrackError(ex, new Dictionary <string, string> {
                    { "Action", "Testing key and project id" }
                });
                await Application.Current.MainPage.DisplayAlert("Error", "The project Id and prediction key don't match an existing project", "OK");
            }
            finally
            {
                IsEnabled = true;
            }
        }