Reference for ultralytics/utils/downloads.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/downloads.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.downloads.is_url(url, check=True)
Check if string is URL and check if URL exists.
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.delete_dsstore(path, files_to_delete=('.DS_Store', '__MACOSX'))
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(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(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(url='https://ultralytics.com/assets/coco128.zip', 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/coco128.zip'. |
'https://ultralytics.com/assets/coco128.zip'
|
sf |
float
|
Safety factor, the multiplier for the required free space. Defaults to 2.0. |
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(link)
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(url, file=None, dir=None, unzip=True, delete=False, curl=False, retry=3, min_bytes=1.0, 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
|
progress |
bool
|
Whether to display a progress bar during the download. Default: True. |
True
|
Source code in ultralytics/utils/downloads.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 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 |
|
ultralytics.utils.downloads.get_github_assets(repo='ultralytics/assets', version='latest', retry=False)
Return GitHub repo tag and assets (i.e. ['yolov8n.pt', 'yolov8s.pt', ...]).
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.attempt_download_asset(file, repo='ultralytics/assets', release='v0.0.0')
Attempt file download from GitHub release assets if not found locally. release = 'latest', 'v6.2', etc.
Source code in ultralytics/utils/downloads.py
ultralytics.utils.downloads.download(url, dir=Path.cwd(), unzip=True, delete=False, curl=False, threads=1, retry=3)
Downloads and unzips files concurrently if threads > 1, else sequentially.