#include <dirent.h> #include <string.h> #include <vector> #include <string> #include <iostream> #include <chrono> #include <dlib/opencv.h> #include <opencv2/highgui/highgui.hpp> #include <opencv2/calib3d/calib3d.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing/render_face_detections.h> #include <dlib/image_processing.h> using namespace std; //没有相机参数的话,可以使用宽度估计焦距,cx和cy分别为宽度和高度的一半 double K[9] = { 12.80000e+002, 0.0, 6.40000000e+002, 0.0, 12.800000e+002, 3.60000000e+002, 0.0, 0.0, 1.0 }; double D[5] = { 7.0834633684407095e-002, 6.9140193737175351e-002, 0.0, 0.0, -1.3073460323689292e+000 }; //读图片 vector<string> imageSequence; void LoadImages(const std::string& strPathToSequence, std::vector< std::string >& strImage) { std::string absolutePath = strPathToSequence; DIR* dir = opendir(absolutePath.c_str()); struct dirent* ptr; while ((ptr = readdir(dir)) != NULL) { if (strcmp(ptr->d_name,".") != 0 && strcmp(ptr->d_name,"..") != 0) { std::string subDirect = strPathToSequence; if(ptr->d_type == 8) { std::string loadPath = subDirect + "/"; loadPath += ptr->d_name; strImage.push_back(loadPath); } } } } //argv1是.dat文件所在的路径,可以在[http: //argv2是图片所在的文件目录 int main(int argc, char** argv) { dlib::frontal_face_detector detector = dlib::get_frontal_face_detector(); dlib::shape_predictor predictor; dlib::deserialize(argv[1]) >> predictor; cv::Mat cam_matrix = cv::Mat(3, 3, CV_64FC1, K); cv::Mat dist_coeffs = cv::Mat(5, 1, CV_64FC1, D); std::vector<cv::Point3d> object_pts; object_pts.push_back(cv::Point3d(6.825897, 6.760612, 4.402142)); object_pts.push_back(cv::Point3d(1.330353, 7.122144, 6.903745)); object_pts.push_back(cv::Point3d(-1.330353, 7.122144, 6.903745)); object_pts.push_back(cv::Point3d(-6.825897, 6.760612, 4.402142)); object_pts.push_back(cv::Point3d(5.311432, 5.485328, 3.987654)); object_pts.push_back(cv::Point3d(1.789930, 5.393625, 4.413414)); object_pts.push_back(cv::Point3d(-1.789930, 5.393625, 4.413414)); object_pts.push_back(cv::Point3d(-5.311432, 5.485328, 3.987654)); object_pts.push_back(cv::Point3d(2.005628, 1.409845, 6.165652)); object_pts.push_back(cv::Point3d(-2.005628, 1.409845, 6.165652)); object_pts.push_back(cv::Point3d(2.774015, -2.080775, 5.048531)); object_pts.push_back(cv::Point3d(-2.774015, -2.080775, 5.048531)); object_pts.push_back(cv::Point3d(0.000000, -3.116408, 6.097667)); object_pts.push_back(cv::Point3d(0.000000, -7.415691, 4.070434)); std::vector<cv::Point2d> image_pts; cv::Mat rotation_vec; cv::Mat rotation_mat; cv::Mat translation_vec; cv::Mat pose_mat = cv::Mat(3, 4, CV_64FC1); cv::Mat euler_angle = cv::Mat(3, 1, CV_64FC1); std::vector<cv::Point3d> reprojectsrc; reprojectsrc.push_back(cv::Point3d(10.0, 10.0, 10.0)); reprojectsrc.push_back(cv::Point3d(10.0, 10.0, -10.0)); reprojectsrc.push_back(cv::Point3d(10.0, -10.0, -10.0)); reprojectsrc.push_back(cv::Point3d(10.0, -10.0, 10.0)); reprojectsrc.push_back(cv::Point3d(-10.0, 10.0, 10.0)); reprojectsrc.push_back(cv::Point3d(-10.0, 10.0, -10.0)); reprojectsrc.push_back(cv::Point3d(-10.0, -10.0, -10.0)); reprojectsrc.push_back(cv::Point3d(-10.0, -10.0, 10.0)); std::vector<cv::Point2d> reprojectdst; reprojectdst.resize(8); cv::Mat out_intrinsics = cv::Mat(3, 3, CV_64FC1); cv::Mat out_rotation = cv::Mat(3, 3, CV_64FC1); cv::Mat out_translation = cv::Mat(3, 1, CV_64FC1); std::ostringstream outtext; LoadImages(argv[2],imageSequence); int order = 0; for(auto& file : imageSequence) { cv::Mat ori,temp; ori = cv::imread(file,0); ori(cv::Rect(640,0,640,720)).copyTo(temp); resize(temp, cv::Size(320,360)); if(temp.rows <= 0 || temp.cols <= 0) break; order += 1; const auto loadframeTimeend = std::chrono::high_resolution_clock::now(); //参见dlib图片格式说明,rgb图,bgr_pixel是其中一种么,灰度图使用unsigned char dlib::cv_image<unsigned char> cimg(temp); std::vector<dlib::rectangle> faces = detector(cimg); const auto detectTimeend = std::chrono::high_resolution_clock::now(); const auto detectTimeSec = (double)std::chrono::duration_cast<std::chrono::nanoseconds>(detectTimeend - loadframeTimeend).count()* 1e-9; cout<< "detect face time: " <<std::to_string(detectTimeSec) << " seconds."<<endl; if (faces.size() > 0) { dlib::full_object_detection shape = predictor(cimg, faces[0]); for (unsigned int i = 0; i < 68; ++i) { cv::circle(temp, cv::Point(shape.part(i).x(), shape.part(i).y()), 2, cv::Scalar(0, 0, 255), -1); } image_pts.push_back(cv::Point2d(shape.part(17).x(), shape.part(17).y())); image_pts.push_back(cv::Point2d(shape.part(21).x(), shape.part(21).y())); image_pts.push_back(cv::Point2d(shape.part(22).x(), shape.part(22).y())); image_pts.push_back(cv::Point2d(shape.part(26).x(), shape.part(26).y())); image_pts.push_back(cv::Point2d(shape.part(36).x(), shape.part(36).y())); image_pts.push_back(cv::Point2d(shape.part(39).x(), shape.part(39).y())); image_pts.push_back(cv::Point2d(shape.part(42).x(), shape.part(42).y())); image_pts.push_back(cv::Point2d(shape.part(45).x(), shape.part(45).y())); image_pts.push_back(cv::Point2d(shape.part(31).x(), shape.part(31).y())); image_pts.push_back(cv::Point2d(shape.part(35).x(), shape.part(35).y())); image_pts.push_back(cv::Point2d(shape.part(48).x(), shape.part(48).y())); image_pts.push_back(cv::Point2d(shape.part(54).x(), shape.part(54).y())); image_pts.push_back(cv::Point2d(shape.part(57).x(), shape.part(57).y())); image_pts.push_back(cv::Point2d(shape.part(8).x(), shape.part(8).y())); cv::solvePnP(object_pts, image_pts, cam_matrix, dist_coeffs, rotation_vec, translation_vec); cv::projectPoints(reprojectsrc, rotation_vec, translation_vec, cam_matrix, dist_coeffs, reprojectdst); cv::line(temp, reprojectdst[0], reprojectdst[1], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[1], reprojectdst[2], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[2], reprojectdst[3], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[3], reprojectdst[0], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[4], reprojectdst[5], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[5], reprojectdst[6], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[6], reprojectdst[7], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[7], reprojectdst[4], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[0], reprojectdst[4], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[1], reprojectdst[5], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[2], reprojectdst[6], cv::Scalar(0, 0, 255)); cv::line(temp, reprojectdst[3], reprojectdst[7], cv::Scalar(0, 0,