diff --git a/tensorbay/opendataset/CADC/loader.py b/tensorbay/opendataset/CADC/loader.py index 3df76321f..ec88eea55 100644 --- a/tensorbay/opendataset/CADC/loader.py +++ b/tensorbay/opendataset/CADC/loader.py @@ -123,7 +123,7 @@ def _load_frame( # The data file name is a string of length 10 with each digit being a number: # 0000000000.jpg # 0000000001.bin - data_file_name = f"{frame_index:010}" + stem = f"{frame_index:010}" # Each line of the timestamps file looks like: # 2018-03-06 15:02:33.000000000 @@ -134,7 +134,7 @@ def _load_frame( # The image folder corresponds to different cameras, whose name is likes "CAM00". # The image folder looks like "image_00". camera_folder = f"image_{sensor_name[-2:]}" - image_file = f"{data_file_name}.png" + image_file = f"{stem}.png" data = Data( os.path.join(data_path, camera_folder, "data", image_file), @@ -143,7 +143,7 @@ def _load_frame( ) else: data = Data( - os.path.join(data_path, "lidar_points", "data", f"{data_file_name}.bin"), + os.path.join(data_path, "lidar_points", "data", f"{stem}.bin"), timestamp=timestamp, ) data.label.box3d = _load_labels(annotation["cuboids"]) diff --git a/tensorbay/opendataset/CIHP/loader.py b/tensorbay/opendataset/CIHP/loader.py index f7d7bb245..ca733db4e 100644 --- a/tensorbay/opendataset/CIHP/loader.py +++ b/tensorbay/opendataset/CIHP/loader.py @@ -71,16 +71,16 @@ def CIHP(path: str) -> Dataset: os.path.join(segment_abspath, f"{segment_name}_id.txt"), "r", encoding="utf-8" ) as fp: if segment_name == "test": - for filename in fp: - segment.append(Data(os.path.join(image_path, f"{filename.rstrip()}.jpg"))) + for stem in fp: + segment.append(Data(os.path.join(image_path, f"{stem.rstrip()}.jpg"))) else: category_path = os.path.join(segment_abspath, "Category_ids") instance_path = os.path.join(segment_abspath, "Instance_ids") - for filename in fp: - filename = filename.rstrip() - data = Data(os.path.join(image_path, f"{filename}.jpg")) + for stem in fp: + stem = stem.rstrip() + data = Data(os.path.join(image_path, f"{stem}.jpg")) label = data.label - png_filename = f"{filename}.png" + png_filename = f"{stem}.png" label.semantic_mask = SemanticMask(os.path.join(category_path, png_filename)) label.instance_mask = InstanceMask(os.path.join(instance_path, png_filename)) segment.append(data) diff --git a/tensorbay/opendataset/LIP/loader.py b/tensorbay/opendataset/LIP/loader.py index 45d8f1fb3..8aac4d177 100644 --- a/tensorbay/opendataset/LIP/loader.py +++ b/tensorbay/opendataset/LIP/loader.py @@ -76,8 +76,8 @@ def LIP(path: str) -> Dataset: if segment_name == "test": image_path = os.path.join(test_path, "testing_images") with open(os.path.join(test_path, "test_id.txt"), "r", encoding="utf-8") as fp: - for filename in fp: - segment.append(Data(os.path.join(image_path, f"{filename.rstrip()}.jpg"))) + for stem in fp: + segment.append(Data(os.path.join(image_path, f"{stem.rstrip()}.jpg"))) else: image_path = os.path.join(trainval_image_path, f"{segment_name}_images") parsing_path = os.path.join(trainval_parsing_path, f"{segment_name}_segmentations") @@ -90,10 +90,10 @@ def LIP(path: str) -> Dataset: def _get_data(keypoints_info: List[str], image_path: str, parsing_path: str) -> Data: - filename = os.path.splitext(keypoints_info[0])[0] - data = Data(os.path.join(image_path, f"{filename}.jpg")) + stem = os.path.splitext(keypoints_info[0])[0] + data = Data(os.path.join(image_path, f"{stem}.jpg")) label = data.label - label.semantic_mask = SemanticMask(os.path.join(parsing_path, f"{filename}.png")) + label.semantic_mask = SemanticMask(os.path.join(parsing_path, f"{stem}.png")) keypoints = LabeledKeypoints2D() for x, y, v in chunked(islice(keypoints_info, 1, None), 3): keypoints.append( diff --git a/tensorbay/opendataset/OxfordIIITPet/loader.py b/tensorbay/opendataset/OxfordIIITPet/loader.py index 7619e2ec4..15d505d21 100644 --- a/tensorbay/opendataset/OxfordIIITPet/loader.py +++ b/tensorbay/opendataset/OxfordIIITPet/loader.py @@ -59,17 +59,14 @@ def OxfordIIITPet(path: str) -> Dataset: test_segment = dataset.create_segment("test") annotation_path = os.path.join(root_path, "annotations") for image_path in glob(os.path.join(root_path, "images", "*.jpg")): - image_name = os.path.splitext(os.path.basename(image_path))[0] - name = "Cat" if image_name.istitle() else "Dog" - category, num = image_name.rsplit("_", 1) - + stem = os.path.splitext(os.path.basename(image_path))[0] + name = "Cat" if stem.istitle() else "Dog" + category, num = stem.rsplit("_", 1) data = Data(image_path, target_remote_path=f"{category}_{num.zfill(3)}.jpg") label = data.label label.classification = Classification(category=f"{name}.{category}") - label.semantic_mask = SemanticMask( - os.path.join(annotation_path, "trimaps", f"{image_name}.png") - ) - xml_path = os.path.join(annotation_path, "xmls", f"{image_name}.xml") + label.semantic_mask = SemanticMask(os.path.join(annotation_path, "trimaps", f"{stem}.png")) + xml_path = os.path.join(annotation_path, "xmls", f"{stem}.xml") if os.path.exists(xml_path): label.box2d = _get_box_label(xml_path) trainval_segment.append(data) diff --git a/tensorbay/opendataset/VOC2012ActionClassification/loader.py b/tensorbay/opendataset/VOC2012ActionClassification/loader.py index 1095623bd..86eae423c 100644 --- a/tensorbay/opendataset/VOC2012ActionClassification/loader.py +++ b/tensorbay/opendataset/VOC2012ActionClassification/loader.py @@ -55,24 +55,23 @@ def VOC2012ActionClassification(path: str) -> Dataset: for segment_name in _SEGMENT_NAMES: segment = dataset.create_segment(segment_name) with open(os.path.join(action_path, f"{segment_name}.txt"), encoding="utf-8") as fp: - for filename in fp: - filename = filename.strip() - segment.append(_get_data(filename, image_path, annotation_path)) + for stem in fp: + stem = stem.strip() + segment.append(_get_data(stem, image_path, annotation_path)) return dataset -def _get_data(filename: str, image_path: str, annotation_path: str) -> Data: +def _get_data(stem: str, image_path: str, annotation_path: str) -> Data: try: import xmltodict # pylint: disable=import-outside-toplevel except ModuleNotFoundError as error: raise ModuleImportError(module_name=error.name) from error - - data = Data(os.path.join(image_path, f"{filename}.jpg")) + data = Data(os.path.join(image_path, f"{stem}.jpg")) box2d = [] - with open(os.path.join(annotation_path, f"{filename}.xml"), "r", encoding="utf-8") as fp: + with open(os.path.join(annotation_path, f"{stem}.xml"), "r", encoding="utf-8") as fp: labels: Any = xmltodict.parse(fp.read()) - objects = labels["annotation"]["object"] + if not isinstance(objects, list): objects = [objects] for item in objects: diff --git a/tensorbay/opendataset/VOC2012Detection/loader.py b/tensorbay/opendataset/VOC2012Detection/loader.py index d9748239f..a490082d3 100644 --- a/tensorbay/opendataset/VOC2012Detection/loader.py +++ b/tensorbay/opendataset/VOC2012Detection/loader.py @@ -60,16 +60,16 @@ def VOC2012Detection(path: str) -> Dataset: for segment_name in _SEGMENT_NAMES: segment = dataset.create_segment(segment_name) with open(os.path.join(main_path, f"{segment_name}.txt"), encoding="utf-8") as fp: - for filename in fp: - segment.append(_get_data(filename.rstrip(), image_path, annotation_path)) + for stem in fp: + segment.append(_get_data(stem.rstrip(), image_path, annotation_path)) return dataset -def _get_data(filename: str, image_path: str, annotation_path: str) -> Data: +def _get_data(stem: str, image_path: str, annotation_path: str) -> Data: """Get all information of the datum corresponding to filename. Arguments: - filename: The filename of the data. + stem: The stem of the data. image_path: The path of the image directory. annotation_path: The path of the annotation directory. @@ -77,12 +77,12 @@ def _get_data(filename: str, image_path: str, annotation_path: str) -> Data: Data: class: `~tensorbay.dataset.data.Data` instance. """ - data = Data(os.path.join(image_path, f"{filename}.jpg")) + data = Data(os.path.join(image_path, f"{stem}.jpg")) box2d = [] - with open(os.path.join(annotation_path, f"{filename}.xml"), "r", encoding="utf-8") as fp: + with open(os.path.join(annotation_path, f"{stem}.xml"), "r", encoding="utf-8") as fp: labels: Any = xmltodict.parse(fp.read()) - objects = labels["annotation"]["object"] + if not isinstance(objects, list): objects = [objects] for obj in objects: