Reference for ultralytics/utils/downloads.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/downloads.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.downloads.is_url
Validates if the given string is a URL and optionally checks if the URL exists online.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The string to be validated as a URL. |
required |
check
|
bool
|
If True, performs an additional check to see if the URL exists online. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
bool
|
Returns True for a valid URL. If 'check' is True, also returns True if the URL exists online. Returns False otherwise. |
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.delete_dsstore
Deletes all ".DS_store" files under a specified directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The directory path where the ".DS_store" files should be deleted. |
required |
files_to_delete
|
tuple
|
The files to be deleted. |
('.DS_Store', '__MACOSX')
|
Note
".DS_store" files are created by the Apple operating system and contain metadata about folders and files. They are hidden system files and can cause issues when transferring files between different operating systems.
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.zip_directory
zip_directory(directory, compress=True, exclude=('.DS_Store', '__MACOSX'), progress=True)
Zips the contents of a directory, excluding files containing strings in the exclude list. The resulting zip file is named after the directory and placed alongside it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str | Path
|
The path to the directory to be zipped. |
required |
compress
|
bool
|
Whether to compress the files while zipping. Default is True. |
True
|
exclude
|
tuple
|
A tuple of filename strings to be excluded. Defaults to ('.DS_Store', '__MACOSX'). |
('.DS_Store', '__MACOSX')
|
progress
|
bool
|
Whether to display a progress bar. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
Path
|
The path to the resulting zip file. |
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.unzip_file
unzip_file(file, path=None, exclude=('.DS_Store', '__MACOSX'), exist_ok=False, progress=True)
Unzips a *.zip file to the specified path, excluding files containing strings in the exclude list.
If the zipfile does not contain a single top-level directory, the function will create a new directory with the same name as the zipfile (without the extension) to extract its contents. If a path is not provided, the function will use the parent directory of the zipfile as the default path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
The path to the zipfile to be extracted. |
required |
path
|
str
|
The path to extract the zipfile to. Defaults to None. |
None
|
exclude
|
tuple
|
A tuple of filename strings to be excluded. Defaults to ('.DS_Store', '__MACOSX'). |
('.DS_Store', '__MACOSX')
|
exist_ok
|
bool
|
Whether to overwrite existing contents if they exist. Defaults to False. |
False
|
progress
|
bool
|
Whether to display a progress bar. Defaults to True. |
True
|
Raises:
Type | Description |
---|---|
BadZipFile
|
If the provided file does not exist or is not a valid zipfile. |
Returns:
Type | Description |
---|---|
Path
|
The path to the directory where the zipfile was extracted. |
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.check_disk_space
check_disk_space(url='https://ultralytics.com/assets/coco8.zip', path=Path.cwd(), sf=1.5, hard=True)
Check if there is sufficient disk space to download and store a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL to the file. Defaults to 'https://ultralytics.com/assets/coco8.zip'. |
'https://ultralytics.com/assets/coco8.zip'
|
path
|
str | Path
|
The path or drive to check the available free space on. |
cwd()
|
sf
|
float
|
Safety factor, the multiplier for the required free space. Defaults to 1.5. |
1.5
|
hard
|
bool
|
Whether to throw an error or not on insufficient disk space. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
bool
|
True if there is sufficient disk space, False otherwise. |
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.get_google_drive_file_info
Retrieves the direct download link and filename for a shareable Google Drive file link.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
link
|
str
|
The shareable link of the Google Drive file. |
required |
Returns:
Type | Description |
---|---|
str
|
Direct download URL for the Google Drive file. |
str
|
Original filename of the Google Drive file. If filename extraction fails, returns None. |
Example
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.safe_download
safe_download(url, file=None, dir=None, unzip=True, delete=False, curl=False, retry=3, min_bytes=1.0, exist_ok=False, progress=True)
Downloads files from a URL, with options for retrying, unzipping, and deleting the downloaded file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL of the file to be downloaded. |
required |
file
|
str
|
The filename of the downloaded file. If not provided, the file will be saved with the same name as the URL. |
None
|
dir
|
str
|
The directory to save the downloaded file. If not provided, the file will be saved in the current working directory. |
None
|
unzip
|
bool
|
Whether to unzip the downloaded file. Default: True. |
True
|
delete
|
bool
|
Whether to delete the downloaded file after unzipping. Default: False. |
False
|
curl
|
bool
|
Whether to use curl command line tool for downloading. Default: False. |
False
|
retry
|
int
|
The number of times to retry the download in case of failure. Default: 3. |
3
|
min_bytes
|
float
|
The minimum number of bytes that the downloaded file should have, to be considered a successful download. Default: 1E0. |
1.0
|
exist_ok
|
bool
|
Whether to overwrite existing contents during unzipping. Defaults to False. |
False
|
progress
|
bool
|
Whether to display a progress bar during the download. Default: True. |
True
|
Example
Source code in ultralytics/utils/downloads.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
ultralytics.utils.downloads.get_github_assets
Retrieve the specified version's tag and assets from a GitHub repository. If the version is not specified, the function fetches the latest release assets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo
|
str
|
The GitHub repository in the format 'owner/repo'. Defaults to 'ultralytics/assets'. |
'ultralytics/assets'
|
version
|
str
|
The release version to fetch assets from. Defaults to 'latest'. |
'latest'
|
retry
|
bool
|
Flag to retry the request in case of a failure. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing the release tag and a list of asset names. |
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.attempt_download_asset
Attempt to download a file from GitHub release assets if it is not found locally. The function checks for the file locally first, then tries to download it from the specified GitHub repository release.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str | Path
|
The filename or file path to be downloaded. |
required |
repo
|
str
|
The GitHub repository in the format 'owner/repo'. Defaults to 'ultralytics/assets'. |
'ultralytics/assets'
|
release
|
str
|
The specific release version to be downloaded. Defaults to 'v8.3.0'. |
'v8.3.0'
|
**kwargs
|
any
|
Additional keyword arguments for the download process. |
{}
|
Returns:
Type | Description |
---|---|
str
|
The path to the downloaded file. |
Example
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.download
download(url, dir=Path.cwd(), unzip=True, delete=False, curl=False, threads=1, retry=3, exist_ok=False)
Downloads files from specified URLs to a given directory. Supports concurrent downloads if multiple threads are specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str | list
|
The URL or list of URLs of the files to be downloaded. |
required |
dir
|
Path
|
The directory where the files will be saved. Defaults to the current working directory. |
cwd()
|
unzip
|
bool
|
Flag to unzip the files after downloading. Defaults to True. |
True
|
delete
|
bool
|
Flag to delete the zip files after extraction. Defaults to False. |
False
|
curl
|
bool
|
Flag to use curl for downloading. Defaults to False. |
False
|
threads
|
int
|
Number of threads to use for concurrent downloads. Defaults to 1. |
1
|
retry
|
int
|
Number of retries in case of download failure. Defaults to 3. |
3
|
exist_ok
|
bool
|
Whether to overwrite existing contents during unzipping. Defaults to False. |
False
|