【opencvsharpDNN】OpenCvSharp中YoloV3和Caffe的实现示例

0c3ee8c24c01790bf2d79568a6e21024.png

14bbed26b1841695bd705c4eb7f0fe00.png

识别效果

演示视频

使用方法

这是 YoloV3 和 Caffe 模型中的实现用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCVCSharpDNN.Impl;
using OpenCVCSharpDNN;
using System.IO;
using System.Drawing;


namespace OpenCVCSharp.Example
{
    class Program
    {
        static void Main(string[] args)
        {
            //包含模型和配置文件的目录
            string dir = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "data");


            // 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");




            //人脸检测模型
            string modelFace = System.IO.Path.Combine(dir, "yolov3-wider_16000.weights");
            string cfgFace = System.IO.Path.Combine(dir, "yolov3-face.cfg");


            //性别分类模型
            string modelGenderCaffe = System.IO.Path.Combine(dir, "gender_net.caffemodel");
            string cfgGenderCaffe = System.IO.Path.Combine(dir, "deploy_gender.prototxt");


            //图片路径
            string testImage = System.IO.Path.Combine(dir, "1.bmp");//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))
            {


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




                //获取 YoloV3 的结果
                NetResult[] resultPersons = yoloV3.Detect(bitmap, labelsFilters: new string[] { "person" });//检测人




                //获取 YoloV3 人脸检测的结果
                NetResult[] resultFaces = yoloV3Faces.Detect(bitmap);//检测人脸


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


                    //推理性别,绘制标签及人脸边界框
                    foreach (NetResult item in resultFaces)
                    {
                        //为每张脸创建感兴趣区域
                        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"));//保存识别结果


            }
        }
    }
}

预训练模型

您可以在以下链接中下载预训练模型:

|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

参考:

https://github.com/julian9012/OpenCVCSharpDNN 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值