Bỏ để qua phần nội dung

Tài liệu tham khảo cho ultralytics/utils/patches.py

Ghi

Tệp này có sẵn tại https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/patches.py. Nếu bạn phát hiện ra một vấn đề, vui lòng giúp khắc phục nó bằng cách đóng góp Yêu cầu 🛠️ kéo. Cảm ơn bạn 🙏 !



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

Đọc hình ảnh từ một tệp.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
filename str

Đường dẫn đến tệp để đọc.

bắt buộc
flags int

Gắn cờ có thể lấy giá trị của cv2. IMREAD_*. Mặc định là cv2. IMREAD_COLOR.

IMREAD_COLOR

Trở lại:

Kiểu Sự miêu tả
ndarray

Hình ảnh đọc.

Mã nguồn trong 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)

Ghi hình ảnh vào tệp.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
filename str

Đường dẫn đến tệp để ghi.

bắt buộc
img ndarray

Hình ảnh để viết.

bắt buộc
params list of ints

Thông số bổ sung. Xem tài liệu OpenCV.

None

Trở lại:

Kiểu Sự miêu tả
bool

True nếu tập tin đã được viết, False nếu không.

Mã nguồn trong 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)

Hiển thị một hình ảnh trong cửa sổ được chỉ định.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
winname str

Tên của cửa sổ.

bắt buộc
mat ndarray

Hình ảnh sẽ được hiển thị.

bắt buộc
Mã nguồn trong 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)

Tùy chọn sử dụng thì là để tuần tự hóa các hàm lambda trong khi dưa chua không có, tăng thêm độ chắc chắn với 3 lần thử lại và bế tắc theo cấp số nhân trong trường hợp lưu thất bại.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
*args tuple

Đối số vị trí để chuyển đến torch.cứu.

()
use_dill bool

Có nên thử sử dụng thì là để lập số sê-ri hóa nếu có hay không. Mặc định là True.

True
**kwargs any

Đối số từ khóa cần chuyển đến torch.cứu.

{}
Mã nguồn trong 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





Đã tạo 2023-11-12, Cập nhật 2024-05-08
Tác giả: Burhan-Q (1), glenn-jocher (3), Laughing-q (1)