Skip to content

Commit

Permalink
Merge pull request #136 from monocongo/issue_135_filter_darknet_label…
Browse files Browse the repository at this point in the history
…s_parse_bug

Filter module Darknet labels parse bug fix
  • Loading branch information
monocongo authored Feb 5, 2020
2 parents 800da9a + 08b9883 commit f8e02ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,11 @@ supports limiting the number of bounding boxes per class type. The filtered data
will contain annotation files with bounding boxes only for the class labels specified
and limited to the number of boxes specified for each class label. For example:
```bash
$ cvdata_filter --src_annotations /data/darknet --dest_annotations /data/filtered_darknet \
--src_images /data/images --dest_images /data/filtered_images \
$ cvdata_filter --format darknet \
--src_annotations /data/darknet \
--dest_annotations /data/filtered_darknet \
--src_images /data/images \
--dest_images /data/filtered_images \
--darknet_labels /data/darknet/labels.txt \
--boxes_per_class car:6000 truck:6000
```
Expand Down
23 changes: 18 additions & 5 deletions src/cvdata/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,34 @@ def _darknet_indices_to_labels(
darknet_labels_path: str,
) -> Dict:
"""
TODO
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 file
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:
darknet_label = line.split()[0]
index_labels[index] = darknet_label
index += 1
if len(line.strip()) > 0:
darknet_label = line.split()[0]
index_labels[index] = darknet_label
index += 1

return index_labels

Expand Down

0 comments on commit f8e02ec

Please sign in to comment.