从俯仰角检测头部点头姿势
Detection of head nod pose from pitch angle
我正在开发一个 android 应用程序,可以通过点头姿势检测驾驶员的疲劳程度。
What I did:
- 我使用 dlib 库从图像中检测到 68 个面部标志。
- 使用 solvePnP 找出旋转矩阵,从该矩阵我得到了滚动角、俯仰角和偏航角。
现在,我必须从俯仰角度检测点头。
My Problem:
- 如何设置阈值,使低于或高于阈值的角度都可以说是点头?
- 它会产生一些负角。 3维轴上的负角是什么意思?
我的代码:
void getEulerAngles(Mat &rotCamerMatrix,Vec3d &eulerAngles)
{
Mat cameraMatrix,rotMatrix,transVect,rotMatrixX,rotMatrixY,rotMatrixZ;
double* _r = rotCamerMatrix.ptr<double>();
double projMatrix[12] = {_r[0],_r[1],_r[2],0,
_r[3],_r[4],_r[5],0,
_r[6],_r[7],_r[8],0};
decomposeProjectionMatrix( Mat(3,4,CV_64FC1,projMatrix),
cameraMatrix,
rotMatrix,
transVect,
rotMatrixX,
rotMatrixY,
rotMatrixZ,
eulerAngles);
}
int renderToMat(std::vector<full_object_detection>& dets, Mat& dst)
{
Scalar color;
std::vector<cv::Point2d> image_points;
std::vector<cv::Point3d> model_points;
string disp;
int sz = 3,l;
color = Scalar(0,255,0);
double p1,p2,p3,leftear,rightear,ear=0,yawn=0.00,yaw=0.00,pitch=0.00,roll=0.00;
l=dets.size();
//I am calculating only for one face.. so assuming l=1
for(unsigned long idx = 0; idx < l; idx++)
{
image_points.push_back(
Point2d(dets[idx].part(30).x(),dets[idx].part(30).x() ) );
image_points.push_back(Point2d(dets[idx].part(8).x(),dets[idx].part(8).x() ) );
image_points.push_back(Point2d(dets[idx].part(36).x(),dets[idx].part(36).x() ) );
image_points.push_back(Point2d(dets[idx].part(45).x(),dets[idx].part(45).x() ) );
image_points.push_back(Point2d(dets[idx].part(48).x(),dets[idx].part(48).x() ) );
image_points.push_back(Point2d(dets[idx].part(54).x(),dets[idx].part(54).x() ) );
}
double focal_length = dst.cols;
Point2d center = cv::Point2d(dst.cols/2.00,dst.rows/2.00);
cv::Mat camera_matrix = (cv::Mat_<double>(3.00,3.00) << 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);
cv::Mat rotation_vector; //s Rotation in axis-angle form
cv::Mat translation_vector;
cv::Mat rotCamerMatrix1;
if(l!=0)
{
model_points.push_back(cv::Point3d(0.0f, 0.0f, 0.0f));
model_points.push_back(cv::Point3d(0.0f, -330.0f, -65.0f));
model_points.push_back(cv::Point3d(-225.0f, 170.0f, -135.0f));
model_points.push_back(cv::Point3d(225.0f, 170.0f, -135.0f));
model_points.push_back(cv::Point3d(-150.0f, -150.0f, -125.0f));
model_points.push_back(cv::Point3d(150.0f, -150.0f, -125.0f));
cv::solvePnP(model_points, image_points, camera_matrix, dist_coeffs,rotation_vector, translation_vector);
Rodrigues(rotation_vector,rotCamerMatrix1);
Vec3d eulerAngles;
getEulerAngles(rotCamerMatrix1,eulerAngles);
yaw = eulerAngles[1];
pitch = eulerAngles[0];
roll = eulerAngles[2];
/*My problem begins here. I don't know how to set a threshold value for pitch so that I can say a value below or
above the pitch is a head nod!*/
}
return 0;
}
首先,您必须了解在 3D 环境中如何使用 3 个角度。基本上,它们相对于原点旋转 3D space 中的对象(原点可以根据上下文改变),但是如何应用 3 个角度?
这道题可以表述为:他们按什么顺序旋转物体。如果你应用偏航,然后倾斜然后滚动它可能会给你一个不同的对象方向,如果你以相反的顺序进行。话虽如此,您必须了解这些值代表什么,才能了解如何处理它们。
现在,你问什么是好的阈值,这取决于。什么?好吧,按照它们的应用顺序。例如,如果您首先应用 45 度的俯仰,它会向下看,然后您应用 180 度的滚动,然后它会向上看,因此定义阈值有点困难。
既然你有你的模型点,你可以创建一个 3D 旋转矩阵并以不同的俯仰角将其应用于它们(其余角度将为 0,因此这里的顺序不重要)并可视化它们和选择您认为正在点头的那个。这有点主观,所以您应该是做这件事的人。
现在,第二个问题。答案再次是,这取决于。这次靠什么?你可能会问。很简单,你的系统是左手还是右手?其中一个意味着顺时针旋转,另一个逆时针旋转,负号改变方向。所以,在左手系统中,它是顺时针的,负号是逆时针的。右手系为逆时针,负号为顺时针。
作为建议,您可以制作一个矢量 (0,0,-1),假设您的 z 轴朝后看。然后对其应用旋转并将其投影到平行于 z 轴的 2D 平面,这里取矢量的尖端,看看这里的角度是多少。这样你就确定你得到了什么。
我正在开发一个 android 应用程序,可以通过点头姿势检测驾驶员的疲劳程度。
What I did:
- 我使用 dlib 库从图像中检测到 68 个面部标志。
- 使用 solvePnP 找出旋转矩阵,从该矩阵我得到了滚动角、俯仰角和偏航角。 现在,我必须从俯仰角度检测点头。
My Problem:
- 如何设置阈值,使低于或高于阈值的角度都可以说是点头?
- 它会产生一些负角。 3维轴上的负角是什么意思?
我的代码:
void getEulerAngles(Mat &rotCamerMatrix,Vec3d &eulerAngles)
{
Mat cameraMatrix,rotMatrix,transVect,rotMatrixX,rotMatrixY,rotMatrixZ;
double* _r = rotCamerMatrix.ptr<double>();
double projMatrix[12] = {_r[0],_r[1],_r[2],0,
_r[3],_r[4],_r[5],0,
_r[6],_r[7],_r[8],0};
decomposeProjectionMatrix( Mat(3,4,CV_64FC1,projMatrix),
cameraMatrix,
rotMatrix,
transVect,
rotMatrixX,
rotMatrixY,
rotMatrixZ,
eulerAngles);
}
int renderToMat(std::vector<full_object_detection>& dets, Mat& dst)
{
Scalar color;
std::vector<cv::Point2d> image_points;
std::vector<cv::Point3d> model_points;
string disp;
int sz = 3,l;
color = Scalar(0,255,0);
double p1,p2,p3,leftear,rightear,ear=0,yawn=0.00,yaw=0.00,pitch=0.00,roll=0.00;
l=dets.size();
//I am calculating only for one face.. so assuming l=1
for(unsigned long idx = 0; idx < l; idx++)
{
image_points.push_back(
Point2d(dets[idx].part(30).x(),dets[idx].part(30).x() ) );
image_points.push_back(Point2d(dets[idx].part(8).x(),dets[idx].part(8).x() ) );
image_points.push_back(Point2d(dets[idx].part(36).x(),dets[idx].part(36).x() ) );
image_points.push_back(Point2d(dets[idx].part(45).x(),dets[idx].part(45).x() ) );
image_points.push_back(Point2d(dets[idx].part(48).x(),dets[idx].part(48).x() ) );
image_points.push_back(Point2d(dets[idx].part(54).x(),dets[idx].part(54).x() ) );
}
double focal_length = dst.cols;
Point2d center = cv::Point2d(dst.cols/2.00,dst.rows/2.00);
cv::Mat camera_matrix = (cv::Mat_<double>(3.00,3.00) << 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);
cv::Mat rotation_vector; //s Rotation in axis-angle form
cv::Mat translation_vector;
cv::Mat rotCamerMatrix1;
if(l!=0)
{
model_points.push_back(cv::Point3d(0.0f, 0.0f, 0.0f));
model_points.push_back(cv::Point3d(0.0f, -330.0f, -65.0f));
model_points.push_back(cv::Point3d(-225.0f, 170.0f, -135.0f));
model_points.push_back(cv::Point3d(225.0f, 170.0f, -135.0f));
model_points.push_back(cv::Point3d(-150.0f, -150.0f, -125.0f));
model_points.push_back(cv::Point3d(150.0f, -150.0f, -125.0f));
cv::solvePnP(model_points, image_points, camera_matrix, dist_coeffs,rotation_vector, translation_vector);
Rodrigues(rotation_vector,rotCamerMatrix1);
Vec3d eulerAngles;
getEulerAngles(rotCamerMatrix1,eulerAngles);
yaw = eulerAngles[1];
pitch = eulerAngles[0];
roll = eulerAngles[2];
/*My problem begins here. I don't know how to set a threshold value for pitch so that I can say a value below or
above the pitch is a head nod!*/
}
return 0;
}
首先,您必须了解在 3D 环境中如何使用 3 个角度。基本上,它们相对于原点旋转 3D space 中的对象(原点可以根据上下文改变),但是如何应用 3 个角度?
这道题可以表述为:他们按什么顺序旋转物体。如果你应用偏航,然后倾斜然后滚动它可能会给你一个不同的对象方向,如果你以相反的顺序进行。话虽如此,您必须了解这些值代表什么,才能了解如何处理它们。
现在,你问什么是好的阈值,这取决于。什么?好吧,按照它们的应用顺序。例如,如果您首先应用 45 度的俯仰,它会向下看,然后您应用 180 度的滚动,然后它会向上看,因此定义阈值有点困难。
既然你有你的模型点,你可以创建一个 3D 旋转矩阵并以不同的俯仰角将其应用于它们(其余角度将为 0,因此这里的顺序不重要)并可视化它们和选择您认为正在点头的那个。这有点主观,所以您应该是做这件事的人。
现在,第二个问题。答案再次是,这取决于。这次靠什么?你可能会问。很简单,你的系统是左手还是右手?其中一个意味着顺时针旋转,另一个逆时针旋转,负号改变方向。所以,在左手系统中,它是顺时针的,负号是逆时针的。右手系为逆时针,负号为顺时针。
作为建议,您可以制作一个矢量 (0,0,-1),假设您的 z 轴朝后看。然后对其应用旋转并将其投影到平行于 z 轴的 2D 平面,这里取矢量的尖端,看看这里的角度是多少。这样你就确定你得到了什么。