ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# OpenCV EmguCv mobilenet_v3 对象检测

2022-03-21 15:34:57  阅读:359  来源: 互联网

标签:string mobilenet C# frame v3 bbox var new net


链接: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.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();
                for (int j = 0; j < classIds.Size; j++)
                {
                    var classId = classIds[j];
                    //var box = bbox[j];
                    Rectangle box = bbox[indices[i]];
                    CvInvoke.Rectangle(frame, box, new Emgu.CV.Structure.MCvScalar(255, 0, 255), 2);
                    CvInvoke.PutText(frame, classes[classIds[i] - 1].ToUpper(), new Point(box.X + 2, box.Y + 2),
                         Emgu.CV.CvEnum.FontFace.HersheyPlain, 0.7, new Emgu.CV.Structure.MCvScalar(200, 200, 200), 1);
                }
            }
            CvInvoke.Imshow("output", frame);
            int c = CvInvoke.WaitKey(5);
            if (c == 27)
            { // ESC退出
                break;
            }
        }
    }
}

 

 

标签:string,mobilenet,C#,frame,v3,bbox,var,new,net
来源: https://www.cnblogs.com/gxrsprite/p/16034893.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有