opencv代码没有检测到人脸的数量也没有在人脸周围放一个圆圈
Posted
技术标签:
【中文标题】opencv代码没有检测到人脸的数量也没有在人脸周围放一个圆圈【英文标题】:opencv code does not detect the number of faces neither puts a circle around the face 【发布时间】:2020-10-14 20:45:51 【问题描述】:我的代码:
#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。为什么它不总是画圆?
【问题讨论】:
你没有检测到人脸,没有使用netCascade。 那我应该用什么? 【参考方案1】:要检测人脸,必须在人脸级联分类器上调用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;
【讨论】:
以上是关于opencv代码没有检测到人脸的数量也没有在人脸周围放一个圆圈的主要内容,如果未能解决你的问题,请参考以下文章