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

模型和测试文件:

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站视频链接

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

 

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

 

进阶推荐:

[opencv_C++] 入门强推!!!

 

posted @ 2022-03-21 09:02  咖喱gg  阅读(1092)  评论(0编辑  收藏  举报