glenn-jocher commited on
Commit
4bdc5a3
1 Parent(s): 73a0669

LoadImages() pathlib update (#2140)

Browse files
Files changed (1) hide show
  1. utils/datasets.py +5 -3
utils/datasets.py CHANGED
@@ -120,8 +120,7 @@ class _RepeatSampler(object):
120
 
121
  class LoadImages: # for inference
122
  def __init__(self, path, img_size=640, stride=32):
123
- p = str(Path(path)) # os-agnostic
124
- p = os.path.abspath(p) # absolute path
125
  if '*' in p:
126
  files = sorted(glob.glob(p, recursive=True)) # glob
127
  elif os.path.isdir(p):
@@ -349,21 +348,24 @@ class LoadImagesAndLabels(Dataset): # for training/testing
349
  self.mosaic_border = [-img_size // 2, -img_size // 2]
350
  self.stride = stride
351
  self.path = path
352
-
353
  try:
354
  f = [] # image files
355
  for p in path if isinstance(path, list) else [path]:
356
  p = Path(p) # os-agnostic
357
  if p.is_dir(): # dir
358
  f += glob.glob(str(p / '**' / '*.*'), recursive=True)
 
359
  elif p.is_file(): # file
360
  with open(p, 'r') as t:
361
  t = t.read().strip().splitlines()
362
  parent = str(p.parent) + os.sep
363
  f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
 
364
  else:
365
  raise Exception(f'{prefix}{p} does not exist')
366
  self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats])
 
367
  assert self.img_files, f'{prefix}No images found'
368
  except Exception as e:
369
  raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}')
 
120
 
121
  class LoadImages: # for inference
122
  def __init__(self, path, img_size=640, stride=32):
123
+ p = str(Path(path).absolute()) # os-agnostic absolute path
 
124
  if '*' in p:
125
  files = sorted(glob.glob(p, recursive=True)) # glob
126
  elif os.path.isdir(p):
 
348
  self.mosaic_border = [-img_size // 2, -img_size // 2]
349
  self.stride = stride
350
  self.path = path
351
+
352
  try:
353
  f = [] # image files
354
  for p in path if isinstance(path, list) else [path]:
355
  p = Path(p) # os-agnostic
356
  if p.is_dir(): # dir
357
  f += glob.glob(str(p / '**' / '*.*'), recursive=True)
358
+ # f = list(p.rglob('**/*.*')) # pathlib
359
  elif p.is_file(): # file
360
  with open(p, 'r') as t:
361
  t = t.read().strip().splitlines()
362
  parent = str(p.parent) + os.sep
363
  f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
364
+ # f += [p.parent / x.lstrip(os.sep) for x in t] # local to global path (pathlib)
365
  else:
366
  raise Exception(f'{prefix}{p} does not exist')
367
  self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats])
368
+ # self.img_files = sorted([x for x in f if x.suffix[1:].lower() in img_formats]) # pathlib
369
  assert self.img_files, f'{prefix}No images found'
370
  except Exception as e:
371
  raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}')