def _iter_imgs_unordered(images_spec): if '*' in images_spec: matches = glob.glob(images_spec) _, ext = os.path.splitext(images_spec) if not ext: matches = (p for p in matches if has_image_ext(p)) elif not has_image_ext(images_spec): raise ValueError( 'Unrecognized extension {} in glob ({})'.format( ext, images_spec)) if not matches: raise ValueError('No matches for glob {}'.format(images_spec)) yield from matches return if has_image_ext(images_spec): yield from [images_spec] return # At this point, images_spec should be a path to a directory if not os.path.isdir(images_spec): raise NotADirectoryError(images_spec) print('Recursively traversing {}...'.format(images_spec)) for root, _, fnames in os.walk(images_spec, followlinks=True): for fname in fnames: if has_image_ext(fname): p = os.path.join(root, fname) if os.path.getsize(p) == 0: print('WARN / 0 bytes /', p) continue yield p
def __init__(self, root_dir_or_img, max_imgs=None, skip_hidden=False, append_id=None): """ :param root_dir_or_img: Either a directory with images or the path of a single image. :param max_imgs: If given, subsample deterministically to only contain max_imgs :param skip_hidden: If given, skip images starting with '.' :param append_id: If given, append `append_id` to self.id :raises ValueError if root_dir is not a directory or does not contain images """ self.root_dir_or_img = root_dir_or_img if os.path.isdir(root_dir_or_img): root_dir = root_dir_or_img self.name = os.path.basename(root_dir.rstrip('/')) self.ps = sorted(p for p in os_ext.listdir_paths(root_dir) if has_image_ext(p)) if skip_hidden: self.ps = self._filter_hidden(self.ps) if max_imgs and max_imgs < len(self.ps): print('Subsampling to use {} imgs of {}...'.format( max_imgs, self.name)) idxs = np.linspace(0, len(self.ps) - 1, max_imgs, dtype=np.int) self.ps = np.array(self.ps)[idxs].tolist() assert len(self.ps) == max_imgs assert_exc( len(self.ps) > 0, 'No images found in {}'.format(root_dir), ValueError) self.id = '{}_{}'.format(self.name, len(self.ps)) self._str = 'Testset({}): in {}, {} images'.format( self.name, root_dir, len(self.ps)) else: img = root_dir_or_img assert_exc(os.path.isfile(img), 'Does not exist: {}'.format(img), FileNotFoundError) self.name = os.path.basename(img) self.ps = [img] self.id = img self._str = 'Testset([{}]): 1 image'.format(self.name) if append_id: self.id += append_id