public async Task <PredictImageResult> PredictImageAsync(IFormFile formFile) { var filePath = Path.GetTempFileName(); if (formFile != null && formFile.Length > 0) { var stream = new FileStream(filePath, FileMode.Create); await formFile.CopyToAsync(stream); stream.Close(); using (var memoryStream = new MemoryStream(System.IO.File.ReadAllBytes(filePath))) { PredictImageResult predictImageResult = await new ClassificationService().PredictImage(memoryStream); //do some processing return(predictImageResult); } } else { throw new Exception("formFile not found or empty file"); } //foreach (var formFile in files) //{ // if (formFile.Length > 0) // { // using (var stream = new FileStream(filePath, FileMode.Create)) // { // await formFile.CopyToAsync(stream); // } // } //} ////var formFile = files.FirstOrDefault(); }
public async Task <PredictImageResult> PredictImage(Stream testImage) { string trainingKey = "6308b3b62b344e3f8e4170c4728deed2"; string predictionKey = "afdffbaa498445c1830aa18ee9216e0b"; // Create a prediction endpoint, passing in obtained prediction key PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = predictionKey }; TrainingApi trainingApi = new TrainingApi() { ApiKey = trainingKey }; var projects = await trainingApi.GetProjectsAsync(); var project = projects.First(f => f.Name == "WA-SE-AI"); try { var result = await endpoint.PredictImageAsync(project.Id, testImage); var tags = await trainingApi.GetTagsAsync(project.Id); // Loop over each prediction and write out the results foreach (var c in result.Predictions) { Console.WriteLine($"\t{c.TagName}: {c.Probability:P1}"); } var topPrediction = result.Predictions.OrderByDescending(m => m.Probability).First(); PredictImageResult predictImageResult = new PredictImageResult { PredictionModel = topPrediction, Tag = tags.FirstOrDefault(f => f.Id == topPrediction.TagId) }; return(predictImageResult); } catch (Exception e) { throw new Exception("PredictImage failed"); } }