์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ ultralytics/utils/downloads.py

์ฐธ๊ณ 

์ด ํŒŒ์ผ์€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/utils/downloads .py์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ (๐Ÿ› ๏ธ)๋ฅผ ์ œ์ถœํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋„๋ก ๋„์™€์ฃผ์„ธ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™!



ultralytics.utils.downloads.is_url(url, check=True)

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด URL์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์„ ํƒ์ ์œผ๋กœ URL์ด ์˜จ๋ผ์ธ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
url str

URL๋กœ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
check bool

True์ธ ๊ฒฝ์šฐ URL์ด ์˜จ๋ผ์ธ์— ์กด์žฌํ•˜๋Š”์ง€ ์ถ”๊ฐ€ ํ™•์ธ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ True์ž…๋‹ˆ๋‹ค.

True

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
bool

๋ฌธ์ž์—ด์ด ์œ ํšจํ•œ URL์ธ ๊ฒฝ์šฐ True๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 'check'๊ฐ€ True์ธ ๊ฒฝ์šฐ URL์ด ์˜จ๋ผ์ธ์— ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ True๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ
valid = is_url("https://www.example.com")
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def is_url(url, check=True):
    """
    Validates if the given string is a URL and optionally checks if the URL exists online.

    Args:
        url (str): The string to be validated as a URL.
        check (bool, optional): If True, performs an additional check to see if the URL exists online.
            Defaults to True.

    Returns:
        (bool): Returns True if the string is a valid URL. If 'check' is True, also returns True if the URL exists online.
            Returns False otherwise.

    Example:
        ```python
        valid = is_url("https://www.example.com")
        ```
    """
    with contextlib.suppress(Exception):
        url = str(url)
        result = parse.urlparse(url)
        assert all([result.scheme, result.netloc])  # check if is url
        if check:
            with request.urlopen(url) as response:
                return response.getcode() == 200  # check if exists online
        return True
    return False



ultralytics.utils.downloads.delete_dsstore(path, files_to_delete=('.DS_Store', '__MACOSX'))

์ง€์ •๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ ์•„๋ž˜์˜ ๋ชจ๋“  '.DS_store' ํŒŒ์ผ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
path str

".DS_store" ํŒŒ์ผ์„ ์‚ญ์ œํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
files_to_delete tuple

์‚ญ์ œํ•  ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.

('.DS_Store', '__MACOSX')
์˜ˆ์ œ
from ultralytics.utils.downloads import delete_dsstore

delete_dsstore('path/to/dir')
์ฐธ๊ณ 

".DS_store" ํŒŒ์ผ์€ Apple ์šด์˜ ์ฒด์ œ์—์„œ ์ƒ์„ฑ๋˜๋ฉฐ ํด๋” ๋ฐ ํŒŒ์ผ์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ๋“ค์€ ์€ ์ˆจ๊ฒจ์ง„ ์‹œ์Šคํ…œ ํŒŒ์ผ์ด๋ฉฐ ์„œ๋กœ ๋‹ค๋ฅธ ์šด์˜ ์ฒด์ œ ๊ฐ„์— ํŒŒ์ผ์„ ์ „์†กํ•  ๋•Œ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def delete_dsstore(path, files_to_delete=(".DS_Store", "__MACOSX")):
    """
    Deletes all ".DS_store" files under a specified directory.

    Args:
        path (str, optional): The directory path where the ".DS_store" files should be deleted.
        files_to_delete (tuple): The files to be deleted.

    Example:
        ```python
        from ultralytics.utils.downloads import delete_dsstore

        delete_dsstore('path/to/dir')
        ```

    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.
    """
    for file in files_to_delete:
        matches = list(Path(path).rglob(file))
        LOGGER.info(f"Deleting {file} files: {matches}")
        for f in matches:
            f.unlink()



ultralytics.utils.downloads.zip_directory(directory, compress=True, exclude=('.DS_Store', '__MACOSX'), progress=True)

์ œ์™ธ ๋ชฉ๋ก์— ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋œ ํŒŒ์ผ์„ ์ œ์™ธํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ๋‚ด์šฉ์„ ์••์ถ•ํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ์••์ถ• ํŒŒ์ผ์€ ๋””๋ ‰ํ† ๋ฆฌ์˜ ์ด๋ฆ„์„ ๋”ฐ์„œ ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ๋‚˜๋ž€ํžˆ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
directory str | Path

์••์ถ•ํ•  ๋””๋ ‰ํ† ๋ฆฌ์˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
compress bool

์••์ถ•ํ•˜๋Š” ๋™์•ˆ ํŒŒ์ผ์„ ์••์ถ•ํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ True์ž…๋‹ˆ๋‹ค.

True
exclude tuple

์ œ์™ธํ•  ํŒŒ์ผ๋ช… ๋ฌธ์ž์—ด์˜ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ('.DS_Store', '__MACOSX')์ž…๋‹ˆ๋‹ค.

('.DS_Store', '__MACOSX')
progress bool

์ง„ํ–‰๋ฅ  ํ‘œ์‹œ์ค„์„ ํ‘œ์‹œํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ True์ž…๋‹ˆ๋‹ค.

True

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
Path

๊ฒฐ๊ณผ๋ฌผ์ธ zip ํŒŒ์ผ์˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ
from ultralytics.utils.downloads import zip_directory

file = zip_directory('path/to/dir')
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def 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.

    Args:
        directory (str | Path): The path to the directory to be zipped.
        compress (bool): Whether to compress the files while zipping. Default is True.
        exclude (tuple, optional): A tuple of filename strings to be excluded. Defaults to ('.DS_Store', '__MACOSX').
        progress (bool, optional): Whether to display a progress bar. Defaults to True.

    Returns:
        (Path): The path to the resulting zip file.

    Example:
        ```python
        from ultralytics.utils.downloads import zip_directory

        file = zip_directory('path/to/dir')
        ```
    """
    from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile

    delete_dsstore(directory)
    directory = Path(directory)
    if not directory.is_dir():
        raise FileNotFoundError(f"Directory '{directory}' does not exist.")

    # Unzip with progress bar
    files_to_zip = [f for f in directory.rglob("*") if f.is_file() and all(x not in f.name for x in exclude)]
    zip_file = directory.with_suffix(".zip")
    compression = ZIP_DEFLATED if compress else ZIP_STORED
    with ZipFile(zip_file, "w", compression) as f:
        for file in TQDM(files_to_zip, desc=f"Zipping {directory} to {zip_file}...", unit="file", disable=not progress):
            f.write(file, file.relative_to(directory))

    return zip_file  # return path to zip file



ultralytics.utils.downloads.unzip_file(file, path=None, exclude=('.DS_Store', '__MACOSX'), exist_ok=False, progress=True)

์ œ์™ธ ๋ชฉ๋ก์— ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ํฌํ•จ๋œ ํŒŒ์ผ์„ ์ œ์™ธํ•œ *.zip ํŒŒ์ผ์„ ์ง€์ •๋œ ๊ฒฝ๋กœ๋กœ ์••์ถ• ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.

zip ํŒŒ์ผ์— ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ํ•˜๋‚˜๋„ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์ด ํ•จ์ˆ˜๋Š” zip ํŒŒ์ผ๊ณผ ๊ฐ™์€ ์ด๋ฆ„์˜ ์ƒˆ๋กœ์šด ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์—ฌ (ํ™•์žฅ์ž ์—†์ด) ๋‚ด์šฉ์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฉด ์ด ํ•จ์ˆ˜๋Š” zip ํŒŒ์ผ์˜ ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ธฐ๋ณธ ๊ฒฝ๋กœ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
file str

์ถ”์ถœํ•  zip ํŒŒ์ผ์˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
path str

zip ํŒŒ์ผ์„ ์ถ”์ถœํ•  ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ์—†์Œ์ž…๋‹ˆ๋‹ค.

None
exclude tuple

์ œ์™ธํ•  ํŒŒ์ผ๋ช… ๋ฌธ์ž์—ด์˜ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ('.DS_Store', '__MACOSX')์ž…๋‹ˆ๋‹ค.

('.DS_Store', '__MACOSX')
exist_ok bool

๊ธฐ์กด ์ฝ˜ํ…์ธ ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฎ์–ด์“ธ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ False์ž…๋‹ˆ๋‹ค.

False
progress bool

์ง„ํ–‰๋ฅ  ํ‘œ์‹œ์ค„์„ ํ‘œ์‹œํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ True์ž…๋‹ˆ๋‹ค.

True

์˜ฌ๋ฆฌ๊ธฐ:

์œ ํ˜• ์„ค๋ช…
BadZipFile

์ œ๊ณต๋œ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ์œ ํšจํ•œ zip ํŒŒ์ผ์ด ์•„๋‹Œ ๊ฒฝ์šฐ.

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
Path

์••์ถ• ํŒŒ์ผ์ด ์ถ”์ถœ๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ
from ultralytics.utils.downloads import unzip_file

dir = unzip_file('path/to/file.zip')
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def 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.

    Args:
        file (str): The path to the zipfile to be extracted.
        path (str, optional): The path to extract the zipfile to. Defaults to None.
        exclude (tuple, optional): A tuple of filename strings to be excluded. Defaults to ('.DS_Store', '__MACOSX').
        exist_ok (bool, optional): Whether to overwrite existing contents if they exist. Defaults to False.
        progress (bool, optional): Whether to display a progress bar. Defaults to True.

    Raises:
        BadZipFile: If the provided file does not exist or is not a valid zipfile.

    Returns:
        (Path): The path to the directory where the zipfile was extracted.

    Example:
        ```python
        from ultralytics.utils.downloads import unzip_file

        dir = unzip_file('path/to/file.zip')
        ```
    """
    from zipfile import BadZipFile, ZipFile, is_zipfile

    if not (Path(file).exists() and is_zipfile(file)):
        raise BadZipFile(f"File '{file}' does not exist or is a bad zip file.")
    if path is None:
        path = Path(file).parent  # default path

    # Unzip the file contents
    with ZipFile(file) as zipObj:
        files = [f for f in zipObj.namelist() if all(x not in f for x in exclude)]
        top_level_dirs = {Path(f).parts[0] for f in files}

        if len(top_level_dirs) > 1 or (len(files) > 1 and not files[0].endswith("/")):
            # Zip has multiple files at top level
            path = extract_path = Path(path) / Path(file).stem  # i.e. ../datasets/coco8
        else:
            # Zip has 1 top-level directory
            extract_path = path  # i.e. ../datasets
            path = Path(path) / list(top_level_dirs)[0]  # i.e. ../datasets/coco8

        # Check if destination directory already exists and contains files
        if path.exists() and any(path.iterdir()) and not exist_ok:
            # If it exists and is not empty, return the path without unzipping
            LOGGER.warning(f"WARNING โš ๏ธ Skipping {file} unzip as destination directory {path} is not empty.")
            return path

        for f in TQDM(files, desc=f"Unzipping {file} to {Path(path).resolve()}...", unit="file", disable=not progress):
            # Ensure the file is within the extract_path to avoid path traversal security vulnerability
            if ".." in Path(f).parts:
                LOGGER.warning(f"Potentially insecure file path: {f}, skipping extraction.")
                continue
            zipObj.extract(f, extract_path)

    return path  # return unzip dir



ultralytics.utils.downloads.check_disk_space(url='https://ultralytics.com/assets/coco128.zip', sf=1.5, hard=True)

ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋””์Šคํฌ ๊ณต๊ฐ„์ด ์ถฉ๋ถ„ํ•œ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
url str

ํŒŒ์ผ์˜ URL์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 'https://ultralytics.com/assets/coco128.zip'์ž…๋‹ˆ๋‹ค.

'https://ultralytics.com/assets/coco128.zip'
sf float

์•ˆ์ „ ๊ณ„์ˆ˜: ํ•„์š”ํ•œ ์—ฌ์œ  ๊ณต๊ฐ„์— ๋Œ€ํ•œ ์Šน์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 2.0์ž…๋‹ˆ๋‹ค.

1.5
hard bool

๋””์Šคํฌ ๊ณต๊ฐ„ ๋ถ€์กฑ ์‹œ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ True์ž…๋‹ˆ๋‹ค.

True

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
bool

๋””์Šคํฌ ๊ณต๊ฐ„์ด ์ถฉ๋ถ„ํ•˜๋ฉด ์ฐธ์ด๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฑฐ์ง“์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def 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.

    Args:
        url (str, optional): The URL to the file. Defaults to 'https://ultralytics.com/assets/coco128.zip'.
        sf (float, optional): Safety factor, the multiplier for the required free space. Defaults to 2.0.
        hard (bool, optional): Whether to throw an error or not on insufficient disk space. Defaults to True.

    Returns:
        (bool): True if there is sufficient disk space, False otherwise.
    """
    try:
        r = requests.head(url)  # response
        assert r.status_code < 400, f"URL error for {url}: {r.status_code} {r.reason}"  # check response
    except Exception:
        return True  # requests issue, default to True

    # Check file size
    gib = 1 << 30  # bytes per GiB
    data = int(r.headers.get("Content-Length", 0)) / gib  # file size (GB)
    total, used, free = (x / gib for x in shutil.disk_usage(Path.cwd()))  # bytes

    if data * sf < free:
        return True  # sufficient space

    # Insufficient space
    text = (
        f"WARNING โš ๏ธ Insufficient free disk space {free:.1f} GB < {data * sf:.3f} GB required, "
        f"Please free {data * sf - free:.1f} GB additional disk space and try again."
    )
    if hard:
        raise MemoryError(text)
    LOGGER.warning(text)
    return False



ultralytics.utils.downloads.get_google_drive_file_info(link)

๊ณต์œ  ๊ฐ€๋Šฅํ•œ Google ๋“œ๋ผ์ด๋ธŒ ํŒŒ์ผ ๋งํฌ์˜ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ ๋งํฌ์™€ ํŒŒ์ผ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
link str

Google ๋“œ๋ผ์ด๋ธŒ ํŒŒ์ผ์˜ ๊ณต์œ  ๊ฐ€๋Šฅํ•œ ๋งํฌ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
str

Google ๋“œ๋ผ์ด๋ธŒ ํŒŒ์ผ์˜ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ URL์ž…๋‹ˆ๋‹ค.

str

Google ๋“œ๋ผ์ด๋ธŒ ํŒŒ์ผ์˜ ์›๋ณธ ํŒŒ์ผ๋ช…์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ์ด๋ฆ„ ์ถ”์ถœ์— ์‹คํŒจํ•˜๋ฉด None์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ
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)
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def get_google_drive_file_info(link):
    """
    Retrieves the direct download link and filename for a shareable Google Drive file link.

    Args:
        link (str): The shareable link of the Google Drive file.

    Returns:
        (str): Direct download URL for the Google Drive file.
        (str): Original filename of the Google Drive file. If filename extraction fails, returns None.

    Example:
        ```python
        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)
        ```
    """
    file_id = link.split("/d/")[1].split("/view")[0]
    drive_url = f"https://drive.google.com/uc?export=download&id={file_id}"
    filename = None

    # Start session
    with requests.Session() as session:
        response = session.get(drive_url, stream=True)
        if "quota exceeded" in str(response.content.lower()):
            raise ConnectionError(
                emojis(
                    f"โŒ  Google Drive file download quota exceeded. "
                    f"Please try again later or download this file manually at {link}."
                )
            )
        for k, v in response.cookies.items():
            if k.startswith("download_warning"):
                drive_url += f"&confirm={v}"  # v is token
        cd = response.headers.get("content-disposition")
        if cd:
            filename = re.findall('filename="(.+)"', cd)[0]
    return drive_url, filename



ultralytics.utils.downloads.safe_download(url, file=None, dir=None, unzip=True, delete=False, curl=False, retry=3, min_bytes=1.0, exist_ok=False, progress=True)

URL์—์„œ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์˜ ์žฌ์‹œ๋„, ์••์ถ• ํ•ด์ œ ๋ฐ ์‚ญ์ œ ์˜ต์…˜์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
url str

๋‹ค์šด๋กœ๋“œํ•  ํŒŒ์ผ์˜ URL์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
file str

๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์˜ ํŒŒ์ผ๋ช…์ž…๋‹ˆ๋‹ค. ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฉด ํŒŒ์ผ์ด URL๊ณผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

None
dir str

๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์„ ์ €์žฅํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฉด ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ์— ํŒŒ์ผ์ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

None
unzip bool

๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์˜ ์••์ถ•์„ ํ’€์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์ž…๋‹ˆ๋‹ค: True.

True
delete bool

์••์ถ•์„ ํ‘ผ ํ›„ ๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์„ ์‚ญ์ œํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์ž…๋‹ˆ๋‹ค: False.

False
curl bool

๋‹ค์šด๋กœ๋“œ์— curl ๋ช…๋ น์ค„ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. Default: False.

False
retry int

๋‹ค์šด๋กœ๋“œ ์‹คํŒจ ์‹œ ๋‹ค์‹œ ์‹œ๋„ํ•  ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’: 3.

3
min_bytes float

์„ฑ๊ณต์ ์ธ ๋‹ค์šด๋กœ๋“œ๋กœ ๊ฐ„์ฃผํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์— ํฌํ•จ๋˜์–ด์•ผ ํ•˜๋Š” ์ตœ์†Œ ๋ฐ”์ดํŠธ ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ตœ์†Œ ๋ฐ”์ดํŠธ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’: 1E0.

1.0
exist_ok bool

์••์ถ•์„ ํ‘ธ๋Š” ๋™์•ˆ ๊ธฐ์กด ์ฝ˜ํ…์ธ ๋ฅผ ๋ฎ์–ด์“ธ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ False์ž…๋‹ˆ๋‹ค.

False
progress bool

๋‹ค์šด๋กœ๋“œ ์ค‘ ์ง„ํ–‰๋ฅ  ํ‘œ์‹œ์ค„์„ ํ‘œ์‹œํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์ž…๋‹ˆ๋‹ค: True.

True
์˜ˆ์ œ
from ultralytics.utils.downloads import safe_download

link = "https://ultralytics.com/assets/bus.jpg"
path = safe_download(link)
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def safe_download(
    url,
    file=None,
    dir=None,
    unzip=True,
    delete=False,
    curl=False,
    retry=3,
    min_bytes=1e0,
    exist_ok=False,
    progress=True,
):
    """
    Downloads files from a URL, with options for retrying, unzipping, and deleting the downloaded file.

    Args:
        url (str): The URL of the file to be downloaded.
        file (str, optional): The filename of the downloaded file.
            If not provided, the file will be saved with the same name as the URL.
        dir (str, optional): The directory to save the downloaded file.
            If not provided, the file will be saved in the current working directory.
        unzip (bool, optional): Whether to unzip the downloaded file. Default: True.
        delete (bool, optional): Whether to delete the downloaded file after unzipping. Default: False.
        curl (bool, optional): Whether to use curl command line tool for downloading. Default: False.
        retry (int, optional): The number of times to retry the download in case of failure. Default: 3.
        min_bytes (float, optional): The minimum number of bytes that the downloaded file should have, to be considered
            a successful download. Default: 1E0.
        exist_ok (bool, optional): Whether to overwrite existing contents during unzipping. Defaults to False.
        progress (bool, optional): Whether to display a progress bar during the download. Default: True.

    Example:
        ```python
        from ultralytics.utils.downloads import safe_download

        link = "https://ultralytics.com/assets/bus.jpg"
        path = safe_download(link)
        ```
    """
    gdrive = url.startswith("https://drive.google.com/")  # check if the URL is a Google Drive link
    if gdrive:
        url, file = get_google_drive_file_info(url)

    f = Path(dir or ".") / (file or url2file(url))  # URL converted to filename
    if "://" not in str(url) and Path(url).is_file():  # URL exists ('://' check required in Windows Python<3.10)
        f = Path(url)  # filename
    elif not f.is_file():  # URL and file do not exist
        desc = f"Downloading {url if gdrive else clean_url(url)} to '{f}'"
        LOGGER.info(f"{desc}...")
        f.parent.mkdir(parents=True, exist_ok=True)  # make directory if missing
        check_disk_space(url)
        for i in range(retry + 1):
            try:
                if curl or i > 0:  # curl download with retry, continue
                    s = "sS" * (not progress)  # silent
                    r = subprocess.run(["curl", "-#", f"-{s}L", url, "-o", f, "--retry", "3", "-C", "-"]).returncode
                    assert r == 0, f"Curl return value {r}"
                else:  # urllib download
                    method = "torch"
                    if method == "torch":
                        torch.hub.download_url_to_file(url, f, progress=progress)
                    else:
                        with request.urlopen(url) as response, TQDM(
                            total=int(response.getheader("Content-Length", 0)),
                            desc=desc,
                            disable=not progress,
                            unit="B",
                            unit_scale=True,
                            unit_divisor=1024,
                        ) as pbar:
                            with open(f, "wb") as f_opened:
                                for data in response:
                                    f_opened.write(data)
                                    pbar.update(len(data))

                if f.exists():
                    if f.stat().st_size > min_bytes:
                        break  # success
                    f.unlink()  # remove partial downloads
            except Exception as e:
                if i == 0 and not is_online():
                    raise ConnectionError(emojis(f"โŒ  Download failure for {url}. Environment is not online.")) from e
                elif i >= retry:
                    raise ConnectionError(emojis(f"โŒ  Download failure for {url}. Retry limit reached.")) from e
                LOGGER.warning(f"โš ๏ธ Download failure, retrying {i + 1}/{retry} {url}...")

    if unzip and f.exists() and f.suffix in ("", ".zip", ".tar", ".gz"):
        from zipfile import is_zipfile

        unzip_dir = (dir or f.parent).resolve()  # unzip to dir if provided else unzip in place
        if is_zipfile(f):
            unzip_dir = unzip_file(file=f, path=unzip_dir, exist_ok=exist_ok, progress=progress)  # unzip
        elif f.suffix in (".tar", ".gz"):
            LOGGER.info(f"Unzipping {f} to {unzip_dir}...")
            subprocess.run(["tar", "xf" if f.suffix == ".tar" else "xfz", f, "--directory", unzip_dir], check=True)
        if delete:
            f.unlink()  # remove zip
        return unzip_dir



ultralytics.utils.downloads.get_github_assets(repo='ultralytics/assets', version='latest', retry=False)

์ง€์ •ํ•œ ๋ฒ„์ „์˜ ํƒœ๊ทธ์™€ ์—์…‹์„ GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„์ „์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ํ•จ์ˆ˜๋Š” ์ตœ์‹  ๋ฆด๋ฆฌ์Šค ์—์…‹์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
repo str

'์†Œ์œ ์ž/๋ฆฌํฌ์ง€ํ† ๋ฆฌ' ํ˜•์‹์˜ GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 'ultralytics/assets'์ž…๋‹ˆ๋‹ค.

'ultralytics/assets'
version str

์—์…‹์„ ๊ฐ€์ ธ์˜ฌ ๋ฆด๋ฆฌ์Šค ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ '์ตœ์‹ '์ž…๋‹ˆ๋‹ค.

'latest'
retry bool

์‹คํŒจ ์‹œ ์š”์ฒญ์„ ๋‹ค์‹œ ์‹œ๋„ํ•˜๋ ค๋ฉด ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ False์ž…๋‹ˆ๋‹ค.

False

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
tuple

๋ฆด๋ฆฌ์Šค ํƒœ๊ทธ์™€ ์—์…‹ ์ด๋ฆ„ ๋ชฉ๋ก์ด ํฌํ•จ๋œ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ
tag, assets = get_github_assets(repo='ultralytics/assets', version='latest')
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def 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.

    Args:
        repo (str, optional): The GitHub repository in the format 'owner/repo'. Defaults to 'ultralytics/assets'.
        version (str, optional): The release version to fetch assets from. Defaults to 'latest'.
        retry (bool, optional): Flag to retry the request in case of a failure. Defaults to False.

    Returns:
        (tuple): A tuple containing the release tag and a list of asset names.

    Example:
        ```python
        tag, assets = get_github_assets(repo='ultralytics/assets', version='latest')
        ```
    """

    if version != "latest":
        version = f"tags/{version}"  # i.e. tags/v6.2
    url = f"https://api.github.com/repos/{repo}/releases/{version}"
    r = requests.get(url)  # github api
    if r.status_code != 200 and r.reason != "rate limit exceeded" and retry:  # failed and not 403 rate limit exceeded
        r = requests.get(url)  # try again
    if r.status_code != 200:
        LOGGER.warning(f"โš ๏ธ GitHub assets check failure for {url}: {r.status_code} {r.reason}")
        return "", []
    data = r.json()
    return data["tag_name"], [x["name"] for x in data["assets"]]  # tag, assets i.e. ['yolov8n.pt', 'yolov8s.pt', ...]



ultralytics.utils.downloads.attempt_download_asset(file, repo='ultralytics/assets', release='v8.1.0', **kwargs)

๋กœ์ปฌ์—์„œ ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ GitHub ๋ฆด๋ฆฌ์Šค ์—์…‹์—์„œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋จผ์ € ๋กœ์ปฌ์—์„œ ํŒŒ์ผ์„ ํŒŒ์ผ์„ ๋กœ์ปฌ์—์„œ ๋จผ์ € ํ™•์ธํ•œ ๋‹ค์Œ ์ง€์ •๋œ GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๋ฆด๋ฆฌ์Šค์—์„œ ๋‹ค์šด๋กœ๋“œํ•˜๋ ค๊ณ  ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
file str | Path

๋‹ค์šด๋กœ๋“œํ•  ํŒŒ์ผ ์ด๋ฆ„ ๋˜๋Š” ํŒŒ์ผ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
repo str

'์†Œ์œ ์ž/๋ฆฌํฌ์ง€ํ† ๋ฆฌ' ํ˜•์‹์˜ GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 'ultralytics/assets'์ž…๋‹ˆ๋‹ค.

'ultralytics/assets'
release str

๋‹ค์šด๋กœ๋“œํ•  ํŠน์ • ๋ฆด๋ฆฌ์Šค ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 'v8.1.0'์ž…๋‹ˆ๋‹ค.

'v8.1.0'
**kwargs dict

๋‹ค์šด๋กœ๋“œ ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜์ž…๋‹ˆ๋‹ค.

{}

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
str

๋‹ค์šด๋กœ๋“œํ•œ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ
file_path = attempt_download_asset('yolov5s.pt', repo='ultralytics/assets', release='latest')
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def attempt_download_asset(file, repo="ultralytics/assets", release="v8.1.0", **kwargs):
    """
    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.

    Args:
        file (str | Path): The filename or file path to be downloaded.
        repo (str, optional): The GitHub repository in the format 'owner/repo'. Defaults to 'ultralytics/assets'.
        release (str, optional): The specific release version to be downloaded. Defaults to 'v8.1.0'.
        **kwargs (dict): Additional keyword arguments for the download process.

    Returns:
        (str): The path to the downloaded file.

    Example:
        ```python
        file_path = attempt_download_asset('yolov5s.pt', repo='ultralytics/assets', release='latest')
        ```
    """
    from ultralytics.utils import SETTINGS  # scoped for circular import

    # YOLOv3/5u updates
    file = str(file)
    file = checks.check_yolov5u_filename(file)
    file = Path(file.strip().replace("'", ""))
    if file.exists():
        return str(file)
    elif (SETTINGS["weights_dir"] / file).exists():
        return str(SETTINGS["weights_dir"] / file)
    else:
        # URL specified
        name = Path(parse.unquote(str(file))).name  # decode '%2F' to '/' etc.
        download_url = f"https://github.com/{repo}/releases/download"
        if str(file).startswith(("http:/", "https:/")):  # download
            url = str(file).replace(":/", "://")  # Pathlib turns :// -> :/
            file = url2file(name)  # parse authentication https://url.com/file.txt?auth...
            if Path(file).is_file():
                LOGGER.info(f"Found {clean_url(url)} locally at {file}")  # file already exists
            else:
                safe_download(url=url, file=file, min_bytes=1e5, **kwargs)

        elif repo == GITHUB_ASSETS_REPO and name in GITHUB_ASSETS_NAMES:
            safe_download(url=f"{download_url}/{release}/{name}", file=file, min_bytes=1e5, **kwargs)

        else:
            tag, assets = get_github_assets(repo, release)
            if not assets:
                tag, assets = get_github_assets(repo)  # latest release
            if name in assets:
                safe_download(url=f"{download_url}/{tag}/{name}", file=file, min_bytes=1e5, **kwargs)

        return str(file)



ultralytics.utils.downloads.download(url, dir=Path.cwd(), unzip=True, delete=False, curl=False, threads=1, retry=3, exist_ok=False)

์ง€์ •๋œ URL์—์„œ ์ง€์ •๋œ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ์ง€์ •๋œ ๊ฒฝ์šฐ ๋™์‹œ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ง€์ •๋œ ๊ฒฝ์šฐ ๋™์‹œ ๋‹ค์šด๋กœ๋“œ๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
url str | list

๋‹ค์šด๋กœ๋“œํ•  ํŒŒ์ผ์˜ URL ๋˜๋Š” URL ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
dir Path

ํŒŒ์ผ์ด ์ €์žฅ๋  ๋””๋ ‰ํ† ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ์ž…๋‹ˆ๋‹ค.

cwd()
unzip bool

๋‹ค์šด๋กœ๋“œ ํ›„ ํŒŒ์ผ์˜ ์••์ถ•์„ ํ’€๋ ค๋ฉด ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ True์ž…๋‹ˆ๋‹ค.

True
delete bool

์ถ”์ถœ ํ›„ ์••์ถ• ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๋ ค๋ฉด ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ False์ž…๋‹ˆ๋‹ค.

False
curl bool

๋‹ค์šด๋กœ๋“œ์— ์ปฌ์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ False์ž…๋‹ˆ๋‹ค.

False
threads int

๋™์‹œ ๋‹ค์šด๋กœ๋“œ์— ์‚ฌ์šฉํ•  ์Šค๋ ˆ๋“œ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 1์ž…๋‹ˆ๋‹ค.

1
retry int

๋‹ค์šด๋กœ๋“œ ์‹คํŒจ ์‹œ ์žฌ์‹œ๋„ ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 3์ž…๋‹ˆ๋‹ค.

3
exist_ok bool

์••์ถ•์„ ํ‘ธ๋Š” ๋™์•ˆ ๊ธฐ์กด ์ฝ˜ํ…์ธ ๋ฅผ ๋ฎ์–ด์“ธ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ False์ž…๋‹ˆ๋‹ค.

False
์˜ˆ์ œ
download('https://ultralytics.com/assets/example.zip', dir='path/to/dir', unzip=True)
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/downloads.py
def 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.

    Args:
        url (str | list): The URL or list of URLs of the files to be downloaded.
        dir (Path, optional): The directory where the files will be saved. Defaults to the current working directory.
        unzip (bool, optional): Flag to unzip the files after downloading. Defaults to True.
        delete (bool, optional): Flag to delete the zip files after extraction. Defaults to False.
        curl (bool, optional): Flag to use curl for downloading. Defaults to False.
        threads (int, optional): Number of threads to use for concurrent downloads. Defaults to 1.
        retry (int, optional): Number of retries in case of download failure. Defaults to 3.
        exist_ok (bool, optional): Whether to overwrite existing contents during unzipping. Defaults to False.

    Example:
        ```python
        download('https://ultralytics.com/assets/example.zip', dir='path/to/dir', unzip=True)
        ```
    """
    dir = Path(dir)
    dir.mkdir(parents=True, exist_ok=True)  # make directory
    if threads > 1:
        with ThreadPool(threads) as pool:
            pool.map(
                lambda x: safe_download(
                    url=x[0],
                    dir=x[1],
                    unzip=unzip,
                    delete=delete,
                    curl=curl,
                    retry=retry,
                    exist_ok=exist_ok,
                    progress=threads <= 1,
                ),
                zip(url, repeat(dir)),
            )
            pool.close()
            pool.join()
    else:
        for u in [url] if isinstance(url, (str, Path)) else url:
            safe_download(url=u, dir=dir, unzip=unzip, delete=delete, curl=curl, retry=retry, exist_ok=exist_ok)





2023-11-12 ์ƒ์„ฑ, 2023-11-25 ์—…๋ฐ์ดํŠธ๋จ
์ž‘์„ฑ์ž: glenn-jocher (3), Laughing-q (1)