gpt4 book ai didi

c++ - Qt和opencv中的人脸检测

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:36 25 4
gpt4 key购买 nike

我使用 opencv 库在 C++ 中编写了一些人脸检测代码。在这里,我使用面部控制鼠标指针。当我在 Qt 中编写相同的代码时,它不会加载 haarcascade xml 文件

这是我的 C++ 代码:

#include<opencv2/opencv.hpp> 
#include<opencv2/highgui/highgui.hpp>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
using namespace cv;

CascadeClassifier faceCade;

String faceCascadeName = "lbpcascade_frontalface.xml";
String FaceDetectWindow = "Face Detector Window";
String FaceDetectGrayWindow = "Face Detector Gray Window";


int main() {

VideoCapture cap(0);
Mat camFrames, grayFrames;
vector<Rect> faces;
long imageIndex = 0;

int preX=0,preY=0;
int j=0;

if( !faceCade.load( faceCascadeName ) ){ cout<<"--(!)Error loading\n"; return -1; };

while (1) {
cap >> camFrames;

cvtColor(camFrames, grayFrames, CV_BGR2GRAY);
equalizeHist(grayFrames, grayFrames);

faceCade.detectMultiScale(grayFrames, faces, 1.1, 2, 0, Size(160, 160));

for (int i = 0; i < faces.size(); i++)
{
Mat faceROI = grayFrames(faces[i]);

rectangle(camFrames, Rect(faces[i].x - 25,faces[i].y - 25,faces[i].width + 35 ,faces[i].height + 35), Scalar(0, 0, 255), 1, 1, 0);

Point center(faces[i].x + faces[i].width * 0.5,faces[i].y + faces[i].height * 0.5);


cout<<"\nx = "<<faces[i].x<<"\ty = "<<faces[i].y;

//while(j==10)
//{
if((faces[i].y<125)&&(faces[i].x<255))
{
printf(" RU");
system("xte 'mousermove 10 -10'");
}
else if((faces[i].y>185)&&(faces[i].x<255)){
printf(" RD");
system("xte 'mousermove 10 10'");
}
else if((faces[i].y>185)&&(faces[i].x>305)){
printf(" LD");
system("xte 'mousermove -10 10'");
}
else if((faces[i].y<125)&&(faces[i].x<255)){
printf(" LU");
system("xte 'mousermove -10 -10'");
}
else if((faces[i].y>185)&&(faces[i].x>255)&&(faces[i].x<305)){
printf(" Down");
system("xte 'mousermove 0 10'");
}
else if((faces[i].y<125)&&(faces[i].x>255)&&(faces[i].x<305)){
printf(" up");
system("xte 'mousermove 0 -10'");
}
else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x<255)){
printf(" L");
system("xte 'mousermove -10 0'");
}
else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x>305)){
printf(" R");
system("xte 'mousermove 10 0'");
}
preX = faces[i].x;
preY = faces[i].y;
j=0;
}
//j++;
//}

imshow(FaceDetectWindow, camFrames);
imshow(FaceDetectGrayWindow, grayFrames);
if(waitKey(30) >= 0) break;

}

}

这里检测到人脸,但在下面的 qt 代码中它不起作用

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
using namespace cv;

CascadeClassifier faceCade;

String faceCascadeName = ":/new/prefix1/lbpcascade_frontalface.xml";
String FaceDetectWindow = "Face Detector Window";
String FaceDetectGrayWindow = "Face Detector Gray Window";


int main() {

VideoCapture cap(0);
Mat camFrames, grayFrames;
vector<Rect> faces;
long imageIndex = 0;

int preX=0,preY=0;
int j=0;

if( !faceCade.load( "lbpcascade_frontalface.xml" ) ){ cout<<"--(!)Error loading\n"; return -1; };

while (1) {
cap >> camFrames;

cvtColor(camFrames, grayFrames, CV_BGR2GRAY);
equalizeHist(grayFrames, grayFrames);

faceCade.detectMultiScale(grayFrames, faces, 1.1, 2, 0, Size(80, 80));

/* for (int i = 0; i < faces.size(); i++)
{
Mat faceROI = grayFrames(faces[i]);

rectangle(camFrames, Rect(faces[i].x - 25,faces[i].y - 25,faces[i].width + 35 ,faces[i].height + 35), Scalar(0, 0, 255), 1, 1, 0);

Point center(faces[i].x + faces[i].width * 0.5,faces[i].y + faces[i].height * 0.5);


cout<<"\nx = "<<faces[i].x<<"\ty = "<<faces[i].y;

//while(j==10)
//{
if((faces[i].y<125)&&(faces[i].x<255))
{
printf(" RU");
system("xte 'mousermove 10 -10'");
}
else if((faces[i].y>185)&&(faces[i].x<255)){
printf(" RD");
system("xte 'mousermove 10 10'");
}
else if((faces[i].y>185)&&(faces[i].x>305)){
printf(" LD");
system("xte 'mousermove -10 10'");
}
else if((faces[i].y<125)&&(faces[i].x<255)){
printf(" LU");
system("xte 'mousermove -10 -10'");
}
else if((faces[i].y>185)&&(faces[i].x>255)&&(faces[i].x<305)){
printf(" Down");
system("xte 'mousermove 0 10'");
}
else if((faces[i].y<125)&&(faces[i].x>255)&&(faces[i].x<305)){
printf(" up");
system("xte 'mousermove 0 -10'");
}
else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x<255)){
printf(" L");
system("xte 'mousermove -10 0'");
}
else if((faces[i].y>125)&&(faces[i].y<185)&&(faces[i].x>305)){
printf(" R");
system("xte 'mousermove 10 0'");
}
preX = faces[i].x;
preY = faces[i].y;
j=0;
}
//j++;
//}*/

imshow(FaceDetectWindow, camFrames);
imshow(FaceDetectGrayWindow, grayFrames);
if(waitKey(30) >= 0) break;

}

}

请帮我在qt平台上做那个这里 qt 代码中显示的输出是 --(!)Error loading这意味着没有加载级联文件

最佳答案

String faceCascadeName = ":/new/prefix1/lbpcascade_frontalface.xml";

这是一个有效的路径,但你需要在Qt资源文件中创建它。

http://qt-project.org/doc/qt-5.0/qtcore/resources.html

否则,如果你不想处理资源,就去让它成为一个相对路径。

例如:

./file.xml" 将从工作目录中查找 xml 文件。

"../file.xml" 将查找工作目录上一级的 xml 文件。

希望对您有所帮助。

关于c++ - Qt和opencv中的人脸检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19469817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com