跳至内容

参考资料 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, use_dill=True, **kwargs)

在 pickle 不能序列化 lambda 函数的情况下,可选择使用 dill 来序列化 lambda 函数。 的稳健性,在保存失败时可进行 3 次重试和指数级延迟。

参数

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

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

()
use_dill bool

是否在可用的情况下尝试使用 dill 进行序列化。默认为 True。

True
**kwargs any

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

{}
源代码 ultralytics/utils/patches.py
def torch_save(*args, use_dill=True, **kwargs):
    """
    Optionally use dill to serialize lambda functions where pickle does not, adding robustness with 3 retries and
    exponential standoff in case of save failure.

    Args:
        *args (tuple): Positional arguments to pass to torch.save.
        use_dill (bool): Whether to try using dill for serialization if available. Defaults to True.
        **kwargs (any): Keyword arguments to pass to torch.save.
    """
    try:
        assert use_dill
        import dill as pickle
    except (AssertionError, ImportError):
        import pickle

    if "pickle_module" not in kwargs:
        kwargs["pickle_module"] = pickle

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





创建于 2023-11-12,更新于 2024-05-08
作者:Burhan-Q(1)、glenn-jocher(3)、Laughing-q(1)