def format_mount_directory(mount_directory, dataset_name): path = mount_directory + "/" + dataset_name if os.path.isdir(path) == False: error_message = '{} is not a valid directory, please check given id and dataset parameter'.format( path) return Result.failure([error_message]) return Result.success(path)
def load_json_string(self, json_file_path): try: return Result.success(JsonHelper.load_json_string(json_file_path)) except ValueError: err_msg = 'Decoding json file contained in folder {} has failed, please check the format of json file'.format( json_file_path) return Result.failure([err_msg])
def check_contains_empty_name(self, dataset_name_list): for dataset_name in dataset_name_list: if dataset_name == "": err_msg = 'dataset_name_list {} can not contains empty string'.format( dataset_name_list) return Result.failure([err_msg]) return Result.success('')
def create_new_dataset(self, dataset_name): try: dataset = self.create_dataset(dataset_name) dataset.save() return Result.success(dataset.id) except: return Result.failure( ['Dataset name is duplicate: ' + dataset_name])
def delete_by_stripId(self, stripId): if self.find_datasets_by_stripID(stripId) == None: err_msg = 'Given stripId {} is invalid'.format(stripId) return Result.failure([err_msg]) for d in self.find_datasets_by_stripID(stripId): self.delete(d) return Result.success(stripId)
def check_duplicate(self, dataset_name_list): original_length = len(dataset_name_list) deduplicate_length = len(list(set(dataset_name_list))) if original_length != deduplicate_length: err_msg = 'dataset {} can not contain duplicate names'.format( dataset_name_list) return Result.failure([err_msg]) return Result.success('')
def labelme2coco(self, labelme_json_string, image_bound_size): try: coco_json = Labelme2CoCoConverter(image_bound_size).convert( labelme_json_string) coco_json_string = json.dumps(coco_json) return Result.success(coco_json_string) except: return Result.failure([ 'The point of shape in labelme.json is out of image bound size' ])
def create(): try: return Result.success(DefectCodeLoader()) except FileNotFoundError as e: return Result.failure( ['Please put label_map.json into root folder']) except ValueError as e: return Result.failure([ 'The format of given label_map.json is invalid, please check and fix the format of json file' ])
def check_shape(shapes): errors = [] for shape in shapes: LabelChecker.check_type(shape, errors) LabelChecker.check_point(shape, errors) LabelChecker.check_attribute(shape, errors) if len(errors) > 0: return Result.failure( LabelChecker.remove_duplicate_error_message(errors)) return Result.success('success')
def test_monadic_type(self): # failure result = self.create_customer_id(1) \ .flat_map(lambda id: self.create_email('1') \ .flat_map(lambda email: Result.success(self.create_customer(id, email)))) assert result.error_messages == ['email length must large than four'] # success result = self.create_customer_id(1) \ .flat_map(lambda id: self.create_email('1sdfsdfsffds') \ .flat_map(lambda email: Result.success(self.create_customer(id, email)))) assert result.value == {'id': 1, 'email': '1sdfsdfsffds'}
def import_json_to_all_dataset(self, dataset_id_list, dataset_source_folder_path): for dataset in DatasetModel.find_datasets_by_id_list(dataset_id_list): result = format_mount_directory(dataset_source_folder_path, dataset.name)\ .flat_map(lambda path: self.execute(dataset.id, path)) if result.is_failure(): return result return Result.success(dataset_id_list)
def check(labelme_document): labels = labelme_document['Labels'] if LabelChecker.is_empty(labels): return Result.success('') shapesAndLabel = labels[0] shapes = shapesAndLabel['Shapes'] return LabelChecker.check_shape(shapes)
def import_annotations_to(self, images, dataset_id, labelme_json_string): for image in images: result = self.import_annotation_to_image.execute( dataset_id, image.id, labelme_json_string) if result.is_failure(): return result return Result.success(dataset_id)
def check_labelme_json(self, dataset_id_list, dataset_source_folder_path): for dataset in DatasetModel.find_datasets_by_id_list(dataset_id_list): result = format_mount_directory(dataset_source_folder_path, dataset.name)\ .flat_map(lambda path: self.find_labelme_json(path)\ .flat_map(lambda labelme_json: LabelChecker.check_string(labelme_json))) if result.is_failure(): return result return Result.success('')
def check_mount_directory(self, stripID, dataset_name_list, mount_directory): for dataset_name in self.format_dataset_name_list( stripID, dataset_name_list): result = format_mount_directory(mount_directory, dataset_name) if result.is_failure(): return result return Result.success(dataset_name_list)
def add_categories_and_annotation(self, dataset_id, image_id, parseResult): category_data_list = parseResult.get_category_data_list() annotation_data_list = parseResult.get_annotation_data_list() category_id_map = self.add_categories_to_dataset_usecase.execute( category_data_list, dataset_id, image_id) annotation_list = self.add_annotations_to_image_usecase.execute( image_id, category_id_map, annotation_data_list) return Result.success(annotation_list)
def find_label_json_in_the(self, directory): if self.is_not_include_label_json(directory): err_msg = "Given directory {} must contains Label.json".format( directory) return Result.failure([err_msg]) json_file_path = self.find_json_file_path_in_the(directory) return self.load_json_string(json_file_path)
def create_all_dataset(self, dataset_name_list): dataset_id_list = [] for dataset_name in dataset_name_list: result = self.to_dataset_id(dataset_name) if result.is_success(): dataset_id_list.append(result.value) else: return result return Result.success(dataset_id_list)
def add_shared_folder_to_user(self, user, stripID, dataset_name_list, mount_directory): result = self.dataset_repository.delete_by_stripId(stripID) if result.is_failure(): return result user.add_strip_folder( stripID, self.format_dataset_name_list(stripID, dataset_name_list), mount_directory) return Result.success(user)
def test_flat_map_of_failure_result(self): result = Result.success(1)\ .flat_map(lambda num: Result.failure(['fail']))\ .flat_map(lambda num: Result.success(num + 1)) assert result.error_messages == ['fail']
def coco2cocoData(self, coco_json_string): return Result.success(CoCoDataParser().parse(coco_json_string))
def to_dataset_id(self, name): dataset = DatasetModel.find_by_name(name) if dataset: # exist return Result.success(dataset.id) else: return self.create_new_dataset(name)
def test_given_invalid_argument_to_failure_result(self): invalid_argument = 5 self.assert_exception(lambda _: Result.failure(invalid_argument), 'Failure object only accept list of string')
def test_flat_map_of_success_result(self): addOne = lambda num: Result.success(num + 1) result = Result.success(1) \ .flat_map(addOne) assert result.value == 2
def test_success_result(self): value = "success" result = Result.success(value) assert result.value == value
def create_customer_id(self, id): if id > 0: return Result.success(id) return Result.failure(['id must large than zero'])
def check_empty_stripID(self, stripID): if stripID == "": err_msg = 'stripID {} can not be an empty string'.format(stripID) return Result.failure([err_msg]) return Result.success('')
def test_failure_result(self): error_messages = ['err1', 'err2'] result = Result.failure(error_messages) assert result.error_messages == error_messages
def create_email(self, email): if len(email) < 4: return Result.failure(['email length must large than four']) return Result.success(email)