コンテンツへスキップ

参考 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

ファイルが書き込まれた場合は真、そうでない場合は偽。

ソースコード 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ではシリアライズされないラムダ関数をdillでシリアライズすることができる。 保存に失敗した場合の指数関数的スタンドオフ。

パラメーター

名称 タイプ 説明 デフォルト
*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)