【问题标题】:Dlib apply self trained detector to video (mmod_dnn)Dlib 将自训练检测器应用于视频 (mmod_dnn)
【发布时间】:2018-05-18 03:04:55
【问题描述】:

我正在尝试使用 Dlib 中的 mmod_dnn 模型在视频中跟踪自我训练的猫检测器。我已经使用 dnn_mmod_train_find_cars_example 形式的 blog post 训练了我自己的网络。使用dnn_mmod_face_detector 示例可以很好地检测图像中的猫,但我希望在视频中跟踪猫。我尝试使用webcam_face_pose 示例作为起点,但我似乎无法弄清楚如何应用我自己的网络而不是人脸检测器。任何帮助都非常受欢迎。

代码:

#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <iostream>
#include <dlib/dnn.h>
#include <dlib/data_io.h>
using namespace dlib;
using namespace std;

// network ----------------------
template <long num_filters, typename SUBNET> using con5d = con<num_filters, 
5, 5, 2, 2, SUBNET>;
template <long num_filters, typename SUBNET> using con5 = con<num_filters, 
5, 5, 1, 1, SUBNET>;
template <typename SUBNET> using downsampler = relu<bn_con<con5d<32, 
relu<bn_con<con5d<32, relu<bn_con<con5d<16, SUBNET>>>>>>>>>;
template <typename SUBNET> using rcon5 = relu<bn_con<con5<55, SUBNET>>>;
using net_type = loss_mmod<con<1, 9, 9, 1, 1, rcon5<rcon5<rcon5<downsampler<input_rgb_image_pyramid<pyramid_down<6>>>>>>>>;
----------------------------------

int main()
{
    try
    {
        cv::VideoCapture cap("cat.avi");
        if (!cap.isOpened())
        {
            cerr << "Unable to connect to camera" << endl;
            return 1;
        }

        image_window win;
        net_type net;
        deserialize("cat_detector.dat") >> net;

        // Grab and process frames until the main window is closed by the user.
        while(!win.is_closed())
        {
            // Grab a frame
            cv::Mat temp;
            if (!cap.read(temp))
            {
                break;
            }

            cv_image<bgr_pixel> cimg(temp);

            // This is where I get an error (error message below):
            std::vector<rectangle> dets = net(cimg);
                -------------------

            // Display it all on the screen
            win.clear_overlay();
            win.set_image(temp);
            win.add_overlay(dets, rgb_pixel(255, 0, 0));
        }
    }
    catch(serialization_error& e)
    {
        cout << endl << e.what() << endl;
    }
    catch(exception& e)
    {
        cout << e.what() << endl;
    }
}

错误:error C2440: 'initializing': cannot convert from 'std::vector&lt;std::vector&lt;dlib::mmod_rect,std::allocator&lt;_Ty&gt;&gt;,std::allocator&lt;std::vector&lt;_Ty,std::allocator&lt;_Ty&gt;&gt;&gt;&gt;' to 'std::vector&lt;dlib::rectangle,std::allocator&lt;_Ty&gt;&gt;'

我似乎有错误的向量类型,即使webcam_face_pose 示例使用了此向量,但我没有在示例中使用人脸模型。我也尝试过std::vector&lt;full_object_detection&gt;,如示例中所示,但我认为我不明白如何正确应用我自己的网络。有什么建议吗?提前感谢您的帮助。

【问题讨论】:

    标签: c++ opencv dlib


    【解决方案1】:

    如果您仍然遇到此问题,我一直在研究类似的问题并找到了一些解决方案。

    这里的主要问题是对于 mmod_dnn 模型,net(cimg) 不返回矩形向量。它返回类似这样的内容:vector&lt;mmod_rect&gt;vector&lt;vector&lt;mmod_rect&gt;&gt;,具体取决于您给它的是单个图像还是多个图像。您链接的face detector example code 使用auto dets = net(img)。然后,您必须将它们转换为 dlib 矩形以尝试计算姿势。

    您可能遇到的其他问题是 mmod_dnn 网络采用 matrix&lt;rgb_pixel&gt;vector&lt;matrix&lt;rgb_pixel&gt;&gt; 类型,因此您必须转换为这些类型。 Dlib 有一些功能可以帮助您:

    cv_image<bgr_pixel> cimg(temp);
    matrix<rgb_pixel> matrix;
    assign_image(matrix, cimg)
    

    【讨论】:

      猜你喜欢
      • 2017-02-01
      • 2016-12-21
      • 2016-11-15
      • 2017-08-23
      • 2016-07-28
      • 2019-07-19
      • 2019-06-06
      • 2015-06-06
      • 1970-01-01
      相关资源
      最近更新 更多