| 34 | |
| 35 | |
| 36 | def make_dataset(dir, recursive=False, read_cache=False, write_cache=False): |
| 37 | images = [] |
| 38 | |
| 39 | if read_cache: |
| 40 | possible_filelist = os.path.join(dir, 'files.list') |
| 41 | if os.path.isfile(possible_filelist): |
| 42 | with open(possible_filelist, 'r') as f: |
| 43 | images = f.read().splitlines() |
| 44 | return images |
| 45 | |
| 46 | if recursive: |
| 47 | make_dataset_rec(dir, images) |
| 48 | else: |
| 49 | assert os.path.isdir(dir) or os.path.islink(dir), '%s is not a valid directory' % dir |
| 50 | |
| 51 | for root, dnames, fnames in sorted(os.walk(dir)): |
| 52 | for fname in fnames: |
| 53 | if is_image_file(fname): |
| 54 | path = os.path.join(root, fname) |
| 55 | images.append(path) |
| 56 | |
| 57 | if write_cache: |
| 58 | filelist_cache = os.path.join(dir, 'files.list') |
| 59 | with open(filelist_cache, 'w') as f: |
| 60 | for path in images: |
| 61 | f.write("%s\n" % path) |
| 62 | print('wrote filelist cache at %s' % filelist_cache) |
| 63 | |
| 64 | return images |
| 65 | |
| 66 | |
| 67 | def default_loader(path): |