public static void ConsoleWrite(this ImagePredictedLabelWithProbability self) { var defaultForeground = Console.ForegroundColor; var labelColor = ConsoleColor.Magenta; var probColor = ConsoleColor.Blue; var exactLabel = ConsoleColor.Green; var failLabel = ConsoleColor.Red; Console.Write("ImagePath: "); Console.ForegroundColor = labelColor; Console.Write($"{Path.GetFileName(self.ImagePath)}"); Console.ForegroundColor = defaultForeground; Console.ForegroundColor = defaultForeground; Console.Write(" predicted as "); Console.ForegroundColor = exactLabel; Console.Write($"{self.PredictedLabel}"); Console.ForegroundColor = defaultForeground; Console.Write(" with probability "); Console.ForegroundColor = probColor; Console.Write(self.Probability); Console.ForegroundColor = defaultForeground; Console.WriteLine(""); }
public async Task <IActionResult> ClassifyImage(IFormFile imageFile) { if (imageFile.Length == 0) { return(BadRequest()); } //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)); } // DELETE FILE WHEN CLOSED // using (FileStream fs = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.RandomAccess | FileOptions.DeleteOnClose)) // { // temp file exists } _logger.LogInformation($"Start processing image file..."); //Measure execution time var watch = System.Diagnostics.Stopwatch.StartNew(); //Predict the image's label (The one with highest probability) ImagePredictedLabelWithProbability imageLabelPrediction = null; //imageLabelPrediction = _modelScorer.PredictLabelForImage(imageData, imageFilePath); ModelInput sampleData = new ModelInput() { SentimentText = "Howdy!" }; //Predict sentiment ModelOutput prediction = _predictionEnginePool.Predict(sampleData); //Stop measuring time watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; //imageLabelPrediction.PredictionExecutionTime = elapsedMs; _logger.LogInformation($"Image processed in {elapsedMs} miliseconds"); //return new ObjectResult(result); return(Ok(imageLabelPrediction)); }
private ImagePredictedLabelWithProbability PredictDataUsingModel(ImageInputData imageName) { var image1Probabilities = _model.Predict(imageName).PredictedLabels; var bestLabelPrediction = new ImagePredictedLabelWithProbability(); (bestLabelPrediction.PredictedLabel, bestLabelPrediction.Probability) = ModelHelpers.GetBestLabel(_labels, image1Probabilities); return(bestLabelPrediction); }
protected IEnumerable <ImagePredictedLabelWithProbability> PredictDataUsingModel(string testLocation, string imagesFolder, string labelsLocation, PredictionFunction <ImageInputData, ImageNetPrediction> model) { ConsoleWriteHeader("Classificate images"); Console.WriteLine($"Images folder: {imagesFolder}"); Console.WriteLine($"Training file: {testLocation}"); Console.WriteLine($"Labels file: {labelsLocation}"); var labels = ModelHelpers.ReadLabels(labelsLocation); ///////////////////////////////////////////////////////////////////////////////////// // IMAGE 1 // Predict label for "green-office-chair-test.jpg" var image1 = new ImageInputData { ImagePath = imagesFolder + "\\" + "green-office-chair-test.jpg" }; var image1Probabilities = model.Predict(image1).PredictedLabels; //Set a single label as predicted or even none if probabilities were lower than 70% var image1BestLabelPrediction = new ImagePredictedLabelWithProbability() { ImagePath = image1.ImagePath, }; (image1BestLabelPrediction.PredictedLabel, image1BestLabelPrediction.Probability) = GetBestLabel(labels, image1Probabilities); image1BestLabelPrediction.ConsoleWrite(); yield return(image1BestLabelPrediction); ///////////////////////////////////////////////////////////////////////////////////// // IMAGE 2 // Predict label for "high-metal-office-chair.jpg" var image2 = new ImageInputData { ImagePath = imagesFolder + "\\" + "high-metal-office-chair.jpg" }; var image2Probabilities = model.Predict(image2).PredictedLabels; //Set a single label as predicted or even none if probabilities were lower than 70% var image2BestLabelPrediction = new ImagePredictedLabelWithProbability() { ImagePath = image2.ImagePath, }; (image2BestLabelPrediction.PredictedLabel, image2BestLabelPrediction.Probability) = GetBestLabel(labels, image2Probabilities); image2BestLabelPrediction.ConsoleWrite(); yield return(image1BestLabelPrediction); }
public async Task <IActionResult> ClassifyImage([FromForm] IFormFile Image) { if (Image.Length == 0) { return(BadRequest()); } MemoryStream imageMemoryStream = new MemoryStream(); await Image.CopyToAsync(imageMemoryStream); byte[] imageData = imageMemoryStream.ToArray(); if (!imageData.IsValidImage()) { return(StatusCode(StatusCodes.Status415UnsupportedMediaType)); } _logger.LogInformation("Start Processing Image ...."); Stopwatch watch = Stopwatch.StartNew(); InMemoryImageData imageInputData = new InMemoryImageData(image: imageData, label: null, imageFileName: null); var prediction = _predictionEnginePool.Predict(imageInputData); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; _logger.LogInformation($"Image processed in { elapsedMs } miliseconds"); string filePath = Path.Combine(_environment.ContentRootPath, "wwwroot", "Images", Image.FileName); using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { await Image.CopyToAsync(fileStream); } ImagePredictedLabelWithProbability imageBestLabelPrediction = new ImagePredictedLabelWithProbability { PredictedLabel = prediction.PredictedLabel, Probability = prediction.Score.Max(), PredictionExecutionTime = elapsedMs, ImageId = Image.FileName }; return(Ok(imageBestLabelPrediction)); }
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)); }
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. var imageData = imageMemoryStream.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 into the ImageInputData type used in the DataView. var imageInputData = new InMemoryImageData(image: imageData, label: null, imageFileName: null); // Predict code for provided image. var prediction = _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). var imageBestLabelPrediction = new ImagePredictedLabelWithProbability { PredictedLabel = prediction.PredictedLabel, Probability = prediction.Score.Max(), PredictionExecutionTime = elapsedMs, ImageId = imageFile.FileName, }; 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 PredictDataUsingModel(string imageName) { var inputImage = new ImageInputData { ImagePath = imageName }; var image1Probabilities = _model.Predict(inputImage).PredictedLabels; //Set a single label as predicted or even none if probabilities were lower than 70% var bestLabelPrediction = new ImagePredictedLabelWithProbability() { ImagePath = inputImage.ImagePath, }; (bestLabelPrediction.PredictedLabel, bestLabelPrediction.Probability) = ModelHelpers.GetBestLabel(_labels, image1Probabilities); return(bestLabelPrediction); }
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); }
static void Main(string[] args) { var modelpath = Path.Combine(Environment.CurrentDirectory, "MLModel", "mlmodel.zip"); var imgspath = Path.Combine(Environment.CurrentDirectory, "ImgDatas"); var labels = System.IO.File.ReadAllLines(Path.Combine(Environment.CurrentDirectory, "MLModel", "labels.txt")); MLContext mlContext = new MLContext(seed: 1); // Load the model ITransformer loadedModel = mlContext.Model.Load(modelpath, out var schema); // Make prediction (input = ImageNetData, output = ImageNetPrediction) var predictor = mlContext.Model.CreatePredictionEngine <ImageInputData, ImageLabelPrediction>(loadedModel); DirectoryInfo imgdir = new DirectoryInfo(imgspath); foreach (var jpgfile in imgdir.GetFiles("*.jpg")) { var imputimg = new ImageInputData { Image = ConvertToBitmap(jpgfile.FullName) }; var pred = predictor.Predict(imputimg); var imgpredresult = new ImagePredictedLabelWithProbability() { Name = jpgfile.Name }; Console.WriteLine($"Filename:{imgpredresult.Name}"); foreach (var item in pred.PredictedLabels) { Console.WriteLine($"Predict Result:{item}"); } var maxresult = pred.PredictedLabels.Max(); if (maxresult >= 0.7) { Console.WriteLine($"Predict Label Result:{labels[pred.PredictedLabels.AsSpan().IndexOf(maxresult)]}"); } Console.WriteLine("===== next image ======"); } }
private IActionResult Classify(byte[] imageData, string filename = null) { // Check that the image is valid if (!imageData.IsValidImage()) { return(StatusCode(StatusCodes.Status415UnsupportedMediaType)); } logger.LogInformation("Start processing image..."); // Measure execution time Stopwatch watch = Stopwatch.StartNew(); // Set the specific image data into the ImageInputData type used in the DataView ImageDataInMemory imageInputData = new ImageDataInMemory(imageData, null, null); // Predict code for provided image ImagePrediction prediction = predictionEnginePool.Predict(imageInputData); // Stop measuring time watch.Stop(); long elapsedTime = watch.ElapsedMilliseconds; logger.LogInformation($"Image processed in {elapsedTime} miliseconds"); // Predict the image's label with highest probability ImagePredictedLabelWithProbability bestPrediction = new ImagePredictedLabelWithProbability { PredictedLabel = prediction.PredictedLabel, Probability = prediction.Score.Max(), PredictionExecutionTime = elapsedTime, ImageID = filename, VietnameseLabel = flowerService.FindVietnameseName(prediction.PredictedLabel) }; return(Ok(bestPrediction)); }
public static ProductItem CreateFrom(ImagePredictedLabelWithProbability scoring) { var tag = scoring.PredictedLabel; return(new ProductItem(tag, (decimal)scoring.Probability)); }
public async Task <IActionResult> ClassifyImage(IFormFile imageFile) { if (imageFile.Length == 0) { return(BadRequest()); } string imageFilePath = "", fileName = ""; try { //Save the temp image image into the temp-folder fileName = await _imageWriter.UploadImageAsync(imageFile, _imagesTmpFolder); imageFilePath = Path.Combine(_imagesTmpFolder, fileName); //Convert image stream to byte[] byte[] imageData = null; // //Image stream still not used in ML.NET 0.7 but only possible through a file // //MemoryStream image = new MemoryStream(); //await imageFile.CopyToAsync(image); //imageData = image.ToArray(); //if (!imageData.IsValidImage()) // return StatusCode(StatusCodes.Status415UnsupportedMediaType); ImagePredictedLabelWithProbability imageLabelPrediction = null; _logger.LogInformation($"Start processing image file { imageFilePath }"); //Measure execution time var watch = System.Diagnostics.Stopwatch.StartNew(); //Predict the image's label (The one with highest probability) imageLabelPrediction = _modelScorer.PredictLabelForImage(imageData, imageFilePath); //Stop measuring time watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; imageLabelPrediction.PredictionExecutionTime = elapsedMs; _logger.LogInformation($"Image processed in {elapsedMs} miliseconds"); //TODO: Commented as the file is still locked by TensorFlow or ML.NET? //_imageWriter.DeleteImageTempFile(imageFilePath); //return new ObjectResult(result); return(Ok(imageLabelPrediction)); } finally { try { if (imageFilePath != string.Empty) { _logger.LogInformation($"Deleting Image {imageFilePath}"); //TODO: Commented as the file is still locked by TensorFlow or ML.NET? //_imageWriter.DeleteImageTempFile(imageFilePath); } } catch (Exception) { _logger.LogInformation("Error deleting image: " + imageFilePath); } } }
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)); }