如何在使用opencv和python检测人脸时获得单个边界框

如何在使用opencv和python检测人脸时获得单个边界框,python,opencv,face-detection,cascade-classifier,Python,Opencv,Face Detection,Cascade Classifier,在使用opencv检测人脸时,我无法获得完美的准确性 这是我的密码: import cv2 #create a cascadeclassifier object face_cascade = cv2.CascadeClassifier("C:/Users/yash/AppData/Local/Programs/Python/Python35/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml") #create a ca

在使用opencv检测人脸时,我无法获得完美的准确性

这是我的密码:

import cv2

#create a cascadeclassifier object
face_cascade = cv2.CascadeClassifier("C:/Users/yash/AppData/Local/Programs/Python/Python35/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml")
#create a cascade classifier.it will contain the features of the face

#reading the image as it is
img = cv2.imread("profile.JPG")

#reading the image as gray_scale image
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #converting colored image to gray scale

#search the co-ordinates of the image
faces = face_cascade.detectMultiScale(gray_img,scaleFactor = 1.05,minNeighbors=5)
#scaleFactor = decreases the shape value by 5%,until the face is found .smaller this value , the greater is the accuracy.
#detectMultiScale = method to search for the face rectangle co-ordinates

#print(type(faces))
#print(faces)

for x,y,w,h in faces:
    img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)

resized_img = cv2.resize(img,(int(img.shape[1]/2) , int(img.shape[0]/2)))    
cv2.imshow("face detection",resized_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

这里有一个我正试图获得完美精度的参数。

对于一个面,使用标志CV_HAAR_FIND_Maximum_OBJECT作为detectMultiScale中的最后一个参数

但是现在,Haar cascades并不是人脸检测的最佳选择。在OpenCV 4.0中,开发人员删除了用于Haar cascades培训的代码-他们建议使用DNN


第二:OpenCV开发人员为DNN推理创建了一个开源框架——还有很多(用于人脸检测)。如果您想在CPU上拥有比使用OpenVINO更快的人脸检测器。

除了@Nuzhny的建议之外,您还应该使用非最大抑制算法来解决多次检测的问题


有一篇很好的文章以及关于这个主题的代码,这将对您有所帮助。

看起来很不错,有什么问题吗?单击图像链接。它会看到两个矩形你说的“完美”是什么意思?100%准确率还是只有一个边界框?在任何分类任务中都很难实现“完美准确率”。您可能需要查看该声明,因为这可能会让人感到困惑。@Nuzhny不起作用……不再起作用