Example #1
0
    def update(self, image):
        self._faces = []

        if utils.is_gray(image):
            image = cv2.equalizeHist(image)
        else:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            cv2.equalizeHist(image, image)

        min_size = utils.width_height_devided_by(image, 8)
        face_rects = self._face_classifier.detectMultiScale(image,
                                                            self.scale_factor,
                                                            self.min_neighbors,
                                                            self.flags, min_size)

        if face_rects is not None:
            for face_rect in face_rects:
                face = Face()
                face.face_rect = face_rect
                x, y, w, h = face_rect

                search_rect = (x + w / 7, y, w * 2 / 7, h / 2)
                face.left_eye_rect = self._detect_one_object(self._eye_classifier, image, search_rect, 64)

                search_rect = (x + w * 4 / 7, y, w * 2 / 7, h / 2)
                face.right_eye_rect = self._detect_one_object(self._eye_classifier, image, search_rect, 64)

                search_rect = (x + w / 4, y + h /4, w / 2, h / 2)
                face.nose_rect = self._detect_one_object(self._nose_classifier, image, search_rect, 32)

                search_rect = (x + w / 6, y + h * 2 / 3, w * 2 / 3, h / 3)
                face.mouth_rect = self._detect_one_object(self._mouth_classifier, image, search_rect, 16)

                self._faces.append(face)
Example #2
0
    def _detect_one_object(self, classifier, image, rect, image_size_to_min_size_ratio):
        x, y, w, h = rect
        min_size = utils.width_height_devided_by(image, image_size_to_min_size_ratio)
        sub_image = image[y:y + h, x: x + w]
        sub_rects = classifier.detectMultiScale(sub_image, self.scale_factor, self.min_neighbors, self.flags, min_size)

        if len(sub_rects) == 0:
            return None

        sub_x, sub_y, sub_w, sub_h = sub_rects[0]

        return x + sub_x, y + sub_y, sub_w, sub_h