Ejemplo n.º 1
0
        private static void LoadAndPredict()
        {
            var mlContext = new MLContext();

            ITransformer trainedModel;

            using (var stream = File.OpenRead(ObjectDetectionModelFilePath))
            {
                trainedModel = mlContext.Model.Load(stream, out var modelInputSchema);
                Console.WriteLine("Load Model file success!");
            }
            var predictionEngine = mlContext.Model.CreatePredictionEngine <ImageNetData, ImageNetPrediction>(trainedModel);

            DirectoryInfo testdir = new DirectoryInfo(testimagesFolder);

            foreach (var jpgfile in testdir.GetFiles("*.jpg"))
            {
                ImageNetData image = new ImageNetData
                {
                    ImagePath = jpgfile.FullName
                };

                Console.WriteLine($".....PredictImage: {image.ImagePath} ....");

                var Predicted = predictionEngine.Predict(image);
                PredictImage(image.ImagePath, Predicted);

                Console.WriteLine("");
            }
        }
Ejemplo n.º 2
0
        protected IEnumerable <ImageNetData> PredictDataUsingModel(string testLocation, string imagesFolder, PredictionModel <ImageNetData, ImageNetPrediction> model)
        {
            ConsoleWriteHeader("Classificate images");
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Training file: {testLocation}");
            Console.WriteLine(" ");

            model.TryGetScoreLabelNames(out string[] labels);
            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder).ToList();

            // add an extra image to "really" test the model
            testData = testData.Concat(new[] { new ImageNetData()
                                               {
                                                   ImagePath = Path.Combine(imagesFolder, "teddy5.jpg")
                                               } }).ToList();

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample).PredictedLabels;
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                };
                (imageData.Label, imageData.Probability) = GetLabel(labels, probs);
                imageData.ConsoleWriteLine();
                yield return(imageData);
            }
        }
Ejemplo n.º 3
0
        public IDictionary <string, IList <YoloBoundingBox> > DetectObjectsFromImages(List <string> imageFilePaths,
                                                                                      string uploadFolderPath, string outputFolderPath)
        {
            MLContext mlContext = new MLContext();
            IDictionary <string, IList <YoloBoundingBox> > detectedObjectsDict = new Dictionary <string, IList <YoloBoundingBox> >();

            try
            {
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFiles(imageFilePaths);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);
                var       modelScorer             = new OnnxModelScorer(this.modelFilePath, mlContext);

                YoloOutputParser      parser        = new YoloOutputParser();
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView); // Use model to score data
                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .7F));

                for (var i = 0; i < images.Count(); i++)
                {
                    string imageFileName = images.ElementAt(i).Label;
                    string imageFilePath = images.ElementAt(i).ImagePath;
                    IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);
                    detectedObjectsDict.Add(imageFilePath, detectedObjects);
                    DrawBoundingBox(imageFilePath, outputFolderPath, imageFileName, detectedObjects);
                }

                return(detectedObjectsDict);
            }
            catch
            {
                throw;
            }
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            var    assetsRelativePath = @"assets";
            string assetsPath         = GetAbsolutePath(assetsRelativePath);
            var    tagsTsv            = Path.Combine(assetsPath, "images", "tags.tsv");
            var    modelFilePath      = Path.Combine(assetsPath, "resnet152v2", "resnet152v2.onnx");
            var    labelsTxt          = Path.Combine(assetsPath, "resnet152v2", "synset_text.txt");

            var imagesFolder = Path.Combine(assetsPath, "images");
            var outputFolder = Path.Combine(assetsPath, "images", "output");

            // Initialize MLContext
            MLContext mlContext = new MLContext();

            try
            {
                // Load Data
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

                // Create instance of model scorer
                var modelScorer = new OnnxModelScorer(tagsTsv, imagesFolder, modelFilePath, labelsTxt);

                // Use model to score data
                modelScorer.Score();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("========= End of Process..Hit any Key ========");
            Console.ReadLine();
        }
Ejemplo n.º 5
0
        protected string PredictDataUsingModel(string testLocation,
                                               string imagesFolder,
                                               string labelsLocation,
                                               PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Training file: {testLocation}");
            Console.WriteLine($"Labels file: {labelsLocation}");

            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);

            List <ImageNetDataProbability> result = new List <ImageNetDataProbability>();

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample);
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                    Label     = sample.Label
                };
                (imageData.PredictedLabel, imageData.Probability) = ModelHelpers.GetBestLabel(labels, probs.PredictedLabels);
                result.Add(imageData);
            }
            return(result.OrderByDescending(x => x.Probability).FirstOrDefault().PredictedLabel);
        }
Ejemplo n.º 6
0
        public static List <(string ImageFilename, List <(string Label, float Score)> TagList)> GetObjectList(string imagesFolder, string modelFilePath)
        {
            List <(string ImageFilename, List <(string Label, float Score)> TagList)> result = new List <(string ImageFilename, List <(string Label, float Score)> TagList)>();

            MLContext mlContext = new MLContext();

            IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
            IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

            var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);

            // Use model to score data
            IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

            YoloOutputParser parser = new YoloOutputParser();

            var boundingBoxes =
                probabilities
                .Select(probability => parser.ParseOutputs(probability))
                .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

            for (var i = 0; i < images.Count(); i++)
            {
                string imageFileName = images.ElementAt(i).Label;
                IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);

                result.Add((imageFileName, ExtractDetectedObjects(detectedObjects)));
            }

            return(result);
        }
Ejemplo n.º 7
0
        public void ClassifyImages()
        {
            ConsoleWriteHeader("Loading model");
            Console.WriteLine($"Model loaded: {modelLocation}");

            // Load the model
            ITransformer loadedModel;

            using (var f = new FileStream(modelLocation, FileMode.Open))
                loadedModel = mlContext.Model.Load(f);

            // Make prediction function (input = ImageNetData, output = ImageNetPrediction)
            var predictor = loadedModel.CreatePredictionEngine <ImageNetData, ImageNetPrediction>(mlContext);
            // Read csv file into List<ImageNetData>
            var testData = ImageNetData.ReadFromCsv(dataLocation, imagesFolder).ToList();

            ConsoleWriteHeader("Making classifications");
            // There is a bug (https://github.com/dotnet/machinelearning/issues/1138),
            // that always buffers the response from the predictor
            // so we have to make a copy-by-value op everytime we get a response
            // from the predictor
            testData
            .Select(td => new { td, pred = predictor.Predict(td) })
            .Select(pr => (pr.td.ImagePath, pr.pred.PredictedLabelValue, pr.pred.Score))
            .ToList()
            .ForEach(pr => ConsoleWriteImagePrediction(pr.ImagePath, pr.PredictedLabelValue, pr.Score.Max()));
        }
Ejemplo n.º 8
0
        protected IEnumerable <ImageNetData> PredictDataUsingModel(string testLocation,
                                                                   string imagesFolder,
                                                                   string labelsLocation,
                                                                   PredictionEngine <ImageNetData, 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);

            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample).PredictedLabels;
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                    Label     = sample.Label
                };
                (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);
                imageData.ConsoleWrite();
                yield return(imageData);
            }
        }
Ejemplo n.º 9
0
        public ImageNetData Score(ImageNetData data)
        {
            var model = LoadModel(dataLocation, imagesFolder, modelLocation);

            var prediction = PredictDataUsingModel(data, labelsLocation, model);

            return(prediction);
        }
Ejemplo n.º 10
0
        public JsonResult CheckImage()
        {
            string isPersonOnImage = "no";

            var file          = Request.Files[0];
            var currentPath   = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
            var modelFilePath = currentPath + @"\Content\Model\TinyYolo2_model.onnx";
            //var modelFilePath = @"C:\Users\Ragnus\Desktop\120819\praca\Thesis\Thesis\Content\Model\TinyYolo2_model.onnx";

            var sourceImage = Image.FromStream(file.InputStream);
            var fileExt     = Path.GetExtension(file.FileName);
            var filename    = Guid.NewGuid() + fileExt; // Unikalny identyfikator + rozszerzenie

            var outputFolder = currentPath + @"\Content\ImageToReturn\";

            //var outputFolder = @"C:\Users\Ragnus\Desktop\120819\praca\Thesis\Thesis\Content\ImageToReturn\";
            var path = Path.Combine(Server.MapPath(AppConfig.ImagesUserFolder), filename);

            sourceImage.Save(path);

            MLContext mlContext = new MLContext();
            IEnumerable <ImageNetData> images   = ImageNetData.ReadFrom1File(path);
            IDataView             imageDataView = mlContext.Data.LoadFromEnumerable(images);
            var                   modelScorer   = new OnnxModelScorer(path, modelFilePath, mlContext);
            IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);
            YoloOutputParser      parser        = new YoloOutputParser();

            var boundingBoxes =
                probabilities
                .Select(probability => parser.ParseOutputs(probability))
                .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

            // Draw bounding boxes for detected objects in each of the images
            for (var i = 0; i < images.Count(); i++)
            {
                string imageFileName = images.ElementAt(i).Label;
                IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);

                if (detectedObjects.Count > 0 && detectedObjects[0].Label == "person")
                {
                    isPersonOnImage = "yes";
                }
                DrawBoundingBox(path, outputFolder, imageFileName, detectedObjects);

                LogDetectedObjects(imageFileName, detectedObjects);
            }

            var img = Image.FromFile(outputFolder + filename);

            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            System.IO.File.Delete(path);

            img.Dispose();
            System.IO.File.Delete(outputFolder + filename);
            return(Json(new { isPerson = isPersonOnImage }
                        , JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 11
0
        public IEnumerable<float[]> Score(IEnumerable<byte[]> imageBytes)
        {

            IEnumerable<ImageNetData> images = ImageNetData.FromImages(imageBytes);
            var dataView = mlContext.Data.LoadFromEnumerable(images);
            var result = PredictDataUsingModel(dataView);
            ImageNetData.EraseFiles(images); // TODO: do this asynchronously
            return result;
        }
        protected IEnumerable <ImageNetPrediction> GetPredictions(string testLocation, string imagesFolder, PredictionModel <ImageNetData, ImageNetPrediction> model)
        {
            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);

            foreach (var sample in testData)
            {
                yield return(model.Predict(sample));
            }
        }
        protected IEnumerable <ImageNetData> PredictDataUsingModel(string testLocation,
                                                                   string imagesFolder,
                                                                   string labelsLocation,
                                                                   PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            ConsoleWriteHeader("Classificate images");
            Console.WriteLine($"Images folder: {imagesFolder}");
            Console.WriteLine($"Training file: {testLocation}");
            Console.WriteLine($"Labels file: {labelsLocation}");

            double acc = 0;
            double tp  = 0;
            double fp  = 0;
            double fn  = 0;

            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);
            var count    = 0;

            Console.ForegroundColor = ConsoleColor.Red;

            foreach (var sample in testData)
            {
                var probs     = model.Predict(sample).PredictedLabels;
                var imageData = new ImageNetDataProbability()
                {
                    ImagePath = sample.ImagePath,
                    Label     = sample.Label
                };
                (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);

                //imageData.ConsoleWrite();

                acc += imageData.Label == imageData.PredictedLabel ? 1 : 0;
                tp  += imageData.PredictedLabel == imageData.Label && imageData.Label == labels[1] ? 1 : 0;
                fp  += imageData.Label == labels[0] && imageData.PredictedLabel == labels[1] ? 1 : 0;
                fn  += imageData.Label == labels[1] && imageData.PredictedLabel == labels[0] ? 1 : 0;

                count++;
                if (count % 100 == 0)
                {
                    Console.WriteLine($"{count} images have been processed");
                }

                yield return(imageData);
            }

            acc = (acc / testData.ToList().Count()) * 100;
            double prec = tp / (tp + fp);
            double rec  = tp / (tp + fn);
            double f1   = 2 * (prec * rec) / (prec + rec);


            Console.WriteLine($"Accuracy: {acc}%, precision: {prec}, recall: {rec}, f1 score: {f1}");
        }
Ejemplo n.º 14
0
        public void DetectObjectsUsingModel(string imagesFilePath)
        {
            var imageInputData = new ImageNetData {
                ImagePath = imagesFilePath
            };
            var probs = model.Predict(imageInputData).PredictedLabels;
            IList <YoloBoundingBox> boundingBoxes = _parser.ParseOutputs(probs);

            filteredBoxes = _parser.NonMaxSuppress(boundingBoxes, 5, .5F);
        }
Ejemplo n.º 15
0
        public string Detect()
        {
            List <DetectionResult> result = new List <DetectionResult>();
            // Initialize MLContext
            MLContext mlContext = new MLContext();

            try
            {
                // Load Data
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(_imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

                // Create instance of model scorer
                var modelScorer = new OnnxModelScorer(_imagesFolder, _modelFilePath, mlContext);

                // Use model to score data
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

                // Post-process model output
                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

                // Draw bounding boxes for detected objects in each of the images
                for (var i = 0; i < images.Count(); i++)
                {
                    string imageFileName = images.ElementAt(i).Label;
                    IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);

                    DrawBoundingBox(_imagesFolder, _outputFolder, imageFileName, detectedObjects);

                    DetectionResult detectionResult = new DetectionResult(imageFileName);

                    foreach (var detObject in detectedObjects)
                    {
                        detectionResult.DetectedObjects.Add(new DetectedObjects(detObject.Label, detObject.Confidence));
                    }

                    LogDetectedObjects(imageFileName, detectedObjects);
                    result.Add(detectionResult);
                }
                return(JsonConvert.SerializeObject(result));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(null);
            }
        }
        public List <CubicBoundingBox> DetectFromFiles(IEnumerable <string> imagePaths, string path)
        {
            IEnumerable <ImageNetData> images = ImageNetData.ReadFromFiles(imagePaths);
            IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

            var modelScorer   = new OnnxModelScorer(path, onnxPath, mlContext);
            var probabilities = modelScorer.Score(imageDataView);

            OutputParser outputParser = new OutputParser(probabilities, 13, 5);
            var          boxes        = outputParser.BoundingBoxes;

            return(boxes);
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var    assetsRelativePath = @"../../../assets";
            string assetsPath         = GetAbsolutePath(assetsRelativePath);
            var    modelFilePath      = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx");
            var    imagesFolder       = Path.Combine(assetsPath, "images");
            var    outputFolder       = Path.Combine(assetsPath, "images", "output");

            //Create the Machine Learning context
            MLContext mlContext = new MLContext();

            try
            {
                //Load the data into an IDataView
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

                //Score the model
                var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);

                // Use model to score data
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

                //Post-processing - understanding the results
                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

                for (var i = 0; i < images.Count(); i++)
                {
                    string imageFileName = images.ElementAt(i).Label;
                    IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);

                    DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects);

                    LogDetectedObjects(imageFileName, detectedObjects);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("========= End of Process..Hit any Key ========");
            Console.ReadLine();
        }
Ejemplo n.º 18
0
        public List <List <Predict> > Pipe(string imgPath, IWebHostEnvironment _env)
        {
            string assetsPath    = GetAbsolutePath("assets");
            var    modelFilePath = Path.Combine(assetsPath, "Model", "Model.onnx");
            var    absolutePath  = Path.Combine(_env.WebRootPath, "assets", imgPath);
            var    imagesFolder  = Path.Combine(assetsPath, "images");
            var    outputFolder  = Path.Combine(assetsPath, "images", "output");

            var predicted = new List <List <Predict> >();

            MLContext mlContext = new MLContext();

            try
            {
                ImageNetData[] img = new ImageNetData[] { new ImageNetData {
                                                              ImagePath = absolutePath, Label = imgPath
                                                          } };
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(img);

                var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);

                // Use model to score data
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

                for (var i = 0; i < images.Count(); i++)
                {
                    string imageFileName = images.ElementAt(i).Label;
                    IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);

                    //DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects);
                    predicted.Add(LogDetectedObjects(imageFileName, detectedObjects));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("========= End of Process..Hit any Key ========");
            //Console.ReadLine();

            return(predicted);
        }
        public List <string> GenerateAnalysis(string guidName)
        {
            List <string> stringsToReturn = new List <string>();

            var    assetsRelativePath = @"../../../../DemoMLNet.Task.ObjectDetection.Console/assets";
            string assetsPath         = ProgramService.GetAbsolutePath(assetsRelativePath);
            var    modelFilePath      = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx");
            var    imagesFolder       = Path.Combine(assetsPath, "images");
            var    outputFolder       = Path.Combine(assetsPath, "images", "output");

            MLContext mlContext = new MLContext();

            try
            {
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

                var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);

                // Use model to score data
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

                for (var i = 0; i < images.Count(); i++)
                {
                    if (images.ElementAt(i).Label == guidName + ".jpg")
                    {
                        string imageFileName = images.ElementAt(i).Label;
                        IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);

                        ProgramService.DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects);

                        stringsToReturn = ProgramService.LogDetectedObjects(imageFileName, detectedObjects);
                    }
                    //Console.WriteLine("========= End of Process..Hit any Key ========");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return(stringsToReturn);
        }
Ejemplo n.º 20
0
        protected ImageNetData PredictDataUsingModel(ImageNetData testData,
                                                     string labelsLocation,
                                                     PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var probs     = model.Predict(testData).PredictedLabels;
            var imageData = new ImageNetDataProbability()
            {
                ImagePath = testData.ImagePath,
                Label     = testData.Label
            };

            (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);
            return(imageData);
        }
Ejemplo n.º 21
0
        static void Main(string[] args)
        {
            try
            {
                var relativePath = @"../../../assets";
                var filehelper   = new FileHelper(relativePath);
                var images       = ImageNetData.ReadFromFile(filehelper.ImagesFolder);

                var mlContext = new MLContext();

                // Загруэаем данные
                var imageDataView = mlContext.Data.LoadFromEnumerable(images);

                // Создаем экземпляр OnnxModelScorer и используем его для оценки загруженных данных
                var modelScorer   = new OnnxModelScorer(filehelper.ImagesFolder, filehelper.ModelFilePath, mlContext);
                var probabilities = modelScorer.Score(imageDataView);

                // Создаем экземпляр YoloOutputParser и используем его для обработки выходных данных модели
                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes = probabilities.Select(probability => parser.ParseOutputs(probability))
                                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

                for (var i = 0; i < images.Count(); i++)
                {
                    string imageFileName   = images.ElementAt(i).Label;
                    var    detectedObjects = boundingBoxes.ElementAt(i);
                    var    imageWithLabels = ImageHelper.DrawBoundingBox(filehelper.ImagesFolder, filehelper.OutputFolder, imageFileName, detectedObjects);

                    if (!Directory.Exists(filehelper.OutputFolder))
                    {
                        Directory.CreateDirectory(filehelper.OutputFolder);
                    }

                    imageWithLabels.Save(Path.Combine(filehelper.OutputFolder, imageFileName));

                    LogDetectedObjects(imageFileName, detectedObjects);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.WriteLine("========= End of Process..Hit any Key ========");
            Console.ReadLine();
        }
Ejemplo n.º 22
0
        private List <ImageNetData> ImageDataset()
        {
            List <ImageNetData> list = new List <ImageNetData>();


            foreach (var filename in Directory.GetFiles(FolderLocation.Text).ToList <string>())
            {
                var filenameonly = System.IO.Path.GetFileName(filename);

                var data = new ImageNetData();
                data.Filename = filenameonly;

                list.Add(data);
            }

            return(list);
        }
Ejemplo n.º 23
0
        public byte[] Classify(string imagePath, string imageName)
        {
            var modelFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..", "assets", "Model", "TinyYolo2_model.onnx");

            MLContext mlContext = new MLContext();

            try
            {
                // Load Data
                ImageNetData data = new ImageNetData()
                {
                    ImagePath = Path.Combine(imagePath, imageName),
                    Label     = imageName
                };
                IEnumerable <ImageNetData> image = new List <ImageNetData>()
                {
                    data
                };
                IDataView imageDataView = mlContext.Data.LoadFromEnumerable(image);

                // Create instance of model scorer
                var modelScorer = new OnnxModelScorer(modelFilePath, mlContext);

                // Use model to score data
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

                // Post-process model output
                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)).First();

                // Draw bounding boxes for detected objects in each of the images
                IList <YoloBoundingBox> detectedObjects = boundingBoxes;

                return(DrawBoundingBox(imagePath, imageName, detectedObjects));
            }
            catch (Exception)
            {
                return(null); //TODO
            }
        }
        internal static void Start()
        {
            //var assetsRelativePath = @"../../../assets";
            //string assetsPath = GetAbsolutePath(assetsRelativePath);
            var modelFilePath = Path.Combine(projectDirectory, "Model", "TinyYolo2_model.onnx");
            var imagesFolder  = Path.Combine(projectDirectory, "images");
            var outputFolder  = Path.Combine(projectDirectory, "images", "output");

            MLContext mlContext = new MLContext();

            try
            {
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

                //Затем создайте экземпляр OnnxModelScorer и используйте его для оценки загруженных данных.
                var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);
                // Use model to score data
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

                /*
                 * Теперь пора выполнить шаг постобработки. Создайте экземпляр YoloOutputParser и используйте его для обработки выходных данных модели.
                 */
                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

                for (var i = 0; i < images.Count(); i++)
                {
                    string imageFileName = images.ElementAt(i).Label;
                    IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);
                    DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects);
                    LogDetectedObjects(imageFileName, detectedObjects);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 25
0
        public void Test1()
        {
            var onnxPath     = @"../../../../TinyYolo/Data/TinyYolo2_model.onnx";
            var testDataPath = @"../../../Data";

            var ml    = new MLContext();
            var train = new Train(ml);

            var yolo        = new TinyYoloModel(CommonHelper.GetAbsolutionPath(onnxPath, typeof(TinyYoloModel)));
            var transformer = train.LoadOnnx(yolo);

            var testImage = Path.Combine(testDataPath, "Image1.jpg" /** Image file here */);

            testImage = CommonHelper.GetAbsolutionPath(testImage, typeof(UnitTest1));

            var image         = Image.FromFile(testImage);
            var bitmap        = (Bitmap)image;
            var testImageData = new ImageNetData {
                Image = bitmap
            };

            var objectDetection = ml.Model.CreatePredictionEngine <ImageNetData, TinyYoloPrediction>(transformer);

            TinyYoloPrediction result = new TinyYoloPrediction();

            objectDetection.Predict(testImageData, ref result);

            OnnxOutputParser parser = new OnnxOutputParser(yolo);
            var boxes = parser.ParseOutputs(result.PredictedLabels);

            boxes = parser.FilterBoundingBoxes(boxes, 3, .5f);

            if (boxes.Any())
            {
                var saveLocation = CommonHelper.GetAbsolutionPath(testDataPath + "/result", typeof(UnitTest1));
                CommonHelper.DrawRect(testDataPath, saveLocation, "Image1.jpg", boxes);
            }
            else
            {
                Assert.False(true);
            }
        }
        protected IEnumerable <ImageNetDataProbability> PredictImageUsingModel(string testLocation,
                                                                               string imageFile,
                                                                               string labelsLocation,
                                                                               PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            var labels = ModelHelpers.ReadLabels(labelsLocation);

            var testData = new ImageNetData {
                ImagePath = imageFile
            };

            var probs     = model.Predict(testData).PredictedLabels;
            var imageData = new ImageNetDataProbability()
            {
                ImagePath = testData.ImagePath,
                Label     = testData.Label
            };

            (imageData.PredictedLabel, imageData.Probability) = ModelHelpers.GetBestLabel(labels, probs);
            yield return(imageData);
        }
Ejemplo n.º 27
0
        public static void Run(string[] args)
        {
            var    assetsRelativePath = @"../../../../Assets";
            string assetsPath         = GetAbsolutePath(assetsRelativePath);
            var    modelFilePath      = Path.Combine(assetsPath, "OnnxModel", "MultiObjectDetectionModel.onnx");
            var    imagesFolder       = Path.Combine(assetsPath, "images");
            var    outputFolder       = Path.Combine(assetsPath, "images", "output");

            MLContext mlContext = new MLContext();


            IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
            IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

            var modelScorer   = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);
            var probabilities = modelScorer.Score(imageDataView);

            OutputParser outputParser = new OutputParser(probabilities, 13, 5);
            var          boxes        = outputParser.BoundingBoxes;

            DrawBoundingBox(Path.Combine(imagesFolder, "000005.jpg"), null, "fuckoff.jpg", boxes);
        }
        public ImageNetDataProbability PredictSingleImageDataUsingModel(
            string imagePath,
            string labelsLocation,
            PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            Debug.WriteLine($"Images folder: {imagePath}");
            Debug.WriteLine($"Labels folder: {labelsLocation}");
            var labels   = ReadLabels(labelsLocation);
            var testData = new ImageNetData {
                ImagePath = imagePath
            };

            var probs     = model.Predict(testData).PredictedLabels;
            var imageData = new ImageNetDataProbability()
            {
                ImagePath = testData.ImagePath,
                Label     = string.Empty
            };

            (imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);
            return(imageData);
        }
Ejemplo n.º 29
0
        static void Main(string[] args)
        {
            var    assetsRelativePath = @"../../../assets";
            string assetsPath         = GetAbsolutePath(assetsRelativePath);
            var    modelFilePath      = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx");
            var    imagesFolder       = Path.Combine(assetsPath, "images");
            var    outputFolder       = Path.Combine(assetsPath, "images", "output");

            MLContext mlContext = new MLContext();

            try
            {
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);
                var       modelScorer             = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);


                YoloOutputParser      parser        = new YoloOutputParser();
                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);
                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));

                for (var i = 0; i < images.Count(); i++)
                {
                    string imageFileName = images.ElementAt(i).Label;
                    IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);
                    DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects);
                    LogDetectedObjects(imageFileName, detectedObjects);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("Does it work? ... Hit Any Key ======");
            Console.ReadLine();
        }
        /// <summary>
        /// WARN The images are copied to the debug folder at compile time so I put a fixed path
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string assetsPath    = "assets";//GetAbsolutePath(assetsRelativePath);
            var    modelFilePath = Path.Combine(assetsPath, "Model", "test.onnx");

            var imagesFolder = Path.Combine(assetsPath, "images");

            MLContext mlContext = new MLContext();

            try
            {
                // Load Data
                IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder);
                IDataView imageDataView           = mlContext.Data.LoadFromEnumerable(images);

                // Create instance of model scorer
                var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext);
                for (int i = 0; i < 10; i++)
                {
                    // Use model to score data
                    var probabilities = modelScorer.Score(imageDataView);
                    Console.WriteLine(Environment.NewLine);

                    foreach (var prob in probabilities)
                    {
                        Console.WriteLine(prob.ToString());
                    }

                    Console.WriteLine("Digit any key for exit");
                }

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }