From 6d11120b8bdaf0ef025ba8db0bb3bf3bb7b69178 Mon Sep 17 00:00:00 2001 From: James Adams Date: Wed, 5 Feb 2020 15:51:32 -0500 Subject: [PATCH] moved the darknet_indices_to_labels function into the common module since it will be used by multiple modules #139 --- src/cvdata/filter.py | 41 ++--------------------------------------- src/cvdata/utils.py | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/cvdata/filter.py b/src/cvdata/filter.py index a868c62..1de747c 100644 --- a/src/cvdata/filter.py +++ b/src/cvdata/filter.py @@ -7,7 +7,7 @@ from tqdm import tqdm from cvdata.common import FORMAT_CHOICES, FORMAT_EXTENSIONS -from cvdata.utils import matching_ids +from cvdata.utils import darknet_indices_to_labels, matching_ids # ------------------------------------------------------------------------------ @@ -20,43 +20,6 @@ _logger = logging.getLogger(__name__) -# ------------------------------------------------------------------------------ -def _darknet_indices_to_labels( - darknet_labels_path: str, -) -> Dict: - """ - Parses a Darknet (YOLO) annotation labels file into a dictionary. The labels - file is expected to contain a single class label per line, and the resulting - dictionary will contain integer keys beginning at 0, so the first class label - will be the value for key 0, the second class label will be the value for key - 1, etc. For example, the labels file with the following lines: - - dog - cat - panda - - will result in the following indices to labels dictionary: - - { 0: "dog", 1: "cat", 2: "panda" } - - :param darknet_labels_path: path to the file containing labels used in - the Darknet dataset, should correspond to the labels used in the Darknet - annotation files of the dataset - :return: dictionary mapping index values to corresponding labels text - """ - - index_labels = {} - with open(darknet_labels_path, "r") as darknet_labels_file: - index = 0 - for line in darknet_labels_file: - if len(line.strip()) > 0: - darknet_label = line.split()[0] - index_labels[index] = darknet_label - index += 1 - - return index_labels - - # ------------------------------------------------------------------------------ def _count_boxes_darknet( darknet_file_path: str, @@ -298,7 +261,7 @@ def filter_class_boxes( darknet_index_labels = None if annotation_format == "darknet": # read the Darknet labels into a dictionary mapping label to label index - darknet_index_labels = _darknet_indices_to_labels(darknet_labels_path) + darknet_index_labels = darknet_indices_to_labels(darknet_labels_path) # get the set of valid indices, i.e. all Darknet indices # corresponding to the labels to be included in the filtered dataset diff --git a/src/cvdata/utils.py b/src/cvdata/utils.py index ae5bc6a..aa01831 100644 --- a/src/cvdata/utils.py +++ b/src/cvdata/utils.py @@ -1,9 +1,46 @@ import os -from typing import Set +from typing import Dict, Set import PIL.Image as Image +# ------------------------------------------------------------------------------ +def darknet_indices_to_labels( + darknet_labels_path: str, +) -> Dict: + """ + Parses a Darknet (YOLO) annotation labels file into a dictionary. The labels + file is expected to contain a single class label per line, and the resulting + dictionary will contain integer keys beginning at 0, so the first class label + will be the value for key 0, the second class label will be the value for key + 1, etc. For example, the labels file with the following lines: + + dog + cat + panda + + will result in the following indices to labels dictionary: + + { 0: "dog", 1: "cat", 2: "panda" } + + :param darknet_labels_path: path to the file containing labels used in + the Darknet dataset, should correspond to the labels used in the Darknet + annotation files of the dataset + :return: dictionary mapping index values to corresponding labels text + """ + + index_labels = {} + with open(darknet_labels_path, "r") as darknet_labels_file: + index = 0 + for line in darknet_labels_file: + if len(line.strip()) > 0: + darknet_label = line.split()[0] + index_labels[index] = darknet_label + index += 1 + + return index_labels + + # ------------------------------------------------------------------------------ def image_dimensions( image_file_path: str,