Face-Alignment-using-Dlib-OpenCV icon indicating copy to clipboard operation
Face-Alignment-using-Dlib-OpenCV copied to clipboard

speed up the detection proceess

Open nguyenthaohut opened this issue 6 years ago • 0 comments

to speed up process , i've try to pyramid down the image to get smaller image, but after solving solvePnP, i got the wrong result

`cv::Mat MatPose2(cv::Mat image) { double dScale = 0.5; Mat gray, resized, detected_edges, Laugh_L, Laugh_R; Mat dst; // if there is a problem reading the image,it throws an exception. if (image.empty()){ addlog("Exception: Image empty"); return dst; } //resize the gray scale image for speeding the face detection. cv::resize(image, resized, Size(), dScale, dScale); //converts original image to gray scale and stores it in "gray". cvtColor(resized, gray, COLOR_BGR2GRAY);

//Histogram equalization is performed on the resized image to improve 
//the contrast of the image which can help in detection.
equalizeHist(gray, gray);
// Conver OpenCV image Dlib image i.e. cimg
dlib::cv_image<dlib::rgb_pixel> cimg(resized);

// Detect faces using DLib
std::vector<dlib::rectangle> face_DLib = m_detector(cimg);
if (face_DLib.empty()){
	cout << "No face is deteced by DLib" << endl;
	return dst;
}
//landmarks vector is declared to store the 68 landmark points. 
//The rest are for individual face components
std::vector<cv::Point> landmarks;

/**at each index of "shapes" vector there is an object of full_object_detection class
which stores the 68 landmark points in dlib::point from,
which needs to be converted back to cv::Point for displaying.*/
dlib::full_object_detection shape = m_sp(dlib::cv_image<unsigned char>(gray), face_DLib[0]);
dlib_point2cv_Point(shape, landmarks, dScale);
// 2D image points. If you change the image, you need to change vector
std::vector<cv::Point2d> image_points;
image_points.push_back(landmarks[30]);    // Nose tip
image_points.push_back(landmarks[8]);    // Chin
image_points.push_back(landmarks[45]);     // Left eye left corner
image_points.push_back(landmarks[36]);    // Right eye right corner
image_points.push_back(landmarks[54]);    // Left Mouth corner
image_points.push_back(landmarks[48]);    // Right mouth corner

// Camera internals
double focal_length = gray.cols; // Approximate focal length.
Point2d center = cv::Point2d(gray.cols / 2, gray.rows / 2);
cv::Mat camera_matrix = (cv::Mat_<double>(3, 3) << focal_length, 0, center.x, 0, 
	focal_length, center.y, 0, 0, 1);
cv::Mat dist_coeffs = cv::Mat::zeros(4, 1, cv::DataType<double>::type); 
// Assuming no lens distortion
// Output rotation and translation
cv::Mat rotation_vector; // Rotation in axis-angle form
cv::Mat translation_vector;

// Solve for pose
cv::solvePnP(m_model_points, image_points, camera_matrix, dist_coeffs, 
	rotation_vector, translation_vector);

// Access the last element in the Rotation Vector
double rot = rotation_vector.at<double>(2, 0);

// Conver to degrees
double theta_deg =   rot / M_PI * 180;
// Rotate around the center
Point2f pt(image.cols / 2., image.rows / 2.);
Mat r = getRotationMatrix2D(pt, theta_deg, 1.0);

// determine bounding rectangle
cv::Rect bbox = cv::RotatedRect(pt, image.size(), theta_deg).boundingRect();
// adjust transformation matrix
center = cv::Point2d(image.cols / 2, image.rows / 2);
r.at<double>(0, 2) += (bbox.width / 2.0 - center.x);
r.at<double>(1, 2) += (bbox.height / 2.0 - center.y);

// Apply affine transform
warpAffine(image, dst, r, dst.size());
//output need to fix
//imwrite("pose2.jpg", dst);
return dst;

}` can any one see my wrong point

nguyenthaohut avatar Feb 22 '19 10:02 nguyenthaohut