Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax importer for Pascal VOC dataset (seach in subdirectories) #50

Merged
merged 3 commits into from
Nov 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions datumaro/plugins/voc_format/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@

from .format import VocTask, VocPath

def find_path(root_path, path, depth=4):
level, is_found = 0, False
full_path = None
while level < depth and not is_found:
full_path = osp.join(root_path, path)
paths = glob(full_path)
if paths:
full_path = paths[0] # ignore all after the first one
is_found = osp.isdir(full_path)
else:
full_path = None

level += 1
root_path = osp.join(root_path, '*')

return full_path

class VocImporter(Importer):
_TASKS = [
Expand Down Expand Up @@ -41,12 +57,21 @@ def __call__(self, path, **extra_params):

@classmethod
def find_sources(cls, path):
# find root path for the dataset
root_path = path
for task, extractor_type, task_dir in cls._TASKS:
task_path = find_path(root_path, osp.join(VocPath.SUBSETS_DIR, task_dir))
if task_path:
root_path = osp.dirname(osp.dirname(task_path))
break

subset_paths = []
for task, extractor_type, task_dir in cls._TASKS:
task_dir = osp.join(path, VocPath.SUBSETS_DIR, task_dir)
if not osp.isdir(task_dir):
task_path = osp.join(root_path, VocPath.SUBSETS_DIR, task_dir)

if not osp.isdir(task_path):
continue
task_subsets = [p for p in glob(osp.join(task_dir, '*.txt'))
task_subsets = [p for p in glob(osp.join(task_path, '*.txt'))
if '_' not in osp.basename(p)]
subset_paths += [(task, extractor_type, p) for p in task_subsets]
return subset_paths