Skip to content

Commit

Permalink
style(opendataset): use "stem" to represent files without suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
willandfree committed Dec 6, 2021
1 parent 670a3e2 commit 01af623
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
6 changes: 3 additions & 3 deletions tensorbay/opendataset/CADC/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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"])
Expand Down
12 changes: 6 additions & 6 deletions tensorbay/opendataset/CIHP/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions tensorbay/opendataset/LIP/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand Down
13 changes: 5 additions & 8 deletions tensorbay/opendataset/OxfordIIITPet/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions tensorbay/opendataset/VOC2012ActionClassification/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions tensorbay/opendataset/VOC2012Detection/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ 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.
Returns:
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:
Expand Down

0 comments on commit 01af623

Please sign in to comment.