OpenCV代码不会检测人脸的数量,也不会在脸周围放一个圆圈

opencv code does not detect the number of faces neither puts a circle around the face

本文关键字:周围放 一个 检测 代码 OpenCV      更新时间:2023-10-16

我的代码:

#include "opencv2/opencv.hpp"
#include <iostream>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
VideoCapture cap(0);

CascadeClassifier cascade, netCascade;
double scale = 1;
netCascade.load("/opt/opencv/data/haarcascades/haarcascade_frontalcatface.xml");
cascade.load("/opt/opencv/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml");

//cap.open();
if(!cap.isOpened())
{
cout<<"error opening video file"<<endl;
return -1;
}

while(1)
{
Mat img;
cap>>img;
if(img.empty())
{
break;
}

//Mat img1 = img.clone();
vector<Rect> faces, faces2;
Mat grey, smallImg;
cvtColor(img, grey, COLOR_BGR2GRAY);
double fx = 1 / scale;
resize(grey, smallImg, Size(), fx, fx, INTER_LINEAR);
equalizeHist(smallImg, smallImg);
cascade.detectMultiScale(smallImg, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30,30));

cout<<"number of faces = "<<faces.size()<<endl;
for(size_t i = 0; i < faces.size(); i++)
{
Rect r = faces[i];
Mat smallImgROI;
vector<Rect> nestedObjects;
Point center;
Scalar color = Scalar(255, 0, 0); // Color for Drawing tool
int radius;
double aspect_ratio = (double)r.width/r.height;

if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
{
center.x = cvRound((r.x + r.width*0.5)*scale);
center.y = cvRound((r.y + r.height*0.5)*scale);
radius = cvRound((r.width + r.height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
else
rectangle( img, cv::Point(cvRound(r.x*scale), cvRound(r.y*scale)),
cv::Point(cvRound((r.x + r.width-1)*scale),
cvRound((r.y + r.height-1)*scale)), color, 3, 8, 0);
if( netCascade.empty() )
continue;
smallImgROI = smallImg( r );

}


imshow("video",img);
char c = (char) waitKey(25);
if(c == 27 || c == 'q' || c == 'Q')
{
break;
}
}
cap.release();
destroyAllWindows();
return 0;
}

首先打开笔记本电脑的摄像头。然后捕获视频。然后分配垫架。然后转换为灰度图像,调整大小。分配一个与 xml 文件匹配的人脸向量。使用 face.size(( 查找面孔数。使用 cout 在命令行上打印面孔数(faces.size(((。在脸上画圆圈。但是当我坐在它前面时,面孔数被打印为 0。我认为它应该打印 1。此外,它不会在脸周围画圆圈。我哪里做错了?当我的脸靠近屏幕时,它有时会在我的两只眼睛周围显示 2 个圆圈,但并非总是如此。有时,faces.size(( 返回 3。这怎么可能具有值 3。为什么它不总是画圆?

若要检测人脸,必须调用人脸级联分类器上的detectMultiScale

#include "opencv2/opencv.hpp"
#include <iostream>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
VideoCapture cap(0);

CascadeClassifier face_cc;
double scale = 1;
face_cc.load("/opt/opencv/data/haarcascades/haarcascade_frontalcatface.xml");

//cap.open();
if(!cap.isOpened())
{
cout<<"error opening video file"<<endl;
return -1;
}
while(1)
{
Mat img;
cap>>img;
if(img.empty())
{
break;
}

//Mat img1 = img.clone();
vector<Rect> faces, faces2;
Mat grey, smallImg;
cvtColor(img, grey, COLOR_BGR2GRAY);
double fx = 1 / scale;
resize(grey, smallImg, Size(), fx, fx, INTER_LINEAR);
equalizeHist(smallImg, smallImg);
//cascade.detectMultiScale(smallImg, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30,30));
//detect faces
face_cc.detectMultiScale( smallImg, faces );

cout<<"number of faces = "<<faces.size()<<endl;
for(size_t i = 0; i < faces.size(); i++)
{
Rect r = faces[i];
Mat smallImgROI;
vector<Rect> nestedObjects;
Point center;
Scalar color = Scalar(255, 0, 0); // Color for Drawing tool
int radius;
double aspect_ratio = (double)r.width/r.height;

if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
{
center.x = cvRound((r.x + r.width*0.5)*scale);
center.y = cvRound((r.y + r.height*0.5)*scale);
radius = cvRound((r.width + r.height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
else
{
rectangle( img, cv::Point(cvRound(r.x*scale), cvRound(r.y*scale)),
cv::Point(cvRound((r.x + r.width-1)*scale),
cvRound((r.y + r.height-1)*scale)), color, 3, 8, 0);
}
smallImgROI = smallImg( r );
}
imshow("video",img);
char c = (char) waitKey(25);
if(c == 27 || c == 'q' || c == 'Q')
{
break;
}
}
cap.release();
destroyAllWindows();
return 0;
}