public async Task <IActionResult> IdentifyObjects(IFormFile imageFile)
        {
            if (imageFile.Length == 0)
            {
                return(BadRequest());
            }

            string imageFilePath = "", fileName = "";

            try
            {
                //Save the temp image into the temp-folder
                fileName = await _imageWriter.UploadImageAsync(imageFile, _imagesTmpFolder);

                imageFilePath = Path.Combine(_imagesTmpFolder, fileName);

                //Detect the objects in the image
                var result = DetectAndPaintImage(imageFilePath);
                return(Ok(result));
            }
            catch (Exception e)
            {
                _logger.LogInformation("Error is: " + e.Message);
                return(BadRequest());
            }
        }
Beispiel #2
0
        public async Task <IActionResult> IdentifyObjects(IFormFile imageFile)
        {
            if (imageFile.Length == 0)
            {
                return(BadRequest());
            }

            string imageFilePath = "", fileName = "";

            try
            {
                //Save the temp image into the temp-folder
                fileName = await _imageWriter.UploadImageAsync(imageFile, _imagesTmpFolder);

                imageFilePath = Path.Combine(_imagesTmpFolder, fileName);

                _logger.LogInformation($"Start processing image file { imageFilePath }");

                //Measure execution time
                var watch = System.Diagnostics.Stopwatch.StartNew();

                //Predict the objects in the image
                var objectsNames = _modelScorer.DetectObjectsUsingModel(imageFilePath);
                _modelScorer.PaintImages(imageFilePath);

                //Stop measuring time
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;

                _logger.LogInformation($"Image processed in {elapsedMs} miliseconds");

                return(Ok(objectsNames));
            }
            catch (Exception e)
            {
                _logger.LogInformation("Error is: " + e.Message);
                return(BadRequest());
            }
        }
        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);
                }
            }
        }