public OnnxModelScorer(string imagesFolder, string modelLocation, MLContext mlContext, IYoloConfiguration cfg) { this.imagesFolder = imagesFolder; this.modelLocation = modelLocation; this.mlContext = mlContext; this.configuration = cfg; }
private static void DrawBoundingBox(string inputImageLocation, string outputImageLocation, string imageName, IList <YoloBoundingBox> filteredBoundingBoxes, IYoloConfiguration cfg) { Image image = Image.FromFile(Path.Combine(inputImageLocation, imageName)); var originalImageHeight = image.Height; var originalImageWidth = image.Width; foreach (var box in filteredBoundingBoxes) { var x = (uint)Math.Max(box.Dimensions.X, 0); var y = (uint)Math.Max(box.Dimensions.Y, 0); var width = box.Dimensions.Width; var height = box.Dimensions.Height; x = (uint)originalImageWidth * x / cfg.ImageWidth; y = (uint)originalImageHeight * y / cfg.ImageHeight; width = (uint)originalImageWidth * width / cfg.ImageWidth; height = (uint)originalImageHeight * height / cfg.ImageHeight; string text = $"{box.Label} ({(box.Confidence * 100).ToString("0")}%)"; using (Graphics thumbnailGraphic = Graphics.FromImage(image)) { thumbnailGraphic.CompositingQuality = CompositingQuality.HighQuality; thumbnailGraphic.SmoothingMode = SmoothingMode.HighQuality; thumbnailGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic; Font drawFont = new Font("Arial", 12, FontStyle.Bold); SizeF size = thumbnailGraphic.MeasureString(text, drawFont); SolidBrush fontBrush = new SolidBrush(Color.Black); Point atPoint = new Point((int)x, (int)y - (int)size.Height - 1); // Define BoundingBox options Pen pen = new Pen(box.BoxColor, 3.2f); SolidBrush colorBrush = new SolidBrush(box.BoxColor); thumbnailGraphic.FillRectangle(colorBrush, (int)x, (int)(y - size.Height - 1), (int)size.Width, (int)size.Height); thumbnailGraphic.DrawString(text, drawFont, fontBrush, atPoint); // Draw bounding box on image thumbnailGraphic.DrawRectangle(pen, x - width / 2, y - height / 2, width, height); if (!Directory.Exists(outputImageLocation)) { Directory.CreateDirectory(outputImageLocation); } image.Save(Path.Combine(outputImageLocation, imageName)); } } }
public ORTWrapper(string modelPath, DNNMode mode) { // Optional : Create session options and set the graph optimization level for the session SessionOptions options = new SessionOptions(); //options.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_EXTENDED; cfg = new Yolov3BaseConfig(); this.mode = mode; switch (mode) { case DNNMode.LT: case DNNMode.Frame: session1 = new InferenceSession(modelPath, SessionOptions.MakeSessionOptionWithCudaProvider(0)); break; case DNNMode.CC: session2 = new InferenceSession(modelPath, SessionOptions.MakeSessionOptionWithCudaProvider(0)); break; } }
public Yolov3OutputParser(IYoloConfiguration configuration, string selectedOutput) { this.configuration = configuration; classCount = (uint)configuration.Labels.Length; this.selectedOutput = (from n in configuration.Outputs where n.Name == selectedOutput select n).First(); }