protected void PredictDataUsingModel(string imagesLocation,
                                             string imagesFolder,
                                             PredictionEngine <ImageNetData, ImageNetPrediction> model)
        {
            Console.WriteLine($"Tags file location: {imagesLocation}");
            Console.WriteLine("");
            Console.WriteLine("=====Identify the objects in the images=====");
            Console.WriteLine("");

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

            foreach (var sample in testData)
            {
                var probs = model.Predict(sample).PredictedLabels;
                IList <YoloBoundingBox> boundingBoxes = _parser.ParseOutputs(probs);
                var filteredBoxes = _parser.NonMaxSuppress(boundingBoxes, 5, .5F);

                Console.WriteLine(".....The objects in the image {0} are detected as below....", sample.Label);
                foreach (var box in filteredBoxes)
                {
                    Console.WriteLine(box.Label + " and its Confidence score: " + box.Confidence);
                }
                Console.WriteLine("");
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            var mL = new MLContext();

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

                var modelScorer = new OnnxModelScorer(imagesFolder, modelPath, mL);

                IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView);

                YoloOutputParser parser = new YoloOutputParser();

                var boundingBoxes = probabilities
                                    .Select(x => parser.ParseOutputs(x))
                                    .Select(x => parser.FilterBoundingBoxes(x, 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());
            }
        }
示例#3
0
        public static void Main()
        {
            var assetsRelativePath = @"../../../assets";
            var assetsPath         = GetAbsolutePath(assetsRelativePath);
            var modelFilePath      = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx");
            //var modelFilePath = Path.Combine(assetsPath, "Model", "Yolov3.onnx");
            //var modelFilePath = Path.Combine(assetsPath, "Model", "model.onnx");
            var imagesFolder = Path.Combine(assetsPath, "people");
            var outputFolder = Path.Combine(imagesFolder, "output");

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

            try
            {
                // Load Data
                var images        = ImageNetData.ReadFromFile(imagesFolder);
                var imageDataView = mlContext.Data.LoadFromEnumerable(images);

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

                // Use model to score data
                var probabilities = modelScorer.Score(imageDataView);

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

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

                // Draw bounding boxes for detected objects in each of the images
                for (var i = 0; i < images.Count(); i++)
                {
                    var imageFileName   = images.ElementAt(i).Label;
                    var 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();
        }
示例#4
0
        private static IEnumerable <ImageNetData> GetImagesData(string folder)
        {
            List <ImageNetData> imagesList = new List <ImageNetData>();

            string[] filePaths = Directory.GetFiles(folder);
            foreach (var filePath in filePaths)
            {
                ImageNetData imagedata = new ImageNetData {
                    ImagePath = filePath, Label = Path.GetFileName(filePath)
                };
                imagesList.Add(imagedata);
            }
            return(imagesList);
        }
示例#5
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);

                // 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);

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

            Console.WriteLine("========= End of Process..Hit any Key ========");
            Console.ReadLine();
        }
示例#6
0
        public ITransformer LoadModel()
        {
            // Create IDataView from empty list to obtain input data schema
            ImageNetData[] inMemoryCollection = new ImageNetData[]
            {
                new ImageNetData
                {
                    InputImage = null,
                    Label      = ""
                }
            };
            var data = mlContext.Data.LoadFromEnumerable <ImageNetData>(inMemoryCollection);

            // Define scoring pipeline
            var pipeline = mlContext.Transforms.ResizeImages(outputColumnName: "image", imageWidth: ImageNetSettings.imageWidth, imageHeight: ImageNetSettings.imageHeight, inputColumnName: "InputImage")
                           .Append(mlContext.Transforms.ExtractPixels(outputColumnName: "image"))
                           .Append(mlContext.Transforms.ApplyOnnxModel(modelFile: modelLocation, outputColumnNames: new[] { TinyYoloModelSettings.ModelOutput }, inputColumnNames: new[] { TinyYoloModelSettings.ModelInput }));

            // Fit scoring pipeline
            var model = pipeline.Fit(data);

            return(model);
        }
示例#7
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            // Frame image buffer
            Mat image = new Mat();

            _isRunning       = true;
            btnStart.Enabled = false;

            var mlContext = new MLContext();

            // Create instance of model scorer
            var modelScorer = new OnnxModelScorer(modelFilePath, mlContext);
            // Load model only once
            var model = modelScorer.LoadModel();

            while (_isRunning)
            {
                _capture.Read(image); // read frame from webcam

                if (image.Empty())
                {
                    break;
                }

                // Store frame as in-memory source for ML.NET
                ImageNetData[] inMemoryCollection = new ImageNetData[]
                {
                    new ImageNetData
                    {
                        InputImage = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image),
                        Label      = "",
                    }
                };
                var imageDataView = mlContext.Data.LoadFromEnumerable <ImageNetData>(inMemoryCollection);

                // Make another copy of the frame. We will use it to draw bounding boxes on it
                _mySharpImage = (Image)OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);

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

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

                var boundingBoxes =
                    probabilities
                    .Select(probability => parser.ParseOutputs(probability))
                    .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F));
                // Since we only have a single frame, it is OK to have i = 0. Otherwise we would need
                // to iterate through images.
                var i = 0;
                IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i);
                DrawBoundingBox(ref _mySharpImage, detectedObjects);

                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image    = _mySharpImage;
                Cv2.WaitKey(1);

                _mySharpImage.Dispose();
                inMemoryCollection[0].InputImage.Dispose();
            }
            btnStart.Enabled = true;
        }