dnn.ssd为什么结果如此晚

时间:2018-01-08 12:37:28

标签: c# opencv image-processing tensorflow emgucv

您好我正在尝试在EmguCV中使用dnn.ssd。我还使用 VGG_VOC0712_SSD_300x300_iter_120000.caffemodel 进行了一些分类,但是当我尝试单张图片时,每张图片需要大约5或6秒钟。我看到了SSD的实时分类,它怎么可能。谢谢大家

硬件:

CPU = 英特尔酷睿i5 5200u 2.7 ghz

内存 = 12GB

我的代码:

public void getSSDfromPhoto(string adress, Emgu.CV.UI.ImageBox imbox, out List<string> output, out List<string[]> output_coordinates)
    {
        output = new List<string>();
        output_coordinates = new List<string[]>();

        //Image<Bgr, Byte> imageCV = new Image<Bgr, byte>(resim);

        Random rnd = new Random();
        for (int i = 0; i < 21; i++)
        {
            Colors[i] = new Rgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)).MCvScalar;
        }
        //get image and set model

        Mat img = CvInvoke.Imread(adress);
       // Mat img = imageCV.Mat;
        var blob = DnnInvoke.BlobFromImage(img, 1, new Size(300, 300));

       var prototxt = "deploy3.prototxt";
       var model = "VGG_VOC0712_SSD_300x300_iter_120000.caffemodel";

       //var prototxt = "MobileNetSSD_deploy.prototxt";
      // var model = "MobileNetSSD_deploy.caffemodel";

        var net = new Net();
        var import = Importer.CreateCaffeImporter(prototxt, model);
        import.PopulateNet(net);
        net.SetInput(blob, "data");
        Stopwatch sw = new Stopwatch();
        sw.Start();
        var prob = net.Forward("detection_out");
        Console.WriteLine(prob);
        sw.Stop();
        Console.WriteLine($"Runtime:{sw.ElapsedMilliseconds} ms");

        byte[] data = new byte[5600];
        prob.CopyTo(data);

        //draw result
        for (int i = 0; i < prob.SizeOfDimemsion[2]; i++)
        {
            var d = BitConverter.ToSingle(data, i * 28 + 8);
            if (d > 0.4)
            {
                var idx = (int)BitConverter.ToSingle(data, i * 28 + 4);
                var w1 = (int)(BitConverter.ToSingle(data, i * 28 + 12) * img.Width);
                var h1 = (int)(BitConverter.ToSingle(data, i * 28 + 16) * img.Height);
                var w2 = (int)(BitConverter.ToSingle(data, i * 28 + 20) * img.Width);
                var h2 = (int)(BitConverter.ToSingle(data, i * 28 + 24) * img.Height);

                string[] output_last = new string[] { Labels[idx], w1.ToString(),w2.ToString(),(h1).ToString(),(h2).ToString()};

                output_coordinates.Add(output_last);

                var label = $"{Labels[idx]} {d * 100:0.00}%";
                Console.WriteLine(label);

                output.Add(Labels[idx]);

                CvInvoke.Rectangle(imbox.Image, new Rectangle(w1, h1, w2 - w1, h2 - h1), Colors[idx], 2);

                int baseline = 0;
                var textSize = CvInvoke.GetTextSize(label, FontFace.HersheyTriplex, 0.5, 1, ref baseline);
                var y = h1 - textSize.Height < 0 ? h1 + textSize.Height : h1;
                CvInvoke.Rectangle(imbox.Image, new Rectangle(w1, y - textSize.Height, textSize.Width, textSize.Height), Colors[idx], -1);
                CvInvoke.PutText(imbox.Image, label, new Point(w1, y), FontFace.HersheyTriplex, 0.5, new Bgr(0, 0, 0).MCvScalar);
            }
        }

0 个答案:

没有答案