转载请注明出处:http://blog.csdn.net/ouyangying123/article/details/70850533
本文章主要进行dlib中....\dlib-18.18\examples下的三个例子的实现。
本小白用的是:VS2013 + dlib18.18 + opencv2.4.11
1.opencv用于读取数据源(待测图片、视频、摄像机等)。
Opencv配置
VC++目录->包含目录:...\opencv\build\include
VC++目录->库目录:...\opencv\build\x86\vc12\lib (vs2013 对应vc12 x86对应win32系统)
链接器->输入->附加依赖项(release模式):
opencv_calib3d2411.libopencv_contrib2411.lib
opencv_core2411.lib
opencv_features2d2411.lib
opencv_flann2411.lib
opencv_gpu2411.lib
opencv_highgui2411.lib
opencv_imgproc2411.lib
opencv_legacy2411.lib
opencv_ml2411.lib
opencv_nonfree2411.lib
opencv_objdetect2411.lib
opencv_ocl2411.lib
opencv_photo2411.lib
opencv_stitching2411.lib
opencv_superres2411.lib
opencv_ts2411.lib
opencv_video2411.lib
opencv_videostab2411.lib
三个例子:
face_detection_ex :图像人脸检测
copy文件....\dlib-18.18\examples\face_detection_ex .cpp
上代码:
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>
using namespace dlib;
using namespace std;
int main(int argc, char** argv)
{
try
{
if (argc == 1)
{
cout << "Give some image files as arguments to this program." << endl;
return 0;
}
frontal_face_detector detector = get_frontal_face_detector();
image_window win;
// Loop over all the images provided on the command line.
for (int i = 1; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
array2d<unsigned char> img;
load_image(img, argv[i]);
pyramid_up(img);
// Now tell the face detector to give us a list of bounding boxes
// around all the faces it can find in the image.
std::vector<rectangle> dets = detector(img);
cout << "Number of faces detected: " << dets.size() << endl;
// Now we show the image on the screen and the face detections as
// red overlay boxes.
win.clear_overlay();
win.set_image(img);
win.add_overlay(dets, rgb_pixel(255,0,0));
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
system("pause");
}
右键生成后,把faces文件夹(...\dlib-18.18\examples\faces)复制到该项目的Release文件夹下,使用命令行进入该项目的Release目录下,运行该命令:
上结果:
face_landmark_detection_ex :图像人脸特征点提取
copy文件....\dlib-18.18\examples\face_landmark_detection_ex.cpp
#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 <dlib/image_io.h>
#include <dlib/opencv.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace dlib;
using namespace std;
int main(int argc, char** argv)
{
try
{
// This example takes in a shape model file and then a list of images to
// process. We will take these filenames in as command line arguments.
// Dlib comes with example images in the examples/faces folder so give
// those as arguments to this program.
if (argc == 1)
{
cout << "Call this program like this:" << endl;
cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
return 0;
}
std::cout <<"argc:"<< argc << std::endl;
// We need a face detector. We will use this to get bounding boxes for
// each face in an image.
frontal_face_detector detector = get_frontal_face_detector();
// And we also need a shape_predictor. This is the tool that will predict face
// landmark positions given an image and face bounding box. Here we are just
// loading the model from the shape_predictor_68_face_landmarks.dat file you gave
// as a command line argument.
shape_predictor sp;
std::cout << "argv[1]:"<< argv[1] << std::endl;
deserialize(argv[1]) >> sp;
image_window win;// win_faces;
// Loop over all the images provided on the command line.
for (int i = 2; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
array2d<rgb_pixel> img;
load_image(img, argv[i]);
// Make the image larger so we can detect small faces.
pyramid_up(img);
// 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);
cv::Mat temp = dlib::toMat(img);
for (int k = 0; k < 68; ++k){
circle(temp, cvPoint(shapes[j].part(k).x(), shapes[j].part(k).y()), 3, cv::Scalar(0, 0, 255), -1);
}
}
// Now let's view our face poses on the screen.
win.clear_overlay();
win.set_image(img);
//win.add_overlay(render_face_detections(shapes));
We can also extract copies of each face that are cropped, rotated upright,
and scaled to a standard size as shown here:
//dlib::array<array2d<rgb_pixel> > face_chips;
//extract_image_chips(img, get_face_chip_details(shapes), face_chips);
//win_faces.set_image(tile_images(face_chips));
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
system("pause");
}
把shape_predictor_68_face_landmarks.dat文件 和 faces文件夹(...\dlib-18.18\examples\faces)复制到该项目的Release文件夹下,使用命令行进入该项目的Release目录下,运行该命令:
face_landmark_detection_ex.exe shape_predictor_68_face_landmarks.dat faces/2008_001322.jpg
webcam_face_pose_ex: 摄像机人脸特征点提取
#define SKIP_FRAMES 2
#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;
#define RATIO 1
#define SKIP_FRAMES 2
int main()
{
try
{
cv::VideoCapture cap(0);
image_window win;
//cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
//cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
// 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;
int count = 0;
std::vector<rectangle> faces;
// Grab and process frames until the main window is closed by the user.
while (!win.is_closed())
{
// Grab a frame
cv::Mat img,img_small;
cap >> img;
cv::resize(img, img_small, cv::Size(), 1.0 / RATIO, 1.0 / RATIO);
cv_image<bgr_pixel> cimg(img);
cv_image<bgr_pixel> cimg_small(img_small);
// Detect faces
if (count++ % SKIP_FRAMES == 0){
faces = detector(cimg_small);
}
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i){
rectangle r(
(long)(faces[i].left() * RATIO),
(long)(faces[i].top() * RATIO),
(long)(faces[i].right() * RATIO),
(long)(faces[i].bottom() * RATIO)
);
shapes.push_back(pose_model(cimg, r));
for (int k = 0; k < 68; ++k){
circle(img, cvPoint(shapes[i].part(k).x(), shapes[i].part(k).y()), 3, cv::Scalar(0, 0, 255), -1);
}
}
std::cout << "count:" << count << std::endl;
// 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;
}
system("pause");
}