C# OpenCV EmguCv mobilenet_v3 对象检测

链接:https://pan.baidu.com/s/1I9N3lRe_t9C24IKJRcv89Q?pwd=1212

由于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;
                }
            }

        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lmr廖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值