Skip to content

Commit

Permalink
moved the darknet_indices_to_labels function into the common module s…
Browse files Browse the repository at this point in the history
…ince it will be used by multiple modules

#139
  • Loading branch information
monocongo committed Feb 5, 2020
1 parent 5d811bd commit 6d11120
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
41 changes: 2 additions & 39 deletions src/cvdata/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


# ------------------------------------------------------------------------------
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion src/cvdata/utils.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 6d11120

Please sign in to comment.