C#使用OpenCVSharp MobileNetSSD模型做物体识别

模型和测试文件:https://wwu.lanzoub.com/i1BJx01rr38h

c++的demo网上到处都有,抄一个C#的demo

因为没有视频,换了张图片把代码改成识别图片了,自己根据需要切换注释

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;


public class dnn_ssd_video_mobilenet
{
    const int width = 300;
    const int height = 300;
    const float meanVal = 127.5f;
    const float scaleFactor = 0.007843f;
    string[] classNames = new string[] { "background",
"aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor" };

    String modelFile = "G:/SourceCode/MobileNetSSD_deploy.caffemodel";
    String model_text_file = "G:/SourceCode/MobileNetSSD_deploy.prototxt";
    string srcPath = @"G:\images\";


    public void Run()
    {
        string imagefile = srcPath + "vehicle_test.jpg";

        //VideoCapture capture = new VideoCapture(0);
        //int w = capture.FrameWidth;
        //int h = capture.FrameHeight;

        //Cv2.NamedWindow("input", WindowFlags.AutoSize);

        // set up net
        Net net = Net.ReadNetFromCaffe(model_text_file, modelFile);
        Mat frame;
        //while (capture.Read(frame))
        frame = Cv2.ImRead(imagefile);
        {
            //Cv2.ImShow("input", frame);

            // 预测
            Mat inputblob = CvDnn.BlobFromImage(frame, scaleFactor, new Size(width, height), meanVal, false);

            net.SetInput(inputblob, "data");
            Mat detection = net.Forward("detection_out");


            //检测
            Mat detectionMat = new Mat(detection.Size(2), detection.Size(3), MatType.CV_32F, detection.Ptr(0));
            float confidence_threshold = 0.3f;


            for (int i = 0; i < detectionMat.Rows; i++)
            {
                float confidence = detectionMat.At<float>(i, 2);
                if (confidence > confidence_threshold)
                {
                    int objIndex = (int)(detectionMat.At<float>(i, 1));
                    float tl_x = detectionMat.At<float>(i, 3) * frame.Cols;
                    float tl_y = detectionMat.At<float>(i, 4) * frame.Rows;
                    float br_x = detectionMat.At<float>(i, 5) * frame.Cols;
                    float br_y = detectionMat.At<float>(i, 6) * frame.Rows;

                    Rect object_box = new Rect((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
                    Cv2.Rectangle(frame, object_box, new Scalar(0, 0, 255), 2, LineTypes.Link8, 0);
                    Cv2.PutText(frame, classNames[objIndex], new Point(tl_x, tl_y), HersheyFonts.HersheySimplex, 1.0, new Scalar(255, 0, 0), 2);
                }
            }
            Cv2.ImShow("ssd-video-demo", frame);
            //int c = Cv2.WaitKey(5);
            //if (c == 27)
            //{ // ESC退出
            //    break;
            //}
            Cv2.WaitKey(0);
            Cv2.DestroyAllWindows();
        }
    }
}

附学习路径:

这个就是一个c++描述的教程我用C#重写了demo方便学习,内附B站视频链接

https://wwa.lanzoub.com/i57Mf01lygzi

匹配视频:OpenCV4 C++ 快速入门视频30讲 - 系列合集_哔哩哔哩_bilibili

https://wwa.lanzoub.com/iDFsp01p3d8b

匹配视频: https://www.bilibili.com/video/BV1QW411F7e7?p=24

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lmr廖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值