• C# OpenCV EmguCv mobilenet_v3 对象检测


    链接:

    由于OpenCVSharp缺少相关api,这次使用EmguCv,这个demo网上都是python版本的,抄写为C#

    使用ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt和frozen_inference_graph.pb模型

     

        public class EasyObjectDetection
        {
            public static void Run()
            {
                string modelPathBase = @"D:\SourceCode\Models\";
                string classesfile = modelPathBase + "coco.names";
                string configPath = modelPathBase + "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt";
                string weightsPath = modelPathBase + "frozen_inference_graph.pb";
    
    
                var thres = 0.5f;
                var cap = new VideoCapture(modelPathBase + "Road_traffic_video2.mp4");
                string[] classes = File.ReadAllLines(classesfile);
    
                var net = new Emgu.CV.Dnn.DetectionModel(weightsPath, configPath);
                //net.SetPreferableBackend(Backend.Cuda);
                //net.SetPreferableTarget(Target.Cuda);
                net.SetInputSize(new Size(320, 320));
                net.SetInputScale(1.0 / 127.5);
                net.SetInputMean(new Emgu.CV.Structure.MCvScalar(127.5, 127.5, 127.5));
                net.SetInputSwapRB(true);
    
                Mat frame = new Mat();
                while (true)
                {
                    var isSuccess = cap.Read(frame);
                    var classIds = new VectorOfInt();
                    var confs = new VectorOfFloat();
                    var bbox = new VectorOfRect();
                    var indices = new VectorOfInt();
    
                    net.Detect(frame, classIds, confs, bbox, confThreshold: 0.5f);
                    DnnInvoke.NMSBoxes(bbox, confs, thres, 0.2f, indices);
    
                    for (int i = 0; i < indices.Size; i++)
                    {
                        var confidence = confs[i].ToString();
                        var classId = classIds[i] - 1;
                        string classname = classes[classId];
                        Rectangle box = bbox[indices[i]];
                        CvInvoke.Rectangle(frame, box, new Emgu.CV.Structure.MCvScalar(255, 0, 255), 2);
                        CvInvoke.PutText(frame, classes[classId].ToUpper(), new Point(box.X + 2, box.Y + 2),
                             Emgu.CV.CvEnum.FontFace.HersheyPlain, 1, new Emgu.CV.Structure.MCvScalar(0, 0, 0), 1);
                    }
                    CvInvoke.Imshow("output", frame);
                    int c = CvInvoke.WaitKey(5);
                    if (c == 27)
                    { // ESC退出
                        break;
                    }
                }
    
            }
    
        }

     

  • 相关阅读:
    日志组件一:Log4j
    HTTPS加密那点事--轻松秒懂HTTPS非对称加密
    图解Git
    Python 迭代器 & __iter__方法
    Fiddler 抓包工具总结
    Python使用struct处理二进制(pack和unpack用法)
    Python binascii
    常见证书格式及相互转换
    MyBatis Generator 详解
    MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
  • 原文地址:https://www.cnblogs.com/gxrsprite/p/16034893.html
Copyright © 2020-2023  润新知