无法从 mmod 面部检测器获取 dlib 矩形坐标

Can't get dlib rectangle coords from mmod face detector

我正在使用 mmod_faces_detection 示例,我试图获取面部矩形坐标,但没有成功,这就是我正在做的事情:

std::vector<rectangle> getFacesVec(matrix<rgb_pixel> img)
{
    net_type net;
    deserialize("mmod_human_face_detector.dat") >> net; 

    std::vector<rectangle> r; 
    while(img.size() < 1800*1800)
        pyramid_up(img);

    auto dets = net(img);
    for (auto&& d : dets) {
        r.push_back(d.rect);
    }
    return r;
}

    ....
faces = getFacesVec(img);
for (auto f : faces) {
    cout << "Rect left: " << f.left() << endl;
    cout << "Rect right: " << f.right() << endl;
    cout << "Rect top: " << f.top() << endl;
    cout << "Rect bottom: " << f.bottom() << endl;
    cout << "Rect width: " << f.width() << endl;
    cout << "Rect height: " << f.height() << endl;

    cv::Rect roi(f.left(), f.top() , f.right(), f.bottom());

    cout << "Trying to print cropped face" << endl;
    cout << "X = " << roi.x << " Y = " << roi.y << " Width = " << roi.width << " Height = " << roi.height << endl;
    cv::Mat crop = m(roi);

    sprintf(s, "%d.jpg", i++);
    cv::imwrite(s, crop);
}

我正在获取图像范围之外的坐标 像这样: 垫子行数:432 垫子列数:768 左矩形:1068 右边:1914 矩形顶部:480 矩形底部:1325 矩形宽度:847 矩形高度:846

我做错了什么?

您pyramid_up放大了输入图像,因此您得到的所有坐标都被放大了。

您可能希望通过更改 getFacesVec 来查看原始版本

std::vector<rectangle> getFacesVec(matrix<rgb_pixel> img)
{
    net_type net;
    deserialize("mmod_human_face_detector.dat") >> net; 

    std::vector<rectangle> r; 
    //while(img.size() < 1800*1800)
    //    pyramid_up(img);

    auto dets = net(img);
    for (auto&& d : dets) {
        r.push_back(d.rect);
    }
    return r;
}