Esempio n. 1
0
    def execute(self) -> StageResult:
        """
        Executes all stages in this pipeline sequentially.
        """

        result = None
        for stage in self.stages:
            stage.before_execute()
            result = stage.execute()
            stage.after_execute()

            if result.continue_pipeline and not result.next_stage:
                break

            if not result.continue_pipeline:
                return StageResult(False, None)

        should_continue = False

        if not self.stages:
            should_continue = True

        if result is not None:
            should_continue = result.next_stage

        return StageResult(True, should_continue)
Esempio n. 2
0
    def execute(self) -> StageResult:
        """
        Tests for the press of the "Q" key or the end of the video.
        """

        if cv2.waitKey(25) & 0xFF == ord("q"):
            return StageResult(False, None)

        return StageResult(True, True)
    def execute(self) -> StageResult:
        """
        Registers the history frame.
        """

        history_frames = self._history_frames.history_frames

        transformation_matrices = self._transformation_matrices.transformation_matrices

        self.shifted_history_frames = [
            Frame(
                cv2.warpPerspective(
                    history_frame.get_frame(),
                    M,
                    (
                        history_frame.get_frame().shape[1],
                        history_frame.get_frame().shape[0],
                    ),
                ).astype(np.uint8),
                history_frame.get_frame_number(),
            ) for history_frame, M in zip(history_frames,
                                          transformation_matrices)
        ]

        return StageResult(True, True)
Esempio n. 4
0
    def execute(self) -> StageResult:
        ShowLastFrame.execute(self)

        # This searches the previous object for frame types.
        if not self._frame_video_writers:
            self._frame_video_writers = {
                f: cv2.VideoWriter(
                    "./output/{stage_name}.{frame_attribute}.mp4".format(
                        stage_name=type(self._dependent).__name__, frame_attribute=f,
                    ),
                    cv2.VideoWriter_fourcc(*"mp4v"),
                    self._capture.fps,
                    (self._capture.frame_width, self._capture.frame_height),
                )
                for f in self._frame_attributes
            }

        # Write one frame to the video for each frame object.
        for frame_attribute in self._frame_attributes:
            frame = getattr(self._dependent, frame_attribute).get_frame()
            if len(frame.shape) == 2:
                frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

            self._frame_video_writers[frame_attribute].write(frame)

        return StageResult(True, True)
    def execute(self) -> StageResult:
        """
        Displays the frame within a window for the user to see.
        Automatically sizes the window to the user's screen.
        """

        if os.environ.get("DISPLAY", "") != "":
            # This searches the previous object for frame types.
            if not self._frame_attributes:
                self._frame_attributes = [
                    a
                    for a in dir(self._dependent)
                    if isinstance(getattr(self._dependent, a), Frame)
                ]

            # Display one cv2.imshow for each frame object.
            for frame_attribute in self._frame_attributes:
                cv2.imshow(
                    "{stage_name}.{frame_attribute}".format(
                        stage_name=type(self._dependent).__name__,
                        frame_attribute=frame_attribute,
                    ),
                    cv2.resize(
                        getattr(self._dependent, frame_attribute).get_frame(),
                        self.im_size,
                    ),
                )

        return StageResult(True, True)
    def execute(self) -> StageResult:
        """
        Registers the history frame.
        """
        # Do nothing.
        if not self._history_frames.is_full():
            return StageResult(True, False)

        processed_frame = self._preprocessed_frame.processed_frame
        history_frames = self._history_frames.history_frames

        self.transformation_matrices = [
            self._register(f, processed_frame) for f in history_frames
        ]

        return StageResult(True, True)
Esempio n. 7
0
    def execute(self) -> StageResult:
        idx = next(self._progress)

        while idx < self._frame.frame.get_frame_number():
            idx = next(self._progress)

        return StageResult(True, True)
Esempio n. 8
0
    def execute(self) -> StageResult:
        self.baboons = [
            b for b in self._baboons.baboons
            if self._calc_area(b) >= self._min_size
        ]

        return StageResult(True, True)
Esempio n. 9
0
    def execute(self) -> StageResult:
        """
        Detect and returns locations of blobs from foreground mask
        Returns list of coordinates
        """

        foreground_mask = self._moving_foregrouned.moving_foreground.get_frame()

        contours, _ = cv2.findContours(
            foreground_mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE
        )
        rectangles = [cv2.boundingRect(c) for c in contours]
        rectangles = [(r[0], r[1], r[0] + r[2], r[1] + r[3]) for r in rectangles]

        blob_image = cv2.cvtColor(foreground_mask, cv2.COLOR_GRAY2BGR)
        for rect in rectangles:
            blob_image = cv2.rectangle(
                blob_image, (rect[0], rect[1]), (rect[2], rect[3]), (0, 255, 0), 2
            )

        self.blob_image = Frame(
            blob_image,
            self._moving_foregrouned.moving_foreground.get_frame_number(),
        )

        self.baboons = [Baboon(r) for r in rectangles]
        return StageResult(True, True)
Esempio n. 10
0
    def execute(self) -> StageResult:
        if self._result is None:
            self._result = np.zeros_like(
                self._moving_foreground.moving_foreground.get_frame())

            self._motion_observations = np.zeros_like(
                self._moving_foreground.moving_foreground.get_frame())

            self._no_motion_observations = np.zeros_like(
                self._moving_foreground.moving_foreground.get_frame())

        mask = self._moving_foreground.moving_foreground.get_frame() == 255
        not_mask = self._moving_foreground.moving_foreground.get_frame() == 0

        self._motion_observations[not_mask] = 0
        self._motion_observations[mask] += 1

        self._no_motion_observations[mask] = 0
        self._no_motion_observations[not_mask] += 1

        self._result[self._motion_observations ==
                     self._required_motion_observations] = 255

        self._result[self._no_motion_observations ==
                     self._required_motion_observations] = 0

        self.moving_foreground = Frame(
            self._result,
            self._moving_foreground.moving_foreground.get_frame_number())

        return StageResult(True, True)
    def execute(self) -> StageResult:
        moving_foreground = self._moving_foreground.moving_foreground.get_frame()

        element = cv2.getStructuringElement(
            cv2.MORPH_ELLIPSE, (self._erode_kernel_size, self._erode_kernel_size)
        )
        eroded = cv2.erode(moving_foreground, element)

        element = cv2.getStructuringElement(
            cv2.MORPH_ELLIPSE, (self._dilate_kernel_size, self._dilate_kernel_size)
        )
        opened_mask = cv2.dilate(eroded, element)

        combined_mask = np.zeros(opened_mask.shape).astype(np.uint8)
        combined_mask[opened_mask == moving_foreground] = 255
        combined_mask[moving_foreground == 0] = 0

        element = cv2.getStructuringElement(
            cv2.MORPH_ELLIPSE, (self._combine_kernel_size, self._combine_kernel_size)
        )
        dialated = cv2.dilate(combined_mask, element)
        self.moving_foreground = Frame(
            cv2.erode(dialated, element),
            self._moving_foreground.moving_foreground.get_frame_number(),
        )

        return StageResult(True, True)
Esempio n. 12
0
    def execute(self) -> StageResult:
        shifted_history_frames = self._shifted_history_frames.shifted_history_frames
        quantized_frames = self._quantized_frames.quantized_frames

        self.history_of_dissimilarity = self._get_history_of_dissimilarity(
            shifted_history_frames, quantized_frames)

        return StageResult(True, True)
Esempio n. 13
0
    def execute(self) -> StageResult:
        """
        Executes all stages in this pipeline in parallel.
        """

        for stage in self.stages:
            stage.before_execute()
            result = stage.execute()
            stage.after_execute()

            if result.continue_pipeline and not result.next_stage:
                break

            if not result.continue_pipeline:
                return StageResult(False, None)

        return StageResult(True, True)
Esempio n. 14
0
    def execute(self) -> StageResult:
        """Quantizes the shifted history frame."""
        self.quantized_frames = [
            self._quantize_frame(f)
            for f in self._shifted_history_frames.shifted_history_frames
        ]

        return StageResult(True, True)
Esempio n. 15
0
    def execute(self) -> StageResult:
        self.processed_frame = Frame(
            cv2.fastNlMeansDenoising(
                self._preprocessed_frame.processed_frame.get_frame(), h=5
            ),
            self._preprocessed_frame.processed_frame.get_frame_number(),
        )

        return StageResult(True, True)
Esempio n. 16
0
    def execute(self) -> StageResult:
        grouped_shifted_history_frames = (
            self._group_shifted_history_frames.grouped_shifted_history_frames)
        grouped_quantized_frames = (
            self._group_shifted_history_frames.grouped_quantized_frames)

        self.intersected_frames = self._intersect_all_frames(
            grouped_shifted_history_frames, grouped_quantized_frames)

        return StageResult(True, True)
    def execute(self) -> StageResult:
        """
        Converts a color image to a gray-scale image.
        """

        self.processed_frame = Frame(
            cv2.cvtColor(self._frame_mixin.frame.get_frame(),
                         cv2.COLOR_BGR2GRAY),
            self._frame_mixin.frame.get_frame_number(),
        )
        return StageResult(True, True)
Esempio n. 18
0
    def execute(self) -> StageResult:
        """
        Get a video frame from a video file.
        """

        success, frame = self._capture.read()

        self.frame = Frame(frame, self._frame_number)
        self._frame_number += 1

        return StageResult(success, success)
Esempio n. 19
0
    def execute(self) -> StageResult:
        # This cleans up the edges after performing image registration.
        for mask in self._shifted_masks.shifted_masks:
            self.moving_foreground = Frame(
                np.multiply(
                    self._moving_foreground.moving_foreground.get_frame(), mask
                ),
                self._frame.frame.get_frame_number(),
            )

        return StageResult(True, True)
Esempio n. 20
0
    def execute(self) -> StageResult:
        """
        Implements a storage of historical frame step for motion detection.
        """
        if self.is_full():
            frame = self.history_frames.popleft()
            self._history_frame_popped_subject.on_next(frame)

        self.history_frames.append(self._preprocessed_frame.processed_frame)

        return StageResult(True, True)
    def execute(self) -> StageResult:
        frame = self._preprocessed_frame.processed_frame
        union = self._unioned_frames.unioned_frames
        weights = self._weights.weights

        frame_new = self._zero_weights(frame.get_frame(), weights)
        union_new = self._zero_weights(union, weights)

        self.foreground = cv2.absdiff(frame_new, union_new)

        return StageResult(True, True)
Esempio n. 22
0
    def execute(self) -> StageResult:
        weights = self._weights.weights
        foreground = self._foreground.foreground
        history_of_dissimilarity = (
            self._history_of_dissimilarity.history_of_dissimilarity)

        self.moving_foreground = Frame(
            self._get_moving_foreground(weights, foreground,
                                        history_of_dissimilarity),
            self._frame.frame.get_frame_number(),
        )

        return StageResult(True, True)
Esempio n. 23
0
    def execute(self) -> StageResult:
        """
        Blurs a gray frame using a Gaussian blur.
        """

        self.processed_frame = Frame(
            cv2.GaussianBlur(
                self._preprocessed_frame.processed_frame.get_frame(), (5, 5),
                0),
            self._preprocessed_frame.processed_frame.get_frame_number(),
        )

        return StageResult(True, True)
    def execute(self) -> StageResult:
        transformation_matrices = self._transformation_matrices.transformation_matrices
        frame = self._frame.processed_frame

        img = np.ones(frame.get_frame().shape).astype(np.uint8)
        self.shifted_masks = [
            cv2.warpPerspective(
                img,
                M,
                (frame.get_frame().shape[1], frame.get_frame().shape[0]),
            ) for M in transformation_matrices
        ]

        return StageResult(True, True)
Esempio n. 25
0
    def execute(self) -> StageResult:
        shifted_history_frames = self._shifted_history_frames.shifted_history_frames
        quantized_frames = self._quantized_frames.quantized_frames

        frame_group_index = range(len(shifted_history_frames) - 1)
        frame_group_index = [(r, r + 1) for r in frame_group_index]

        self.grouped_shifted_history_frames = [(shifted_history_frames[g[0]],
                                                shifted_history_frames[g[1]])
                                               for g in frame_group_index]
        self.grouped_quantized_frames = [(quantized_frames[g[0]],
                                          quantized_frames[g[1]])
                                         for g in frame_group_index]

        return StageResult(True, True)
Esempio n. 26
0
    def execute(self) -> StageResult:
        for baboon in self._baboons.baboons:
            x1, y1, x2, y2 = baboon.rectangle

            self._file.write(str(x1))
            self._file.write(", ")
            self._file.write(str(y1))
            self._file.write(", ")
            self._file.write(str(x2))
            self._file.write(", ")
            self._file.write(str(y2))
            self._file.write(", ")
            self._file.write(str(self._frame.frame.get_frame_number()))
            self._file.write("\n")

        return StageResult(True, True)
Esempio n. 27
0
    def execute(self) -> StageResult:
        moving_foreground = np.zeros_like(
            self._moving_foreground.moving_foreground.get_frame())
        height, width = moving_foreground.shape

        _execute(
            moving_foreground,
            self._moving_foreground.moving_foreground.get_frame(),
            self._group_size,
            height,
            width,
        )

        self.moving_foreground = Frame(
            moving_foreground,
            self._moving_foreground.moving_foreground.get_frame_number(),
        )

        return StageResult(True, True)
Esempio n. 28
0
    def execute(self) -> StageResult:
        """
        Detect and returns locations of blobs from foreground mask
        Returns list of coordinates
        """

        foreground_mask = self._moving_foregrouned.moving_foreground.get_frame(
        )
        keypoints = self._detector.detect(foreground_mask)

        self.blob_image = Frame(
            cv2.drawKeypoints(
                cv2.cvtColor(foreground_mask, cv2.COLOR_GRAY2BGR),
                keypoints,
                np.array([]),
                (0, 255, 0),
                cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,
            ),
            self._moving_foregrouned.moving_foreground.get_frame_number(),
        )

        self.baboons = [Baboon((k.pt[0], k.pt[1]), k.size) for k in keypoints]
        return StageResult(True, True)
Esempio n. 29
0
    def execute(self) -> StageResult:
        region_frame = self._frame.frame.get_frame().copy()

        rectangles = [(b.rectangle, b.id_str) for b in self._baboons.baboons]
        for rect, id_str in rectangles:
            region_frame = cv2.rectangle(region_frame, (rect[0], rect[1]),
                                         (rect[2], rect[3]), (0, 255, 0), 2)

            if id_str is not None:
                cv2.putText(
                    region_frame,
                    id_str,
                    (rect[0], rect[1] - 10),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.9,
                    (36, 255, 12),
                    2,
                )

        self.region_frame = Frame(region_frame,
                                  self._frame.frame.get_frame_number())

        return StageResult(True, True)
    def execute(self) -> StageResult:
        quantized_frames = self._quantized_frames.quantized_frames

        self.weights = self._get_weights(quantized_frames)

        return StageResult(True, True)