Esempio n. 1
0
        public async Task <JsonResult> Predict(string imageData)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(imageData))
                {
                    var    imageClient = new VisionServiceClient(AppSettings.ImageService_ApiKey, AppSettings.ImageService_ApiEndpoint, AppSettings.ImageService_ProjectId, AppSettings.ImageService_ModelName);
                    var    imgd        = imageData.Replace("data:image/png;base64,", string.Empty);
                    byte[] bytes       = Convert.FromBase64String(imgd);
                    using (MemoryStream ms = new MemoryStream(bytes))
                    {
                        var predictionResult = await imageClient.PredictImage(ms);

                        //Select into DTO so each probability can be rounded
                        var newResults = predictionResult.Predictions.Select(n =>
                                                                             new AzureDraw.DTO.PredictionModel {
                            TagName = n.TagName, Probability = n.Probability
                        });
                        foreach (var item in newResults)
                        {
                            item.Probability = Math.Round(item.Probability, 6);
                        }
                        var topResult = newResults.OrderByDescending(p => Math.Round(p.Probability, 6)).First();

                        return(Json(topResult));
                    }
                }
                else
                {
                    return(Json(string.Empty));
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Json("Error"));
            }
        }