Unverified 提交 4bdc5a39 authored 作者: Glenn Jocher's avatar Glenn Jocher 提交者: GitHub

LoadImages() pathlib update (#2140)

上级 73a06699
...@@ -120,8 +120,7 @@ class _RepeatSampler(object): ...@@ -120,8 +120,7 @@ class _RepeatSampler(object):
class LoadImages: # for inference class LoadImages: # for inference
def __init__(self, path, img_size=640, stride=32): def __init__(self, path, img_size=640, stride=32):
p = str(Path(path)) # os-agnostic p = str(Path(path).absolute()) # os-agnostic absolute path
p = os.path.abspath(p) # absolute path
if '*' in p: if '*' in p:
files = sorted(glob.glob(p, recursive=True)) # glob files = sorted(glob.glob(p, recursive=True)) # glob
elif os.path.isdir(p): elif os.path.isdir(p):
...@@ -349,21 +348,24 @@ class LoadImagesAndLabels(Dataset): # for training/testing ...@@ -349,21 +348,24 @@ class LoadImagesAndLabels(Dataset): # for training/testing
self.mosaic_border = [-img_size // 2, -img_size // 2] self.mosaic_border = [-img_size // 2, -img_size // 2]
self.stride = stride self.stride = stride
self.path = path self.path = path
try: try:
f = [] # image files f = [] # image files
for p in path if isinstance(path, list) else [path]: for p in path if isinstance(path, list) else [path]:
p = Path(p) # os-agnostic p = Path(p) # os-agnostic
if p.is_dir(): # dir if p.is_dir(): # dir
f += glob.glob(str(p / '**' / '*.*'), recursive=True) f += glob.glob(str(p / '**' / '*.*'), recursive=True)
# f = list(p.rglob('**/*.*')) # pathlib
elif p.is_file(): # file elif p.is_file(): # file
with open(p, 'r') as t: with open(p, 'r') as t:
t = t.read().strip().splitlines() t = t.read().strip().splitlines()
parent = str(p.parent) + os.sep parent = str(p.parent) + os.sep
f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
# f += [p.parent / x.lstrip(os.sep) for x in t] # local to global path (pathlib)
else: else:
raise Exception(f'{prefix}{p} does not exist') raise Exception(f'{prefix}{p} does not exist')
self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats]) self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats])
# self.img_files = sorted([x for x in f if x.suffix[1:].lower() in img_formats]) # pathlib
assert self.img_files, f'{prefix}No images found' assert self.img_files, f'{prefix}No images found'
except Exception as e: except Exception as e:
raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}') raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论