繁体   English   中英

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

[英]How to get single bounding box while detecting face using opencv and python

使用 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_BIGGEST_OBJECT 作为detectMultiScale 中的最后一个参数。

但是 Haar 级联现在不是人脸检测的最佳选择。 在 OpenCV 4.0 中,开发人员删除了 Haar 级联训练的代码——他们推荐使用 DNN。 例如这里

第二:OpenCV 开发人员为 DNN 推理创建了一个开源框架 - OpenVINO和许多预训练模型(也用于人脸检测)。 如果您想在 CPU 上拥有最快的面部检测器,则需要使用 OpenVINO。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM