/OpenCvSharpDNN

Implementation of YoloV3 and Caffe in OpenCvSharp

Primary LanguageC#MIT LicenseMIT

OpenCvSharp DNN

Example of implementation of YoloV3 and Caffe in OpenCvSharp, in this example it is used pre-trained models to detect persons, faces and a estimation of the probability gender of the faces detected

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 link:

Google Drive: https://drive.google.com/drive/folders/1oj9p04mPjbbCbq1qSK8ChMjOhMLMpk42

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

License

Licensed under the MIT License.