Skip to content

OpenCV Sharp implementation of DNN module for YoloV3 and Caffe

Notifications You must be signed in to change notification settings

mendiromania/OpenCVCSharpDNN

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenCvSharp DNN

Example of implementation of YoloV3 and Caffe in OpenCvSharp

Requirements

Usage

This is a implementation usage in YoloV3 and Caffe models

            //Directory contains the models and configuration files
            string dir = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "data");

            //Model of YoloV3
            string model = System.IO.Path.Combine(dir, "yolov3.weights");
            string cfg = System.IO.Path.Combine(dir, "yolov3.cfg");
            string labelsYolo = System.IO.Path.Combine(dir, "coco.names");


            //Model of face
            string modelFace = System.IO.Path.Combine(dir, "yolov3-wider_16000.weights");
            string cfgFace = System.IO.Path.Combine(dir, "yolov3-face.cfg");

            //Model of Gender classifaction
            string modelGenderCaffe = System.IO.Path.Combine(dir, "gender_net.caffemodel");
            string cfgGenderCaffe = System.IO.Path.Combine(dir, "deploy_gender.prototxt");

            //Image Path
            string testImage = System.IO.Path.Combine(dir, "friends.jpg");


            using (NetYoloV3 yoloV3 = new NetYoloV3())
            using (NetYoloV3 yoloV3Faces = new NetYoloV3())
            using (NetCaffeAgeGender caffeGender = new NetCaffeAgeGender())
            using (Bitmap bitmap = new Bitmap(testImage))
            using (Bitmap resultImage = new Bitmap(testImage))
            {

                //Initialize models
                yoloV3.Initialize(model, cfg, labelsYolo);
                yoloV3Faces.Initialize(modelFace, cfgFace, new string[] { "faces" });
                caffeGender.Initialize(modelGenderCaffe, cfgGenderCaffe, new string[] { "Male", "Female" });


                //Get result of YoloV3
                NetResult[] resultPersons = yoloV3.Detect(bitmap, labelsFilters: new string[] { "person" });


                //Get result of YoloV3 faces train
                NetResult[] resultFaces = yoloV3Faces.Detect(bitmap);

                using (Graphics canvas = Graphics.FromImage(resultImage))
                {
                    Font font = new Font(FontFamily.GenericSansSerif, 15);


                    foreach (NetResult item in resultFaces)
                    {
                        //Create a roi by each faces
                        using (Bitmap roi = (Bitmap)bitmap.Clone(item.Rectangle, bitmap.PixelFormat))
                        {
                            NetResult resultGender = caffeGender.Detect(roi).FirstOrDefault();

                            canvas.DrawString($"{resultGender.Label} {resultGender.Probability:0.0%}",
                                font,
                                new SolidBrush(Color.Green),
                                item.Rectangle.X - font.GetHeight(), item.Rectangle.Y - font.GetHeight());

                        }

                        canvas.DrawRectangle(new Pen(Color.Red, 2), item.Rectangle);
                    }

                    canvas.Save();
                }

                resultImage.Save(Path.Combine(dir, "result.jpg"));

            }

Pre-trained models

You can download the pre-trained models in these links:

|Pre-Trained Model|Link |--|--|- |YoloV3|https://pjreddie.com/darknet/yolo/ |YoloV3 Faces|http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/index.html |Caffe Age and Gender Classification|https://talhassner.github.io/home/publication/2015_CVPR

Results

For the next image:

Input image

This is the result: Input image

And this is the diagnostics the time of execution: The model NetYoloV3 - Faces has taken 1609 milliseconds The model NetCaffeGender has taken 45 milliseconds

About

OpenCV Sharp implementation of DNN module for YoloV3 and Caffe

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%