public ImageClassificationResult Classify(MemoryStream image) { // TODO: the classification depends on System.Drawing which needs some libraries to be installed in mac/linux // There are alternatives to Bitmap, like ImageSharp, but would that work with ML.NET ???? // https://www.hanselman.com/blog/HowDoYouUseSystemDrawingInNETCore.aspx // Can install with: brew install mono-libgdiplus // Convert to Bitmap Bitmap bitmapImage = (Bitmap)Image.FromStream(image); // Set the specific image data into the ImageInputData type used in the DataView ImageInputData imageInputData = new ImageInputData { Image = bitmapImage }; // Predict code for provided image ImageLabelPredictions imageLabelPredictions = _predictionEnginePool.Predict(imageInputData); // Predict the image's label (The one with highest probability) float[] probabilities = imageLabelPredictions.PredictedLabels; var maxProbability = probabilities.Max(); var maxProbabilityIndex = probabilities.AsSpan().IndexOf(maxProbability); return(new ImageClassificationResult() { Label = _labels[maxProbabilityIndex], Probability = maxProbability }); }
public async Task <IActionResult> ClassifyImage(IFormFile imageFile) { if (imageFile.Length == 0) { return(BadRequest()); } var imageMemoryStream = new MemoryStream(); await imageFile.CopyToAsync(imageMemoryStream); // Check that the image is valid. byte[] imageData = imageMemoryStream.ToArray(); if (!imageData.IsValidImage()) { return(StatusCode(StatusCodes.Status415UnsupportedMediaType)); } // Convert to Image. Image image = Image.FromStream(imageMemoryStream); // Convert to Bitmap. Bitmap bitmapImage = (Bitmap)image; _logger.LogInformation("Start processing image..."); // Measure execution time. var watch = System.Diagnostics.Stopwatch.StartNew(); // Set the specific image data into the ImageInputData type used in the DataView. var imageInputData = new ImageInputData { Image = bitmapImage }; // Predict code for provided image. ImageLabelPredictions imageLabelPredictions = _predictionEnginePool.Predict(imageInputData); // Stop measuring time. watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; _logger.LogInformation($"Image processed in {elapsedMs} miliseconds"); // Predict the image's label (The one with highest probability). ImagePredictedLabelWithProbability imageBestLabelPrediction = FindBestLabelWithProbability(imageLabelPredictions, imageInputData); return(Ok(imageBestLabelPrediction)); }
private ImagePredictedLabelWithProbability FindBestLabelWithProbability(ImageLabelPredictions imageLabelPredictions, ImageInputData imageInputData) { // Read TF model's labels (labels.txt) to classify the image across those labels. var labels = ReadLabels(_labelsFilePath); float[] probabilities = imageLabelPredictions.PredictedLabels; // Set a single label as predicted or even none if probabilities were lower than 70%. var imageBestLabelPrediction = new ImagePredictedLabelWithProbability() { ImageId = imageInputData.GetHashCode().ToString(), //This ID is not really needed, it could come from the application itself, etc. }; (imageBestLabelPrediction.PredictedLabel, imageBestLabelPrediction.Probability) = GetBestLabel(labels, probabilities); return(imageBestLabelPrediction); }
private ImagePredictedLabelWithProbability FindBestLabelWithProbability(ImageLabelPredictions imageLabelPredictions, ImageInputData imageInputData) { //Read TF model's labels (labels.txt) to classify the image across those labels var labels = ReadLabels(_labelsFilePath); float[] probabilities = imageLabelPredictions.PredictedLabels; //Set a single label as predicted or even none if probabilities were lower than 70% var imageBestLabelPrediction = new ImagePredictedLabelWithProbability() { ImagePath = imageInputData.ImagePath, }; (imageBestLabelPrediction.PredictedLabel, imageBestLabelPrediction.Probability) = GetBestLabel(labels, probabilities); return(imageBestLabelPrediction); }
public async Task <IActionResult> ClassifyImage(IFormFile imageFile) { if (imageFile.Length == 0) { return(BadRequest()); } //Save the temp image file into the temp-folder IImageFileWriter imageWriter = new ImageFileWriter(); string fileName = await imageWriter.UploadImageAsync(imageFile, _tempImagesFolderPath); string imageFilePath = Path.Combine(_tempImagesFolderPath, fileName); //Convert image stream to byte[] // //byte[] imageData = null; //MemoryStream image = new MemoryStream(); //await imageFile.CopyToAsync(image); //imageData = image.ToArray(); //if (!imageData.IsValidImage()) // return StatusCode(StatusCodes.Status415UnsupportedMediaType); _logger.LogInformation($"Start processing image..."); //Measure execution time var watch = System.Diagnostics.Stopwatch.StartNew(); //Set the specific image data var imageInputData = new ImageInputData { ImagePath = imageFilePath }; //Predict code for provided image ImageLabelPredictions imageLabelPredictions = _predictionEnginePool.Predict(imageInputData); //Stop measuring time watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; _logger.LogInformation($"Image processed in {elapsedMs} miliseconds"); //try //{ // // DELETE FILE WHEN CLOSED // using (FileStream fs = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.DeleteOnClose)) // { // // temp file exists // fs.Close(); // } //} //catch (Exception e) //{ // throw e; //} //Predict the image's label (The one with highest probability) ImagePredictedLabelWithProbability imageBestLabelPrediction = FindBestLabelWithProbability(imageLabelPredictions, imageInputData); //return new ObjectResult(result); return(Ok(imageBestLabelPrediction)); }