Add URL download to check_file() (#3330)
* Add URL file download to check_file() * cleanup * pathlib bug fix
This commit is contained in:
1 parent
c6b5bfca85
commit
2435bfe896
1 file changed
+11
-4
+11
-4
@@ -173,12 +173,19 @@ def check_imshow():
|
|||||||
|
|
||||||
|
|
||||||
def check_file(file):
|
def check_file(file):
|
||||||
# Search for file if not found
|
# Search/download file (if necessary) and return path
|
||||||
if Path(file).is_file() or file == '':
|
file = str(file) # convert to str()
|
||||||
|
if Path(file).is_file() or file == '': # exists
|
||||||
return file
|
return file
|
||||||
else:
|
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).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
|
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
|
assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}" # assert unique
|
||||||
return files[0] # return file
|
return files[0] # return file
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user