DLIB将自训练的检测器应用于视频(MMOD_DNN)

Dlib apply self trained detector to video (mmod_dnn)

本文关键字:视频 MMOD DNN 应用于 检测器 DLIB      更新时间:2023-10-16

im试图在视频中使用DLIB中的MMOD_DNN模型在视频中跟踪一个自训练的猫检测器。我已经使用dnn_mmod_train_find_cars_example训练了自己的网络。它可以使用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<std::vector<dlib::mmod_rect,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>' to 'std::vector<dlib::rectangle,std::allocator<_Ty>>'

我似乎有错误的向量类型,即使WebCam_face_pose示例使用此矢量,除了我不使用示例中的face模型。我还尝试了示例中的std::vector<full_object_detection>,但是我认为我不明白如何正确应用自己的网络。有建议吗?事先感谢您的任何帮助。

如果您仍然遇到麻烦,我一直在处理类似的问题并找到了一些解决方案。

这里的主要问题是对于MMOD_DNN型号,net(cimg)不会返回矩形的向量。它返回类似的内容:vector<mmod_rect>vector<vector<mmod_rect>>,具体取决于您给它单张图像还是多个图像。您链接的面部检测示例代码使用auto dets = net(img)。然后,您必须将它们转换为DLIB矩形才能尝试解决姿势。

您可能遇到的其他问题是MMOD_DNN网络采用matrix<rgb_pixel>vector<matrix<rgb_pixel>>类型,因此您必须转换为这些类型。Dlib有一些功能可以帮助那里:

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