Skip to content

Reference for ultralytics/utils/patches.py

Note

Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/patches.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!


ultralytics.utils.patches.imread(filename, flags=cv2.IMREAD_COLOR)

Read an image from a file.

Parameters:

Name Type Description Default
filename str

Path to the file to read.

required
flags int

Flag that can take values of cv2.IMREAD_*. Defaults to cv2.IMREAD_COLOR.

IMREAD_COLOR

Returns:

Type Description
ndarray

The read image.

Source code in ultralytics/utils/patches.py
def imread(filename: str, flags: int = cv2.IMREAD_COLOR):
    """Read an image from a file.

    Args:
        filename (str): Path to the file to read.
        flags (int, optional): Flag that can take values of cv2.IMREAD_*. Defaults to cv2.IMREAD_COLOR.

    Returns:
        (np.ndarray): The read image.
    """
    return cv2.imdecode(np.fromfile(filename, np.uint8), flags)




ultralytics.utils.patches.imwrite(filename, img, params=None)

Write an image to a file.

Parameters:

Name Type Description Default
filename str

Path to the file to write.

required
img ndarray

Image to write.

required
params list of ints

Additional parameters. See OpenCV documentation.

None

Returns:

Type Description
bool

True if the file was written, False otherwise.

Source code in ultralytics/utils/patches.py
def imwrite(filename: str, img: np.ndarray, params=None):
    """Write an image to a file.

    Args:
        filename (str): Path to the file to write.
        img (np.ndarray): Image to write.
        params (list of ints, optional): Additional parameters. See OpenCV documentation.

    Returns:
        (bool): True if the file was written, False otherwise.
    """
    try:
        cv2.imencode(Path(filename).suffix, img, params)[1].tofile(filename)
        return True
    except Exception:
        return False




ultralytics.utils.patches.imshow(winname, mat)

Displays an image in the specified window.

Parameters:

Name Type Description Default
winname str

Name of the window.

required
mat ndarray

Image to be shown.

required
Source code in ultralytics/utils/patches.py
def imshow(winname: str, mat: np.ndarray):
    """Displays an image in the specified window.

    Args:
        winname (str): Name of the window.
        mat (np.ndarray): Image to be shown.
    """
    _imshow(winname.encode('unicode_escape').decode(), mat)




ultralytics.utils.patches.torch_save(*args, **kwargs)

Use dill (if exists) to serialize the lambda functions where pickle does not do this.

Parameters:

Name Type Description Default
*args tuple

Positional arguments to pass to torch.save.

()
**kwargs dict

Keyword arguments to pass to torch.save.

{}
Source code in ultralytics/utils/patches.py
def torch_save(*args, **kwargs):
    """Use dill (if exists) to serialize the lambda functions where pickle does not do this.

    Args:
        *args (tuple): Positional arguments to pass to torch.save.
        **kwargs (dict): Keyword arguments to pass to torch.save.
    """
    try:
        import dill as pickle  # noqa
    except ImportError:
        import pickle

    if 'pickle_module' not in kwargs:
        kwargs['pickle_module'] = pickle  # noqa
    return _torch_save(*args, **kwargs)




Created 2023-07-16, Updated 2023-08-07
Authors: glenn-jocher (5), Laughing-q (1)