def detect(self, img, cascade):
        faces = []
        bias = 15
        rects = cascade.detectMultiScale(img, **self.haar_params)
        if len(rects) == 0:
            return []
        rects[:, 2:] += rects[:, :2]
        for x1, y1, x2, y2 in rects:
            if x1 - bias > 0:
                x1 = x1 - bias
            else:
                x1 = x1

            if y1 - bias > 0:
                y1 = y1 - bias
            else:
                y1 = y1

            if x2 + bias < imtools.get_width(img):
                x2 = x2 + bias
            else:
                x2 = x2

            if y2 + bias < imtools.get_height(img):
                y2 = y2 + bias
            else:
                y2 = y2

            faces.append([x1, y1, x2, y2])

        return faces
    def detect_face(self, img):
        self.rects = []
        self.faces = []
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        gray = cv2.equalizeHist(gray)

        rects = self.detect(gray, self.cascade_1)
        if len(rects) == 0:
            rects = self.detect(gray, self.cascade_3)
        if len(rects) == 0:
            rects = self.detect(gray, self.cascade_2)
        self.rects = rects
        copy = img.copy()
        self.draw_rects(copy, rects, self.colors['green'])

        if len(rects) > 0:
            self.last_rects = rects
            self.face_hit += 1
        else:
            if self.take_last_rects:
                rects = self.last_rects
            self.face_miss += 1

        for rect in rects:
            (x1, y1, x2, y2) = rect
            # get detected face images
            face = self.fetch_face(img, (x1, y1, x2, y2))
            self.faces.append(face)
            name = self.recognize_face(face)

            if name is not None:  # and name in self.name_rect:
                self.name_rect[name] = rect
                self.trace_face(name, imtools.get_width(img))
                if str(name) == str(self.target):
                    self.id_hit += 1
                    color = self.colors['red']
                else:
                    self.id_miss += 1
                    color = self.colors['green']

                cv2.rectangle(copy, (x1, y1), (x2, y2), color, 2)
                cv2.putText(copy, name, (x1, y1 - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)

        # detect eyes and draw the roi of eyes
        # for x1, y1, x2, y2 in rects:
        #     roi = gray[y1:y2, x1:x2]
        #     copy_roi = copy[y1:y2, x1:x2]
        #     subrects = self.detect(roi.copy(), self.cascade_eye)
        #     self.draw_rects(copy_roi, subrects, self.colors['red'])

        return copy