public async Task <ImageClassificationResult> ClassifyImage(SoftwareBitmap bitmap) { var endpoint = new PredictionEndpoint() { ApiKey = _Configuration.CognitiveServicesCustomVisionApiKey }; using (var stream = new InMemoryRandomAccessStream()) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.BitmapTransform.Flip = BitmapFlip.Horizontal; encoder.SetSoftwareBitmap(bitmap); await encoder.FlushAsync(); var result = endpoint.PredictImage( _Configuration.CognitiveServicesCustomVisionProjectId, stream.AsStream() ); double highestProbability = 0; ImageTagPredictionModel predictedModel = null; foreach (var prediction in result.Predictions) { if (prediction.Probability > highestProbability) { highestProbability = prediction.Probability; predictedModel = prediction; } } if (predictedModel == null) { return(null); } return(new ImageClassificationResult() { Tag = predictedModel.Tag, Probability = predictedModel.Probability }); } }
async public static Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = nameof(AnalyseCustomImage))] HttpRequestMessage req, TraceWriter log) { using (var analytic = new AnalyticService(new RequestTelemetry { Name = nameof(AnalyseCustomImage) })) { try { var allTags = new List <string>(); var j = JObject.Parse(req.Content.ReadAsStringAsync().Result); var imageUrl = (string)j["imageUrl"]; var treasureId = (string)j["treasureId"]; var gameId = (string)j["gameId"]; var game = CosmosDataService.Instance.GetItemAsync <Game>(gameId).Result; if (game == null) { return(req.CreateErrorResponse(HttpStatusCode.NotFound, "Game not found")); } var treasure = game.Treasures.SingleOrDefault(t => t.Id == treasureId); if (treasure == null) { var data = new Event("Custom image analyzed for treasure failed"); data.Add("hint", treasure.Hint).Add("source", treasure.ImageSource).Add("sent", imageUrl); await EventHubService.Instance.SendEvent(data); return(req.CreateErrorResponse(HttpStatusCode.NotFound, "Treasure not found")); } var endpoint = new PredictionEndpoint { ApiKey = ConfigManager.Instance.CustomVisionPredictionKey }; //This is where we run our prediction against the default iteration var result = endpoint.PredictImageUrl(new Guid(game.CustomVisionProjectId), new ImageUrl(imageUrl)); ImageTagPredictionModel goodTag = null; // Loop over each prediction and write out the results foreach (var prediction in result.Predictions) { if (treasure.Attributes.Any(a => a.Name.Equals(prediction.Tag, StringComparison.InvariantCultureIgnoreCase))) { if (prediction.Probability >= ConfigManager.Instance.CustomVisionMinimumPredictionProbability) { goodTag = prediction; break; } } } { var outcome = goodTag == null ? "failed" : "succeeded"; var data = new Event($"Custom image analyzed for treasure {outcome}"); if (goodTag != null) { data.Add("tags", goodTag.Tag).Add("probability", goodTag.Probability.ToString("P")); } data.Add("hint", treasure.Hint).Add("source", treasure.ImageSource).Add("sent", imageUrl); await EventHubService.Instance.SendEvent(data); } return(req.CreateResponse(HttpStatusCode.OK, goodTag != null)); } catch (Exception e) { analytic.TrackException(e); return(req.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } } }