我正在尝试检测Elon Musks的面部特征。现在它已经可以,但是我检测到太多的眼睛, Nose 和嘴巴特征。我不确定如何解决此问题,以便只找到一套。

我正在使用opencv的github提供的haar文件,对于 Nose 和嘴巴,我在网上某个地方找到了一些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()

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

最佳答案

在此行中:

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

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



该功能的作用是检测具有定义边界的所有功能。我建议您需要使用这些值,直到矩形的数量达到可接受的数量为止。

实际上,maxSize似乎是一个不错的开始,因为每次检测都有较小的矩形

08-25 00:44