コード例 #1
0
        static void PerformSubMenu_Prediction()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            ConsoleHelper.Print_CenteredTitle("VUI LÒNG CHỌN CHỨC NĂNG DƯỚI ĐÂY:", 100);
            Console.WriteLine("\n" + new string('=', 100));
            Console.WriteLine("1. Tự động dự đoán tất cả các bức hình có trong thư mục ");
            Console.WriteLine("2. Dự đoán một bức hình tùy ý");
            ConsoleHelper.Print_WarningText("Lưu ý: Chỉ nhận hình ảnh là tập tin *.jpg hoặc *.png");
            Console.WriteLine(new string('=', 100));
            Console.ResetColor();
            int    function = SelectMenu(2);
            string trainedModelPath;

            switch (function)
            {
            case 1:
                Console.Clear();
                Print_FilePathPrompt(out trainedModelPath, "Nhập đường dẫn đến mô hình đã được huấn luyện trước: ");
                Print_FolderPathPrompt(out string predictionFolderPath, "Nhập đường dẫn đến thư mục hình ảnh cần dự đoán: ");
                MLTraining mlTraining_1 = new MLTraining(trainedModelPath, null, null);
                mlTraining_1.TryMultiplePredictions(predictionFolderPath);
                break;

            case 2:
                Console.Clear();
                Print_FilePathPrompt(out trainedModelPath, "Nhập đường dẫn đến mô hình đã được huấn luyện trước: ");
                ConsumeModel.MLNetModelPath = trainedModelPath;
                while (true)
                {
                    if (!Print_FilePathPrompt(out string imagePath, "Nhập đường dẫn hình ảnh cần dự đoán, hoặc gõ 'exit' để THOÁT: "))
                    {
                        break;
                    }
                    ImageDataInMemory imageData        = new ImageDataInMemory(imagePath, Path.GetFileName(imagePath));
                    ImagePrediction   predictionResult = ConsumeModel.Predict(imageData);
                    Console.WriteLine($"Tên dự đoán: {predictionResult.PredictedLabel}");
                    Console.WriteLine($"Độ chính xác: {predictionResult.Score.Max()}");
                }
                break;

            default:
                break;
            }
            Console.WriteLine("Nhấn phím bất kỳ để quay trở lại...");
        }
コード例 #2
0
        /// <summary>
        /// Get the first image file and run prediction method
        /// </summary>
        public void TrySinglePrediction(string InputFolderPathForPrediction)
        {
            if (trainedModel == null)
            {
                if (File.Exists(OutputModelFilePath))
                {
                    LoadTrainedModel();
                }
                else
                {
                    throw new Exception("Please run the pipeline before predicting!");
                }
            }
            PredictionEngine <ImageDataInMemory, ImagePrediction> predictionEngine = mlContext.Model.CreatePredictionEngine <ImageDataInMemory, ImagePrediction>(trainedModel);
            IEnumerable <ImageDataInMemory> predictedImages = FileUtils.LoadImagesFromDirectoryInMemory(InputFolderPathForPrediction, false);
            ImageDataInMemory image      = predictedImages.First();
            ImagePrediction   prediction = predictionEngine.Predict(image);

            PrintImagePrediction(image.ImagePath, image.Label, prediction.PredictedLabel, prediction.Score.Max());
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        // For more info on consuming ML.NET models, visit https://aka.ms/mlnet-consume
        // Method for consuming model in your app
        public static ImagePrediction Predict(ImageDataInMemory input)
        {
            ImagePrediction result = PredictionEngine.Value.Predict(input);

            return(result);
        }