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

问题描述 投票:-2回答:2

使用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()

这里有image我试图获得完美的准确性。

python opencv face-detection cascade-classifier
2个回答
2
投票

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

但哈尔级联现在不是人脸检测的最佳选择。在OpenCV 4.0开发人员中删除了Haar级联培训的代码 - 他们建议使用DNN。 For example here

第二:OpenCV开发人员为DNN推理创建了一个开源框架 - OpenVINO和许多pretrained models(也用于面部检测)。如果你想在CPU上拥有最快的面部检测器而不是你需要使用OpenVINO。


0
投票

除了@ Nuzhny的推荐,你应该使用非最大抑制算法来解决多个检测的问题。

Pyimagesearch有一篇非常好的文章以及有关此主题的代码,可以帮助您。

© www.soinside.com 2019 - 2024. All rights reserved.