Skip to content

Commit

Permalink
new relabel function for Darknet (YOLO) annotation files
Browse files Browse the repository at this point in the history
  • Loading branch information
monocongo committed Feb 5, 2020
1 parent 8b8f389 commit 2e65134
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/cvdata/relabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@
_logger = logging.getLogger(__name__)


# ------------------------------------------------------------------------------
def relabel_darknet(
file_path: str,
old_index: int,
new_index: int,
):
"""
Replaces the label index values of a Darknet (YOLO) annotation file.
:param file_path: path of the Darknet (YOLO) file
:param old_index: label index value which if found will be replaced by the
new label index
:param new_index: new label index value
"""

# arguments validation
if (old_index < 0) or (new_index < 0):
raise ValueError("Invalid label index argument, must be equal or greater than zero")

# replace the label indices in-place
with fileinput.FileInput(file_path, inplace=True) as file_input:
for line in file_input:
line = line.rstrip("\r\n")
parts = line.split()
if (len(parts) > 0) and (parts[0] == str(old_index)):
parts[0] = str(new_index)
print(" ".join(parts))


# ------------------------------------------------------------------------------
def relabel_kitti(
file_path: str,
Expand All @@ -29,7 +58,7 @@ def relabel_kitti(
"""
Replaces the label values of a KITTI annotation file.
:param file_path: path of the KITTI file to have labels replaced
:param file_path: path of the KITTI file
:param old_label: label value which if found will be replaced by the new label
:param new_label: new label value
"""
Expand All @@ -53,7 +82,7 @@ def relabel_pascal(
"""
Replaces the label values of a PASCAL VOC annotation file.
:param file_path: path of the PASCAL VOC file to have labels replaced
:param file_path: path of the PASCAL VOC file
:param old_label: label value which if found will be replaced by the new label
:param new_label: new label value
"""
Expand Down Expand Up @@ -92,6 +121,21 @@ def _validate_args(
raise ValueError(f"File path argument {file_path} is not a valid file path")


# ------------------------------------------------------------------------------
def _relabel_darknet(arguments: Dict):
"""
Unpacks a dictionary of arguments and calls the function for replacing the
labels of a Darknet (YOLO) annotation file.
:param arguments: dictionary of function arguments, should include:
"file_path": path of the Darknet (YOLO) file to have labels renamed
"old": label index which if found will be renamed
"new": new label index value
"""

relabel_darknet(arguments["file_path"], arguments["old"], arguments["new"])


# ------------------------------------------------------------------------------
def _relabel_kitti(arguments: Dict):
"""
Expand Down Expand Up @@ -161,6 +205,9 @@ def main():
elif args["format"] == "pascal":
file_ext = ".xml"
relabel_function = _relabel_pascal
elif args["format"] == "darknet":
file_ext = ".txt"
relabel_function = _relabel_darknet
else:
raise ValueError("Only KITTI and PASCAL annotation files are supported")

Expand Down

0 comments on commit 2e65134

Please sign in to comment.