如何将dlib shape_pedictor与视频捕获一起编写

时间:2015-04-17 15:04:24

标签: c++ opencv dlib

我正在尝试将每个帧从相机写入视频。直到这里很好。但是,我希望我的视频在每一帧都包含shape_predictor,所以当它再现时它也会出现在图像上。到目前为止,我有这个...任何想法?谢谢

cap >> frame;
cv::VideoWriter oVideoWriter;
// . . .
cv_image<bgr_pixel> cimg(frame);               //Mat to something dlib can deal with
frontal_face_detector detector = get_frontal_face_detector();
std::vector<rectangle> faces = detector(cimg);
pose_model(cimg, faces[0]);
oVideoWriter.write(dlib::toMat(cimg));          //Turn it into an Opencv Mat

1 个答案:

答案 0 :(得分:3)

形状预测器不是面部检测器。您必须首先调用面部检测器,然后调整形状预测器。

请参阅此示例程序:http://dlib.net/face_landmark_detection_ex.cpp.html

您正确初始化了面部检测器。然后您必须初始化跟踪器。像这样:

shape_predictor sp;
deserialize("shape_predictor_68_face_landmarks.dat") >> sp;

可在此处找到该模型:http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2

剩下的方法,你可以按照我上面链接的示例程序。这是跟踪器运行的部分。您必须将检测器返回的输出(边界框)传递给跟踪器才能工作。下面的代码遍历检测器返回的所有框。

        // Now tell the face detector to give us a list of bounding boxes
        // around all the faces in the image.
        std::vector<rectangle> dets = detector(img);
        cout << "Number of faces detected: " << dets.size() << endl;

        // Now we will go ask the shape_predictor to tell us the pose of
        // each face we detected.
        std::vector<full_object_detection> shapes;
        for (unsigned long j = 0; j < dets.size(); ++j)
        {
            full_object_detection shape = sp(img, dets[j]);
            cout << "number of parts: "<< shape.num_parts() << endl;
            cout << "pixel position of first part:  " << shape.part(0) << endl;
            cout << "pixel position of second part: " << shape.part(1) << endl;
            // You get the idea, you can get all the face part locations if
            // you want them.  Here we just store them in shapes so we can
            // put them on the screen.
            shapes.push_back(shape);
        }