【发布时间】:2015-01-08 07:01:30
【问题描述】:
我开发了一个使用 OpenCV 库通过摄像头跟踪面部的项目。
我使用带有haarcascade_frontalface_alt.xml 的haar 级联来检测人脸。
我的问题是,如果从网络摄像头捕获的图像不包含任何人脸,则检测人脸的过程非常缓慢,因此来自摄像头的图像(连续向用户显示)会延迟。
我的源代码:
void camera()
{
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eye_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";
VideoCapture cap(0);
if (!face_cascade.load(face_cascade_name))
printf("--(!)Error loading\n");
if (!eyes_cascade.load(eye_cascade_name))
printf("--(!)Error loading\n");
if (!cap.isOpened())
{
cerr << "Capture Device ID " << 0 << "cannot be opened." << endl;
}
else
{
Mat frame;
vector<Rect> faces;
vector<Rect> eyes;
Mat original;
Mat frame_gray;
Mat face;
Mat processedFace;
for (;;)
{
cap.read(frame);
original = frame.clone();
cvtColor(original, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray, faces, 2, 0,
0 | CASCADE_SCALE_IMAGE, Size(200, 200));
if (faces.size() > 0)
rectangle(original, faces[0], Scalar(0, 0, 255), 2, 8, 0);
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
imshow(window_name, original);
}
if (waitKey(30) == 27)
break;
}
}
【问题讨论】:
-
“非常慢”是什么意思?你的图像解决方案是什么?
face_cascade.detectMultiScale需要一些时间。如果你已经使用了发布模式的 opencv 库,那么可能还有一次机会:也许 openCV 也有一个 gpu haarcascade 检测器,但我不知道。 -
我的图像解决方案是 640 x 480。如果图像不包含人脸,我的意思是函数 detectMultiscale 处理后检测人脸的速度非常慢:(
-
所以只有没有检测到人脸才慢,有人脸就快?!?
-
这不可能吧??
标签: c++ opencv face-detection face-recognition