Пример #1
0
    def test_wrong_background_size(self):
        pipeline_parameters = {
            "camera_name": "simulation",
            "image_background": "white_background"
        }

        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        image = simulated_camera.get_image()
        x_axis, y_axis = simulated_camera.get_x_y_axis()

        background_provider = MockBackgroundManager()

        # Invalid background size.
        background_provider.save_background("white_background", numpy.zeros(shape=(100, 100)),
                                            append_timestamp=False)

        parameters = PipelineConfig("test_pipeline", pipeline_parameters).get_configuration()
        image_background_array = background_provider.get_background("white_background")

        with self.assertRaisesRegex(RuntimeError, "Invalid background_image size "):
            process_image(image=image,
                          timestamp=time.time(),
                          x_axis=x_axis,
                          y_axis=y_axis,
                          parameters=parameters,
                          image_background_array=image_background_array)
Пример #2
0
    def test_pipeline_background_manager(self):
        manager = multiprocessing.Manager()
        stop_event = multiprocessing.Event()
        statistics = manager.Namespace()
        parameter_queue = multiprocessing.Queue()

        pipeline_config = PipelineConfig("test_pipeline",
                                         parameters={
                                             "camera_name": "simulation",
                                             "image_background":
                                             "full_background",
                                             "image_background_enable": True,
                                             "image_threshold": 0
                                         })

        background_manager = MockBackgroundManager()

        with self.assertRaises(Exception):
            processing_pipeline(stop_event, statistics, parameter_queue,
                                self.client, pipeline_config, 12000,
                                background_manager)

        simulated_camera_shape = (960, 1280)

        background_array = numpy.zeros(shape=simulated_camera_shape)
        background_array.fill(99999)
        background_manager.save_background("full_background",
                                           background_array,
                                           append_timestamp=False)

        def send():
            processing_pipeline(stop_event, statistics, parameter_queue,
                                self.client, pipeline_config, 12000,
                                background_manager)

        thread = Thread(target=send)
        thread.start()

        with source(host="127.0.0.1", port=12000, mode=SUB) as stream:
            data = stream.receive()

            self.assertIsNotNone(data, "Received None message.")
            self.assertTrue(
                numpy.array_equal(data.data.data["image"].value,
                                  numpy.zeros(shape=simulated_camera_shape)))

        stop_event.set()
        thread.join()
Пример #3
0
    def test_image_background(self):
        pipeline_parameters = {
            "camera_name": "simulation",
            "image_background": "white_background"
        }

        simulated_camera = CameraSimulation(CameraConfig("simulation"))
        image = simulated_camera.get_image()
        x_axis, y_axis = simulated_camera.get_x_y_axis()

        background_provider = MockBackgroundManager()
        x_size, y_size = simulated_camera.get_geometry()
        background_provider.save_background("white_background", numpy.zeros(shape=(y_size, x_size)),
                                            append_timestamp=False)

        pipeline_config = PipelineConfig("test_pipeline", pipeline_parameters)
        parameters = pipeline_config.get_configuration()
        image_background_array = background_provider.get_background(parameters.get("image_background"))

        result = process_image(image=image,
                               timestamp=time.time(),
                               x_axis=x_axis,
                               y_axis=y_axis,
                               parameters=parameters,
                               image_background_array=image_background_array)

        self.assertTrue(numpy.array_equal(result["image"], image),
                        "A zero background should not change the image.")

        max_value_in_image = result["max_value"]

        pipeline_parameters = {
            "camera_name": "simulation",
            "image_background": "max_background",
            "image_threshold": 0
        }

        max_background = numpy.zeros(shape=(y_size, x_size), dtype="uint16")
        max_background.fill(max_value_in_image)
        background_provider.save_background("max_background", max_background, append_timestamp=False)

        pipeline_config = PipelineConfig("test_pipeline", pipeline_parameters)
        parameters = pipeline_config.get_configuration()
        image_background_array = background_provider.get_background(parameters.get("image_background"))

        expected_image = numpy.zeros(shape=(y_size, x_size))

        result = process_image(image=image,
                               timestamp=time.time(),
                               x_axis=x_axis,
                               y_axis=y_axis,
                               parameters=parameters,
                               image_background_array=image_background_array)

        self.assertTrue(numpy.array_equal(result["image"], expected_image),
                        "The image should be all zeros - negative numbers are not allowed.")
    def test_custom_hostname(self):
        config_manager = PipelineConfigManager(
            config_provider=MockConfigStorage())
        pipeline_instance_manager = PipelineInstanceManager(
            config_manager,
            MockBackgroundManager(),
            CamClient("http://0.0.0.0:8888"),
            hostname="custom_cam_hostname")

        _, stream_address = pipeline_instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertTrue(stream_address.startswith("tcp://custom_cam_hostname"))

        pipeline_instance_manager.stop_all_instances()
    def test_system_exit(self):
        manager = multiprocessing.Manager()
        stop_event = multiprocessing.Event()
        statistics = manager.Namespace()
        parameter_queue = multiprocessing.Queue()
        pipeline_config = PipelineConfig("test_pipeline",
                                         parameters={
                                             "camera_name": "simulation",
                                             "image_background":
                                             "full_background",
                                             "image_background_enable": True,
                                             "image_threshold": 0
                                         })

        background_manager = MockBackgroundManager()

        with self.assertRaises(SystemExit):
            processing_pipeline(stop_event, statistics, parameter_queue,
                                self.client, pipeline_config, 12001,
                                background_manager)
Пример #6
0
    def test_out_of_ports(self):
        config_manager = PipelineConfigManager(
            config_provider=MockConfigStorage())
        instance_manager = PipelineInstanceManager(config_manager,
                                                   MockBackgroundManager(),
                                                   None,
                                                   MockCamServerClient(),
                                                   port_range=(12000, 12003))

        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(
            instance_manager.get_instance(instance_id_0).get_stream_port(),
            12000)
        self.assertEqual(
            instance_manager.get_instance(instance_id_1).get_stream_port(),
            12001)
        self.assertEqual(
            instance_manager.get_instance(instance_id_2).get_stream_port(),
            12002)

        with self.assertRaisesRegex(
                Exception,
                "All ports are used. Stop some instances before opening a new stream."
        ):
            instance_manager.create_pipeline(
                configuration={"camera_name": "simulation"})

        instance_manager.stop_instance(instance_id_1)

        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(
            instance_manager.get_instance(instance_id_1).get_stream_port(),
            12001,
            "Instance_id_1 should have freeded the port 10001, but some other port was assigned."
        )

        instance_manager.stop_all_instances()

        # Check if the port rotation works as expected.
        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(
            instance_manager.get_instance(instance_id_2).get_stream_port(),
            12002)
        self.assertEqual(
            instance_manager.get_instance(instance_id_0).get_stream_port(),
            12000)
        self.assertEqual(
            instance_manager.get_instance(instance_id_1).get_stream_port(),
            12001)

        instance_manager.stop_all_instances()

        old_timeout = config.MFLOW_NO_CLIENTS_TIMEOUT
        config.MFLOW_NO_CLIENTS_TIMEOUT = 1

        # Test the cleanup procedure.
        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(len(instance_manager.get_info()["active_instances"]),
                         3, "All 3 instances should be running")

        with self.assertRaisesRegex(
                Exception,
                "All ports are used. Stop some instances before opening a new stream."
        ):
            instance_manager.create_pipeline(
                configuration={"camera_name": "simulation"})

        # Wait for the instances to die.
        sleep(5)

        self.assertEqual(len(instance_manager.get_info()["active_instances"]),
                         0, "All instances should be dead by now.")

        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        # Restore previous state.
        config.MFLOW_NO_CLIENTS_TIMEOUT = old_timeout
        instance_manager.stop_all_instances()
 def send():
     processing_pipeline(stop_event, statistics, parameter_queue,
                         self.client, pipeline_config, 12000,
                         MockBackgroundManager())
    def test_rotate_camera(self):
        manager = multiprocessing.Manager()
        stop_event = multiprocessing.Event()
        statistics = manager.Namespace()
        parameter_queue = multiprocessing.Queue()

        self.client.set_camera_config(
            "simulation_temp", self.client.get_camera_config("simulation"))

        pipeline_config = PipelineConfig(
            "test_pipeline", parameters={"camera_name": "simulation_temp"})

        background_manager = MockBackgroundManager()

        x_size, y_size = get_simulated_camera().get_geometry()
        simulated_camera_shape = (y_size, x_size)

        def send():
            processing_pipeline(stop_event, statistics, parameter_queue,
                                self.client, pipeline_config, 12002,
                                background_manager)

        thread = Thread(target=send)
        thread.start()

        with source(host="127.0.0.1",
                    port=12002,
                    mode=SUB,
                    receive_timeout=3000) as stream:
            data = stream.receive()
            self.assertIsNotNone(data, "Received None message.")

            x_size = data.data.data["width"].value
            y_size = data.data.data["height"].value
            x_axis = data.data.data["x_axis"].value
            y_axis = data.data.data["y_axis"].value
            image_shape = data.data.data["image"].value.shape

            self.assertEqual(x_size, simulated_camera_shape[1])
            self.assertEqual(y_size, simulated_camera_shape[0])

            # Sanity checks.
            self.assertEqual(x_size, len(x_axis))
            self.assertEqual(y_size, len(y_axis))
            self.assertEqual(image_shape, (y_size, x_size))

            # Rotate the image by 90 degree.
            camera_config = self.client.get_camera_config("simulation_temp")
            camera_config["rotate"] = 1
            self.client.set_camera_config("simulation_temp", camera_config)

            # Make a few frames pass.
            for _ in range(5):
                data = stream.receive()

            self.assertIsNotNone(data, "Received None message.")

            x_size = data.data.data["width"].value
            y_size = data.data.data["height"].value
            x_axis = data.data.data["x_axis"].value
            y_axis = data.data.data["y_axis"].value
            image_shape = data.data.data["image"].value.shape

            # X and Y size should be inverted.
            self.assertEqual(x_size, simulated_camera_shape[0])
            self.assertEqual(y_size, simulated_camera_shape[1])

            # Sanity checks.
            self.assertEqual(x_size, len(x_axis))
            self.assertEqual(y_size, len(y_axis))
            self.assertEqual(image_shape, (y_size, x_size))

        self.client.delete_camera_config("simulation_temp")

        stop_event.set()
        thread.join()