如何使用 "shape_predictor_68_face_landmarks.dat" 在使用 Dlib 的 opencv C++ 中进行地标提取
how to use "shape_predictor_68_face_landmarks.dat" in landmark extraction in opencv C++ using Dlib
我正在尝试 运行 以下代码并检测从网络摄像头拍摄的帧的面部特征。
#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>
using namespace dlib;
using namespace std;
int main()
{
try
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
while(!win.is_closed())
{
// Grab a frame
cv::Mat temp;
cap >> temp;
// Turn OpenCV's Mat into something dlib can deal with. Note that this just
// wraps the Mat object, it doesn't copy anything. So cimg is only valid as
// long as temp is valid. Also don't do anything to temp that would cause it
// to reallocate the memory which stores the image as that will make cimg
// contain dangling pointers. This basically means you shouldn't modify temp
// while using cimg.
cv_image<bgr_pixel> cimg(temp);
// Detect faces
std::vector<rectangle> faces = detector(cimg);
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
// Display it all on the screen
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
}
}
catch(serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch(exception& e)
{
cout << e.what() << endl;
}
}
但是当执行这个 .cpp 文件时,它会给出这样的控制台输出。
enter image description here
因为我已经下载了 shape_predictor_68_face_landmarks.dat。但我不知道在哪里添加这个 .dat 和 whish 目录。谁能告诉我如何使用这个 shape_predictor_68_face_landmarks.dat.
错误似乎来自:
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
上述问题的解决方法是提供完整的限定路径,如:
deserialize("/full/path/to/shape_predictor_68_face_landmarks.dat") >> pose_model;
这可能是因为 C++ 可执行文件可以 运行 来自某个构建位置,该文件可能不在本地范围内,因此无法找到它,最好的方法将使用完全限定路径。
我正在尝试 运行 以下代码并检测从网络摄像头拍摄的帧的面部特征。
#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>
using namespace dlib;
using namespace std;
int main()
{
try
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
while(!win.is_closed())
{
// Grab a frame
cv::Mat temp;
cap >> temp;
// Turn OpenCV's Mat into something dlib can deal with. Note that this just
// wraps the Mat object, it doesn't copy anything. So cimg is only valid as
// long as temp is valid. Also don't do anything to temp that would cause it
// to reallocate the memory which stores the image as that will make cimg
// contain dangling pointers. This basically means you shouldn't modify temp
// while using cimg.
cv_image<bgr_pixel> cimg(temp);
// Detect faces
std::vector<rectangle> faces = detector(cimg);
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
// Display it all on the screen
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
}
}
catch(serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch(exception& e)
{
cout << e.what() << endl;
}
}
但是当执行这个 .cpp 文件时,它会给出这样的控制台输出。
enter image description here
因为我已经下载了 shape_predictor_68_face_landmarks.dat。但我不知道在哪里添加这个 .dat 和 whish 目录。谁能告诉我如何使用这个 shape_predictor_68_face_landmarks.dat.
错误似乎来自:
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
上述问题的解决方法是提供完整的限定路径,如:
deserialize("/full/path/to/shape_predictor_68_face_landmarks.dat") >> pose_model;
这可能是因为 C++ 可执行文件可以 运行 来自某个构建位置,该文件可能不在本地范围内,因此无法找到它,最好的方法将使用完全限定路径。