Dlib是一个现代化的C ++工具箱,其中包含用于在C ++中创建复杂软件以解决实际问题的机器学习算法和工具。 它在工业界和学术界广泛使用,包括机器人技术,嵌入式设备,手机和大型高性能计算环境。 Dlib的开源许可证允许我们在任何应用程序中免费使用它。
dlib配置
- 从官网上下载dlib,其中要注意的是dlib19.4及以上版本需要vc14运行库(即visual studio 2015)及以上
- 利用cmake编译dlib中的dlib文件夹,编译成所需要的相应版本
- 点击生成的工程文件,设定好配置后(比如release & x64),利用vs开始编译
- 编译后产生dlib.lib库文件,在vs附加库目录中添加该路径,并在附加依赖项中,添加dlib.lib
- 在包含目录中,把整个解压后的包的路径添上即可
- 将vs修改为所编译的工作环境(release & x64),即可开始dlib之旅
实现人脸68点检测
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <dlib/opencv.h> #include <opencv2/opencv.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::Mat frame; frame = cv::imread("face.png"); //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. // Grab a frame cv::Mat temp; temp = frame.clone(); 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])); if (!shapes.empty()) { for (int i = 0; i < 68; i++) { circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1); putText(temp, to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 255)); // shapes[0].part(i).x();//68个 } } //Display it all on the screen imshow("Dlib特征点", temp); cv::waitKey(0); } 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; } } |
效果图