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
is_url(url, check=False)
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. |
Examples:
>>> valid = is_url("https://www.example.com")
Source code in ultralytics/utils/downloads.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
ultralytics.utils.downloads.delete_dsstore
delete_dsstore(path, files_to_delete=('.DS_Store', '__MACOSX'))
Delete all ".DS_store" files in 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')
|
Examples:
>>> from ultralytics.utils.downloads import delete_dsstore
>>> delete_dsstore("path/to/dir")
Notes
".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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
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. |
Examples:
>>> from ultralytics.utils.downloads import zip_directory
>>> file = zip_directory("path/to/dir")
Source code in ultralytics/utils/downloads.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
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 | Path
|
The path to the zipfile to be extracted. |
required |
path
|
str | Path
|
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. |
Examples:
>>> from ultralytics.utils.downloads import unzip_file
>>> directory = unzip_file("path/to/file.zip")
Source code in ultralytics/utils/downloads.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
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
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
ultralytics.utils.downloads.get_google_drive_file_info
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. |
Examples:
>>> from ultralytics.utils.downloads import get_google_drive_file_info
>>> link = "https://drive.google.com/file/d/1cqT-cJgANNrhIHCrEufUYhQ4RqiWG_lJ/view?usp=drive_link"
>>> url, filename = get_google_drive_file_info(link)
Source code in ultralytics/utils/downloads.py
230 231 232 233 234 235 236 237 238 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 |
|
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 | Path
|
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
|
Returns:
Type | Description |
---|---|
Path | str
|
The path to the downloaded file or extracted directory. |
Examples:
>>> from ultralytics.utils.downloads import safe_download
>>> link = "https://ultralytics.com/assets/bus.jpg"
>>> path = safe_download(link)
Source code in ultralytics/utils/downloads.py
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 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 |
|
ultralytics.utils.downloads.get_github_assets
get_github_assets(repo='ultralytics/assets', version='latest', retry=False)
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 |
---|---|
str
|
The release tag. |
List[str]
|
A list of asset names. |
Examples:
>>> tag, assets = get_github_assets(repo="ultralytics/assets", version="latest")
Source code in ultralytics/utils/downloads.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
|
ultralytics.utils.downloads.attempt_download_asset
attempt_download_asset(
file, repo="ultralytics/assets", release="v8.3.0", **kwargs
)
Attempt to download a file from GitHub release assets if it is not found locally.
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. |
Examples:
>>> file_path = attempt_download_asset("yolo11n.pt", repo="ultralytics/assets", release="latest")
Source code in ultralytics/utils/downloads.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
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[str]
|
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
|
Examples:
>>> download("https://ultralytics.com/assets/example.zip", dir="path/to/dir", unzip=True)
Source code in ultralytics/utils/downloads.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
|