def test_get_instance_stream(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_manager.stop_all_instances() self.assertTrue( len(instance_manager.get_info()["active_instances"]) == 0, "There should be no running instances.") pipeline_id = "test_pipeline" pipeline_config = PipelineConfig( pipeline_id, parameters={"camera_name": "simulation"}) instance_manager.config_manager.save_pipeline_config( pipeline_id, pipeline_config.get_configuration()) instance_stream_1 = instance_manager.get_instance_stream(pipeline_id) self.assertTrue( instance_manager.get_info()["active_instances"][pipeline_id] ["read_only"], "Instance should be read only.") with self.assertRaisesRegex( ValueError, "Cannot set config on a read only instance."): instance_manager.get_instance(pipeline_id).set_parameter({}) instance_stream_2 = instance_manager.get_instance_stream(pipeline_id) self.assertEqual(instance_stream_1, instance_stream_2, "Only one instance should be present.") instance_manager.stop_all_instances()
def test_get_invalid_instance_stream(self): instance_manager = get_test_pipeline_manager_with_real_cam() with self.assertRaisesRegex( ValueError, "is not present on server and it is not a saved"): instance_manager.get_instance_stream("simulation_sp1") instance_manager.stop_all_instances()
def test_camera_offline(self): instance_manager = get_test_pipeline_manager_with_real_cam() with self.assertRaisesRegex( ValueError, "Camera camera_example_1 is not online. Cannot start pipeline." ): instance_manager.create_pipeline( configuration={"camera_name": "camera_example_1"})
def test_collect_background(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_manager.background_manager = BackgroundImageManager( self.background_folder) pipeline_id = "test_pipeline" number_of_images = 10 pipeline_config = PipelineConfig( pipeline_id, parameters={"camera_name": "simulation"}) instance_manager.config_manager.save_pipeline_config( pipeline_id, pipeline_config.get_configuration()) pipeline_stream_address = instance_manager.get_instance_stream( pipeline_id) pipeline_host, pipeline_port = get_host_port_from_stream_address( pipeline_stream_address) # Collect from the pipeline. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: data = stream.receive() self.assertIsNotNone(data, "This should really not happen anymore.") camera_name = instance_manager.get_instance( pipeline_id).get_info()["camera_name"] background_id = instance_manager.collect_background( camera_name, number_of_images) self.assertTrue(background_id.startswith("simulation"), "Background id not as expected.") host, port = get_host_port_from_stream_address( instance_manager.cam_server_client.get_instance_stream( "simulation")) # Collect from the camera. with source(host=host, port=port, mode=SUB) as stream: data = stream.receive() self.assertIsNotNone(data, "This should really not happen anymore.") self.assertEqual( instance_manager.background_manager.get_background( background_id).shape, data.data.data["image"].value.shape, "Background and image have to be of the same shape.") self.assertEqual( instance_manager.background_manager.get_background( background_id).dtype, data.data.data["image"].value.dtype, "Background and image have to be of the same dtype.") instance_manager.stop_all_instances()
def test_statistics(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_id_0, _ = instance_manager.create_pipeline( configuration={"camera_name": "simulation"}) latest_statistics = instance_manager.get_instance( instance_id_0).get_statistics() for stat in "total_bytes", "clients", "throughput", "time", "rx", "tx", "pid", "cpu", "memory": self.assertTrue(stat in latest_statistics) instance_manager.stop_all_instances()
def test_statistics(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_id_0, _ = instance_manager.create_pipeline( configuration={"camera_name": "simulation"}) latest_statistics = instance_manager.get_instance( instance_id_0).get_statistics() self.assertTrue("n_clients" in latest_statistics) self.assertTrue("input_throughput" in latest_statistics) instance_manager.stop_all_instances()
def test_last_start_time(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_id_0, _ = instance_manager.create_pipeline( configuration={"camera_name": "simulation"}) pipeline_info = instance_manager.get_instance(instance_id_0).get_info() self.assertTrue("last_start_time" in pipeline_info) last_time = pipeline_info["last_start_time"] new_last_time = instance_manager.get_instance( instance_id_0).get_info()["last_start_time"] self.assertEqual( last_time, new_last_time, "The instance was still running, the times should be the same.") instance_manager.stop_all_instances()
def test_create_pipeline_instance(self): instance_manager = get_test_pipeline_manager_with_real_cam() pipeline_config = {"camera_name": "simulation"} instance_manager.config_manager.save_pipeline_config( "test_pipeline", pipeline_config) pipeline_id_1, stream_address_1 = instance_manager.create_pipeline( "test_pipeline") pipeline_id_2, stream_address_2 = instance_manager.create_pipeline( "test_pipeline") self.assertNotEqual(pipeline_id_1, pipeline_id_2, "The pipeline ids should be different.") self.assertNotEqual(stream_address_1, stream_address_2, "The pipeline streams should be different.") self.assertFalse( instance_manager.get_info()["active_instances"][pipeline_id_1] ["read_only"], "Instance should not be read only.") self.assertFalse( instance_manager.get_info()["active_instances"][pipeline_id_2] ["read_only"], "Instance should not be read only.") pipeline_id_3, stream_address_3 = instance_manager.create_pipeline( configuration=pipeline_config) self.assertNotEqual(pipeline_id_2, pipeline_id_3, "The pipeline ids should be different.") self.assertNotEqual(stream_address_2, stream_address_3, "The pipeline streams should be different.") self.assertFalse( instance_manager.get_info()["active_instances"][pipeline_id_3] ["read_only"], "Instance should not be read only.") with self.assertRaisesRegex( ValueError, "You must specify either the pipeline name or the " "configuration for the pipeline."): instance_manager.create_pipeline(pipeline_name="test_pipeline", configuration=pipeline_config) instance_manager.stop_all_instances()
def test_get_instance_from_config(self): instance_manager = get_test_pipeline_manager_with_real_cam() configuration_1 = { "camera_name": "simulation", "pipeline_type": "store", "stream_port": 10000, } instance_id_1, instance_stream_1 = instance_manager.get_instance_stream_from_config( configuration_1) instance_id_2, instance_stream_2 = instance_manager.get_instance_stream_from_config( configuration_1) self.assertEqual(instance_id_1, instance_id_2) self.assertEqual(instance_stream_1, instance_stream_2) self.assertTrue( instance_manager.get_instance(instance_id_1).is_read_only_config()) configuration_2 = { "camera_name": "simulation", "pipeline_type": "processing" } instance_id_3, instance_stream_3 = instance_manager.get_instance_stream_from_config( configuration_2) self.assertTrue( instance_manager.get_instance(instance_id_3).is_read_only_config()) self.assertNotEqual(instance_id_1, instance_id_3) self.assertNotEqual(instance_stream_1, instance_stream_3) instance_id_4, instance_stream_4 = instance_manager.create_pipeline( configuration=configuration_1) instance_id_5, instance_stream_5 = instance_manager.get_instance_stream_from_config( configuration_1) self.assertFalse( instance_manager.get_instance(instance_id_4).is_read_only_config()) self.assertTrue( instance_manager.get_instance(instance_id_5).is_read_only_config()) self.assertNotEqual( instance_id_4, instance_id_5, "Only read only instances can be returned by get_instance_stream_from_config." ) self.assertNotEqual( instance_stream_4, instance_stream_5, "Only read only instances can be returned by get_instance_stream_from_config." ) instance_manager.stop_all_instances() old_timeout = config.MFLOW_NO_CLIENTS_TIMEOUT config.MFLOW_NO_CLIENTS_TIMEOUT = 1 instance_id_6, instance_stream_6 = instance_manager.get_instance_stream_from_config( configuration_1) # Lets be sure that the instance goes down. sleep(2) self.assertFalse( instance_id_6 in instance_manager.get_info()["active_instances"], "Instance not stopped yet.") instance_id_7, instance_stream_7 = instance_manager.get_instance_stream_from_config( configuration_1) self.assertNotEqual(instance_id_6, instance_id_7, "A new instance id should be assigned here.") config.MFLOW_NO_CLIENTS_TIMEOUT = old_timeout instance_manager.stop_all_instances()
def test_update_instance_config_with_running(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_manager.background_manager = BackgroundImageManager( self.background_folder) pipeline_config = {"camera_name": "simulation"} x_size, y_size = get_simulated_camera().get_geometry() black_image = numpy.zeros(shape=(y_size, x_size)).astype(dtype="uint16") pipeline_id, pipeline_stream_address = instance_manager.create_pipeline( configuration=pipeline_config) pipeline_host, pipeline_port = get_host_port_from_stream_address( pipeline_stream_address) # Collect from the pipeline. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: normal_image_data = stream.receive() self.assertIsNotNone(normal_image_data, "This should really not happen anymore.") self.assertFalse( numpy.array_equal(normal_image_data.data.data['image'].value, black_image), "There should be a non black image.") self.assertEqual(normal_image_data.data.data["width"].value, x_size, "Incorrect width.") self.assertEqual(normal_image_data.data.data["height"].value, y_size, "Incorrect height.") white_image = numpy.zeros(shape=(y_size, x_size)) white_image.fill(65535) instance_manager.background_manager.save_background( "white_background", white_image, append_timestamp=False) instance_manager.update_instance_config( pipeline_id, {"image_background": "white_background"}) # Give it some time to load the background. sleep(0.5) # Collect from the camera. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: image_data = stream.receive() self.assertIsNotNone(image_data, "This should really not happen anymore.") # Because we didn't set the "image_background_enable" yet. self.assertFalse( numpy.array_equal(image_data.data.data["image"].value, black_image), "Background should not be yet applied.") instance_manager.update_instance_config(pipeline_id, { "image_background_enable": True, "image_threshold": 0 }) # Give it some time to load the background. sleep(1.0) # Collect from the camera. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: black_image_data = stream.receive() print(pipeline_host, pipeline_port) self.assertIsNotNone(black_image_data, "This should really not happen anymore.") print("---") print(black_image_data.data.data["image"].value.shape, black_image_data.data.data["image"].value.dtype) print(black_image.shape, black_image.dtype) black_image.astype(dtype="uint16") for i in range(len(black_image)): for j in range(len(black_image[0])): if black_image_data.data.data["image"].value[i][ j] != black_image[i][j]: print(i, j, black_image_data.data.data["image"].value[i][j], black_image[i][j]) self.assertTrue( numpy.array_equal(black_image_data.data.data["image"].value, black_image), "Now background should work.") instance_manager.stop_all_instances()
def test_update_instance_config_with_running(self): instance_manager = get_test_pipeline_manager_with_real_cam() instance_manager.background_manager = BackgroundImageManager( self.background_folder) pipeline_config = {"camera_name": "simulation"} black_image = numpy.zeros(shape=(960, 1280)) pipeline_id, pipeline_stream_address = instance_manager.create_pipeline( configuration=pipeline_config) pipeline_host, pipeline_port = get_host_port_from_stream_address( pipeline_stream_address) # Collect from the pipeline. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: normal_image_data = stream.receive() self.assertIsNotNone(normal_image_data, "This should really not happen anymore.") self.assertFalse( numpy.array_equal(normal_image_data.data.data['image'].value, black_image), "There should be a non black image.") self.assertEqual(normal_image_data.data.data["width"].value, 1280, "Incorrect width.") self.assertEqual(normal_image_data.data.data["height"].value, 960, "Incorrect height.") white_image = numpy.zeros(shape=(960, 1280)) white_image.fill(99999) instance_manager.background_manager.save_background( "white_background", white_image, append_timestamp=False) instance_manager.update_instance_config( pipeline_id, {"image_background": "white_background"}) # Give it some time to load the background. sleep(0.5) # Collect from the camera. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: image_data = stream.receive() self.assertIsNotNone(image_data, "This should really not happen anymore.") # Because we didn't set the "image_background_enable" yet. self.assertFalse( numpy.array_equal(image_data.data.data["image"].value, black_image), "Background should not be yet applied.") instance_manager.update_instance_config(pipeline_id, { "image_background_enable": True, "image_threshold": 0 }) # Give it some time to load the background. sleep(0.5) # Collect from the camera. with source(host=pipeline_host, port=pipeline_port, mode=SUB) as stream: black_image_data = stream.receive() self.assertIsNotNone(black_image_data, "This should really not happen anymore.") self.assertTrue( numpy.array_equal(black_image_data.data.data["image"].value, black_image), "Now background should work.") instance_manager.stop_all_instances()