跳至内容

参考资料 ultralytics/utils/patches.py

备注

该文件可从https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/utils/patches .py 获取。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!



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

从文件中读取图像

参数

名称 类型 说明 默认值
filename str

要读取文件的路径。

所需
flags int

可以使用 cv2.IMREAD_* 值的标志。默认为 cv2.IMREAD_COLOR。

IMREAD_COLOR

返回:

类型 说明
ndarray

读取图像。

源代码 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)

将图像写入文件

参数

名称 类型 说明 默认值
filename str

要写入文件的路径。

所需
img ndarray

图片来写。

所需
params list of ints

附加参数。请参见 OpenCV 文档。

None

返回:

类型 说明
bool

如果文件已写入,则为 True,否则为 False。

源代码 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)

在指定窗口中显示图像。

参数

名称 类型 说明 默认值
winname str

窗口名称。

所需
mat ndarray

显示图像。

所需
源代码 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)

使用 dill(如果存在)对 lambda 函数进行序列化,而 pickle 不会这样做。还增加了 3 次重试 指数级间隔重试,以提高对瞬态问题的稳健性。

参数

名称 类型 说明 默认值
*args tuple

传递给torch.save 的位置参数。

()
**kwargs dict

要传递给torch.save 的关键字参数。

{}
源代码 ultralytics/utils/patches.py
def torch_save(*args, **kwargs):
    """
    Use dill (if exists) to serialize the lambda functions where pickle does not do this. Also adds 3 retries with
    exponential standoff in case of save failure to improve robustness to transient issues.

    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

    for i in range(4):  # 3 retries
        try:
            return _torch_save(*args, **kwargs)
        except RuntimeError:  # unable to save, possibly waiting for device to flush or anti-virus to finish scanning
            if i == 3:
                raise
            time.sleep((2**i) / 2)  # exponential standoff 0.5s, 1.0s, 2.0s





创建于 2023-11-12,更新于 2023-11-25
作者:glenn-jocher(3),Laughing-q(1)