opencv haar 文件返回太多面部特征

opencv haar files returning too many facial features

我正在尝试检测 Elon Musk 的面部特征。现在它有点能够,但我检测到太多的眼睛、鼻子和嘴巴特征。我不确定如何解决此问题以仅找到一组。

我正在使用 opencv github 提供的 haar 文件,对于鼻子和嘴巴,我在网上某处找到了一些 haar 文件。

haarcascade_frontalface_default.xml
haarcascade_eye.xml

haar 文件

class Filterize(object):
    def __init__(self, filterpath):
        self.filterpath = filterpath
        self.haarpath = os.path.join(os.getcwd(), 'haar')
        self.face_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'face.xml'))
        self.eye_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'eye.xml'))
        self.nose_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'nose.xml'))
        self.mouth_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'mouth.xml'))

    def get_filter_facial_features(self):
        filter = cv2.imread(self.filterpath)
        grayscale_filter = cv2.cvtColor(filter, cv2.COLOR_BGR2GRAY)
        face = self.face_cascade.detectMultiScale(grayscale_filter, 1.3, 5)
        for x, y, w, h in face:
            cv2.rectangle(filter, (x, y), (x + w, y + h), (255, 0, 0), 2)
            roi_gray = grayscale_filter[y:y + h, x:x + w]
            roi_color = filter[y:y + h, x:x + w]
            eyes = self.eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
            nose = self.nose_cascade.detectMultiScale(roi_gray, 1.3, 5)
            mouth = self.mouth_cascade.detectMultiScale(roi_gray, 1.3, 5)
            for eye_x, eye_y, eye_w, eye_h in eyes:
                cv2.rectangle(roi_color, (eye_x, eye_y), (eye_x + eye_w, eye_y + eye_h), (0, 255, 0), 2)
            for nose_x, nose_y, nose_w, nose_h in nose:
                cv2.rectangle(roi_color, (nose_x, nose_y), (nose_x + nose_w, nose_y + nose_h), (0, 255, 0), 2)
            for mouth_x, mouth_y, mouth_w, mouth_h in mouth:
                cv2.rectangle(roi_color, (mouth_x, mouth_y), (mouth_x + mouth_w, mouth_y + mouth_h), (0, 255, 0), 2)
            cv2.imwrite('face.png', filter)

创建照片:

a = Filterize(filterpath)
a.get_filter_facial_features()

在此行中:

face = self.face_cascade.detectMultiScale(grayscale_filter, 1.3, 5)

您传入以下可用参数(取自the docs):

Parameters:

  • cascade – Haar classifier cascade (OpenCV 1.x API only). It can be loaded from XML or YAML file using Load(). When the cascade is not
    needed anymore, release it using
    cvReleaseHaarClassifierCascade(&cascade).
  • image – Matrix of the type CV_8U containing an image where objects are detected. objects – Vector of rectangles where each rectangle contains the detected object.
  • scaleFactor – Parameter specifying how much the image size is reduced at each image scale.
  • minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.

  • flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.

  • minSize – Minimum possible object size. Objects smaller than that are ignored.

  • maxSize – Maximum possible object size. Objects larger than that are ignored.

此函数的作用是检测它可以定义边界的所有特征。我建议您需要尝试使用这些值,直到矩形的数量达到可接受的数量。

事实上,maxSize 看起来是一个好的开始,因为每次检测都有较小的矩形