From e8127f8250f56e60167ec60a4fa0bdc34e825163 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 25 May 2021 17:44:54 +0200 Subject: [PATCH 1/3] Add URL file download to check_file() --- utils/general.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/utils/general.py b/utils/general.py index 9a882715f0ad..996060dd510e 100755 --- a/utils/general.py +++ b/utils/general.py @@ -173,12 +173,18 @@ def check_imshow(): def check_file(file): - # Search for file if not found - if Path(file).is_file() or file == '': + # Search/download file (if necessary) and return path + file = str(file) # convert to str() + if Path(file).is_file() or file == '': # exists return file - else: + elif file.startswith(('http://', 'https://')): # download + url, file = file, Path(file).name + torch.hub.download_url_to_file(url, file) + assert Path(file).is_file(), f'File download failed: {url}' + return file + else: # search files = glob.glob('./**/' + file, recursive=True) # find file - assert len(files), f'File Not Found: {file}' # assert file was found + assert len(files), f'File not found: {file}' # assert file was found assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}" # assert unique return files[0] # return file From 05dd0b28407803b94cac120bbcbb15fcfecb9bf0 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 26 May 2021 15:46:16 +0200 Subject: [PATCH 2/3] cleanup --- utils/general.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 996060dd510e..72a5443d2693 100755 --- a/utils/general.py +++ b/utils/general.py @@ -179,8 +179,9 @@ def check_file(file): return file elif file.startswith(('http://', 'https://')): # download url, file = file, Path(file).name + print(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, file) - assert Path(file).is_file(), f'File download failed: {url}' + assert file.exists() and file.stat().st_size > 1E6, f'File download failed: {url}' # check return file else: # search files = glob.glob('./**/' + file, recursive=True) # find file From b340543b0429655e6944ff1c2654364170472300 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 26 May 2021 15:48:25 +0200 Subject: [PATCH 3/3] pathlib bug fix --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 72a5443d2693..006e64859f32 100755 --- a/utils/general.py +++ b/utils/general.py @@ -181,7 +181,7 @@ def check_file(file): url, file = file, Path(file).name print(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, file) - assert file.exists() and file.stat().st_size > 1E6, f'File download failed: {url}' # check + assert Path(file).exists() and Path(file).stat().st_size > 0, f'File download failed: {url}' # check return file else: # search files = glob.glob('./**/' + file, recursive=True) # find file