def _test_image(self, test_config, detection_mode): test_image = Image.from_file(self.BASE_PATH + test_config['filename']) local_world_map = LocalWorldMap() if 'gripper_mock_location' in test_config: local_world_map._gripper = Gripper(test_config['gripper_mock_location']) detection_results = self._local_world_detector.detect_objects(test_image, detection_mode, local_world_map) Logger.get_instance().log(self, "World Detection Test Results", log_entry_type = LogEntryType.INFORMATIONAL, details = "'{0}': {1}.".format(test_config['filename'], detection_results)) self._verify_results(test_config, detection_results)
def __init__(self, camera_arm_control_service): self._local_world_detector = LocalWorldDetector(camera_arm_control_service) self._local_world_map_drawing_service = LocalWorldMapDrawingService() self._on_detection_complete = EventHandler() self._detection_mode = LocalWorldMapDetectionMode.NONE self._local_world_map = LocalWorldMap() self._camera_arm_control_service = camera_arm_control_service self._detection_lock = Lock()
class LocalWorldMapService(object): DETECTION_MODE_SWITCH_WAIT_TIME = 1 @property def local_world_map(self): return self._local_world_map @property def detection_mode(self): return self._detection_mode @detection_mode.setter def detection_mode(self, value): self._switch_detection_mode(value) @property def on_detection_complete(self): return self._on_detection_complete @on_detection_complete.setter def on_detection_complete(self, value): self._on_detection_complete = value def __init__(self, camera_arm_control_service): self._local_world_detector = LocalWorldDetector(camera_arm_control_service) self._local_world_map_drawing_service = LocalWorldMapDrawingService() self._on_detection_complete = EventHandler() self._detection_mode = LocalWorldMapDetectionMode.NONE self._local_world_map = LocalWorldMap() self._camera_arm_control_service = camera_arm_control_service self._detection_lock = Lock() def detect_objects(self, image): self._detection_lock.acquire() try: self._execute_detection(image) finally: self._detection_lock.release() self._on_detection_complete.fire() def _execute_detection(self, image): local_world_descriptor = self._local_world_detector.detect_objects(image, self._detection_mode, self._local_world_map) if local_world_descriptor is not None: self._local_world_map.merge_local_world_descriptor(local_world_descriptor) def reset(self, local_world_object_types): self._local_world_map.reset(local_world_object_types) def draw_local_world_map(self): self._local_world_map_drawing_service.draw(self._local_world_map) return self._local_world_map.reference_image def wait_for_detection(self, local_world_object_types, timeout): start_time = datetime.datetime.now() while not self._local_world_map.contains(local_world_object_types): self._on_detection_complete.join(timeout) if (datetime.datetime.now() - start_time).total_seconds() > timeout: raise TimeoutError("Timeout was reached while waiting for the specified detection status.") return self._local_world_map def _switch_detection_mode(self, detection_mode): self._detection_mode = detection_mode self._detection_lock.acquire() if self._detection_mode == LocalWorldMapDetectionMode.TREASURE_PICKUP_SUCCESS_DETECTION: self._local_world_map.reset([LocalWorldObjectType.TREASURE]) elif self._detection_mode != LocalWorldMapDetectionMode.NONE: self._local_world_map.reset(LocalWorldObjectType.all()) self._detection_lock.release()
class TestLocalWorldMap(object): SAMPLE_PLAYFIELD = object() SAMPLE_GRIPPER = object() SAMPLE_TREASURES = [object(), object()] SAMPLE_REFERENCE_IMAGE = object() def setup(self): self._local_world_map = LocalWorldMap() self._local_world_descriptor = LocalWorldDescriptor(LocalWorldObjectType.all(), self.SAMPLE_REFERENCE_IMAGE, self.SAMPLE_PLAYFIELD, self.SAMPLE_GRIPPER, self.SAMPLE_TREASURES) def test_given_complete_local_world_descriptor_when_merging_local_world_descriptor_then_results_are_merged(self): self._local_world_map.merge_local_world_descriptor(self._local_world_descriptor) assert_is(self._local_world_map.playfield, self.SAMPLE_PLAYFIELD) assert_is(self._local_world_map.gripper, self.SAMPLE_GRIPPER) assert_equals(self._local_world_map.treasures, self.SAMPLE_TREASURES) def test_given_complete_local_world_descriptor_when_merging_local_world_descriptor_then_local_world_map_contains_the_descriptor_objects(self): self._local_world_map.merge_local_world_descriptor(self._local_world_descriptor) assert_true(self._local_world_map.contains(LocalWorldObjectType.PLAYFIELD)) assert_true(self._local_world_map.contains(LocalWorldObjectType.GRIPPER)) assert_true(self._local_world_map.contains(LocalWorldObjectType.TREASURE)) def test_given_partial_descriptor_when_merging_local_world_descriptor_then_last_objects_are_kept(self): self._local_world_map.merge_local_world_descriptor(self._local_world_descriptor) self._local_world_descriptor = LocalWorldDescriptor([], self.SAMPLE_REFERENCE_IMAGE) self._local_world_map.merge_local_world_descriptor(self._local_world_descriptor) assert_is(self._local_world_map.playfield, self.SAMPLE_PLAYFIELD) assert_is(self._local_world_map.gripper, self.SAMPLE_GRIPPER) assert_equals(self._local_world_map.treasures, self.SAMPLE_TREASURES) assert_is(self._local_world_map.reference_image, self.SAMPLE_REFERENCE_IMAGE) def test_given_non_empty_local_world_map_when_fully_resetting_local_world_map_then_local_world_map_is_cleared(self): self._local_world_map.merge_local_world_descriptor(self._local_world_descriptor) self._local_world_map.reset(LocalWorldObjectType.all()) assert_false(self._local_world_map.contains(LocalWorldObjectType.PLAYFIELD)) assert_false(self._local_world_map.contains(LocalWorldObjectType.GRIPPER)) assert_false(self._local_world_map.contains(LocalWorldObjectType.TREASURE)) def test_given_non_empty_local_world_map_when_partially_resetting_local_world_map_then_local_world_map_is_partially_cleared(self): self._local_world_map.merge_local_world_descriptor(self._local_world_descriptor) self._local_world_map.reset([LocalWorldObjectType.GRIPPER]) assert_true(self._local_world_map.contains(LocalWorldObjectType.PLAYFIELD)) assert_false(self._local_world_map.contains(LocalWorldObjectType.GRIPPER))
def setup(self): self._local_world_map = LocalWorldMap() self._local_world_descriptor = LocalWorldDescriptor(LocalWorldObjectType.all(), self.SAMPLE_REFERENCE_IMAGE, self.SAMPLE_PLAYFIELD, self.SAMPLE_GRIPPER, self.SAMPLE_TREASURES)