GithubHelp home page GithubHelp logo

Comments (1)

terasadi avatar terasadi commented on September 13, 2024

On this sample Yolov7 model is loaded only one time and you keep using the same loaded model instance to handle with your code/inferences.

`

public partial class Form1 : Form
{
    Yolov7? yolo = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (yolo == null)
        {
            // init Yolov7 with onnx (include nms results)file path
            yolo = new Yolov7("./assets/best.onnx", true);

            // setup labels of onnx model 
            //yolo.SetupYoloDefaultLabels();   // use custom trained model should use your labels like: yolo.SetupLabels(string[] labels)
            string[] labels = { "myCustomLabel" };
            yolo.SetupLabels(labels);
        }

        var dirPos = Directory.GetFiles(@"./Assets/val/images/pos");
        var dirNeg = Directory.GetFiles(@"./Assets/val/images/neg");

        var dtNow = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

        foreach (var item in dirPos)
        {
            if (Path.GetExtension(item).Equals(".jpg") ||
                Path.GetExtension(item).Equals(".png"))
            {
                using var image = Image.FromFile(item);
                var predictions = yolo.Predict(image);

                // draw box
                using var graphics = Graphics.FromImage(image);
                foreach (var prediction in predictions) // iterate predictions to draw results
                {
                    double score = Math.Round(prediction.Score, 2);
                    graphics.DrawRectangles(new Pen(prediction.Label.Color, 1), new[] { prediction.Rectangle });
                    var (x, y) = (prediction.Rectangle.X - 3, prediction.Rectangle.Y - 23);
                    graphics.DrawString($"{prediction.Label.Name} ({score})",
                                    new Font("Consolas", 16, GraphicsUnit.Pixel), new SolidBrush(prediction.Label.Color),
                                    new PointF(x, y));
                }

                var fileName = Path.GetFileName(item);
                var path = Path.GetDirectoryName(item);
                image.Save(Path.Combine(path, (fileName + "_" + dtNow + ".png")), ImageFormat.Png);
            }
        }
    }
}

`

from yolov7net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.