/// <summary>
        ///     Gets bounding boxes/objects from bitmap
        /// </summary>
        /// <param name="imageInputData"></param>
        /// <returns></returns>
        private List <BoundingBox> GetObjectsFromModel(ImageInputData imageInputData)
        {
            var labels        = CustomVisionPredictionEngine?.Predict(imageInputData).PredictedLabels ?? TinyYoloPredictionEngine?.Predict(imageInputData).PredictedLabels;
            var boundingBoxes = OutputParser.ParseOutputs(labels);
            var filteredBoxes = OutputParser.FilterBoundingBoxes(boundingBoxes, 5, 0.5f);

            return(filteredBoxes);
        }
Example #2
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();
        }
    public IEnumerator Detect(Color32[] picture, System.Action <IList <BoundingBox> > callback)
    {
        // Anonymous block thar uses a tenspr param, this tensor isfrom the TransformInput method
        using (var tensor = TransformInput(picture, ImageNetSettings.imageWidth, ImageNetSettings.imageHeight)) {
            var inputs = new Dictionary <string, Tensor> ();
            inputs.Add(ModelSettings.ModelInput, tensor);
            yield return(StartCoroutine(worker.StartManualSchedule(inputs)));

            var output  = worker.PeekOutput(ModelSettings.ModelOutput);
            var results = outputParser.ParseOutputs(output, MINIMUM_CONFIDENCE);
            var boxes   = outputParser.FilterBoundingBoxes(results, 5, MINIMUM_CONFIDENCE);

            callback(boxes);
        }
    }