示例#1
0
    def _process_frame(self, frame, frame_index, pixel_diff_threshold,
                       pixel_count_threshold, beta):
        # HACK naive implementation of background subtraction
        foreground = get_foreground(frame, self.background,
                                    pixel_diff_threshold)
        display_foreground = get_foreground(frame, self.display_background,
                                            pixel_diff_threshold)
        # update background with running average
        self.display_background = self.display_background * beta + frame * (
            1. - beta)

        # get the two opposite vertices of real bounding box
        xx, yy = np.nonzero(foreground)
        upper, lower, left, right = find_bounding_box(
            xx, yy, pix_count_thres=pixel_count_threshold)
        vertices = ((left, upper), (right, lower))

        # check whether the foreground is located in mute areas
        for rect in self.mute_rects:
            fill_zero(foreground, rect)

        # get the basic stats of foreground with muted areas (points_count, value, area, ratio, obliqueness)
        value = np.sum(foreground)
        xx, yy = np.nonzero(foreground)
        u_l_l_r = find_bounding_box(xx,
                                    yy,
                                    pix_count_thres=pixel_count_threshold
                                    )  # returns upper, lower, left, right
        bounding_rectangle = Rectangle(*u_l_l_r)
        area = bounding_rectangle.get_area()
        ratio = bounding_rectangle.get_ratio()
        try:
            coords = coords_within_boundary(xx, yy, *u_l_l_r, zero_mean=True)
            self.pca_solver.fit(coords)
            obliqueness = self.pca_solver.explained_variance_ratio_
        except:
            obliqueness = 0.5

        # check whether the shape of foreground should trigger the alarm
        self.parameter_list.append((len(xx), value, area, ratio, obliqueness))
        if self.threshold.check(len(xx), value, area, ratio, obliqueness):
            cv2.rectangle(frame, *vertices, color=(0, 0, 255))
            cv2.putText(frame, "WARNING", (left, upper),
                        cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255))
            self._trigger_alarm(frame_index, frame)
        else:
            cv2.rectangle(frame, *vertices, color=(255, 0, 0))
            cv2.putText(frame, "Object", (left, upper),
                        cv2.FONT_HERSHEY_DUPLEX, 1, (255, 0, 0))
        return frame, display_foreground