def test_extract_lane_shape(self):
     data_in = numpy.clip(
         cv2.imread("Resources/Lane_Fork_Noise.png", cv2.IMREAD_GRAYSCALE),
         0, 1)
     contours = ImagePreprocessor.find_contours(data_in)
     data_out = ImagePreprocessor.extract_lane_shape(data_in, contours)
     expected = numpy.clip(
         cv2.imread("Resources/Lane_Fork_Clean.png", cv2.IMREAD_GRAYSCALE),
         0, 1)
     numpy.testing.assert_array_equal(data_out, expected)
Beispiel #2
0
    def process_frame(self, image):
        """
        Processes the frame captured by Cozmo's camera
        :param image: Current frame from Cozmo's feed
        :type image: PIL image
        """
        # Convert image to binary
        bin_img = ImagePreprocessor.pil_rgb_to_numpy_binary(image)

        # Find contours on image
        contours = ImagePreprocessor.find_contours(bin_img)

        # Extract lane shape and remove noise
        lane_img = ImagePreprocessor.extract_lane_shape(bin_img, contours)

        # Create image for later display
        display_img = cv2.cvtColor(lane_img * 255, cv2.COLOR_GRAY2BGR)

        # Counting signs and overwrite attribute in Lane Analyzer
        if RobotStatusController.enable_sign_recognition and \
                not Settings.disable_sign_detection and \
                not RobotStatusController.disable_autonomous_behavior:
            RobotStatusController.sign_count = ImagePreprocessor.calculate_number_of_signs(
                display_img, contours)
            self.sign_handler.react_to_signs(RobotStatusController.sign_count)

        lane_correction = 0
        if not RobotStatusController.disable_autonomous_behavior:
            # Calculate lane correction based on image data
            lane_correction = self.corr_calculator.calculate_lane_correction(
                lane_img)

            if not RobotStatusController.is_in_packet_station:
                crossing_type = CrossingTypeIdentifier.analyze_frame(lane_img)
                if crossing_type is not None:
                    Navigator.navigate()

        if not RobotStatusController.disable_autonomous_behavior:
            # If correction is required let Cozmo correct
            if lane_correction is not None:
                self.drive_controller.correct(lane_correction)

        # Update current frame
        self.current_cam_frame = display_img * 255

        # Show cam live preview if enabled
        if Settings.show_live_preview:
            # self.preview_utils.show_cam_frame(bin_img*255)
            self.preview_utils.show_cam_frame(display_img)