protected void PredictDataUsingModel(string imagesFolder, PredictionEngine <ImageNetData, ImageNetPrediction> model) { Console.WriteLine($"Images location: {imagesFolder}"); Console.WriteLine(""); Console.WriteLine("=====Identify the objects in the images====="); Console.WriteLine(""); var testData = GetImagesData(imagesFolder); foreach (var sample in testData) { var probs = model.Predict(sample).PredictedLabels; _boundingBoxes = _parser.ParseOutputs(probs); var filteredBoxes = _parser.FilterBoundingBoxes(_boundingBoxes, 5, .5F); var outputDirectory = Path.Combine(Directory.GetParent(sample.ImagePath).FullName, "output"); var filename = new FileInfo(sample.ImagePath).Name; DrawBoundingBox(imagesFolder, outputDirectory, filename, filteredBoxes); 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(""); } }
public static void Main() { 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"); // 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 YoloWinMlParser parser = new YoloWinMlParser(); 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); LogDetectedObjects(imageFileName, detectedObjects); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("========= End of Process..Hit any Key ========"); Console.ReadLine(); }