انتقل إلى المحتوى

مرجع ل ultralytics/utils/patches.py

ملاحظه

هذا الملف متاح في https://github.com/ultralytics/ultralytics/ نقطة / الرئيسية /ultralytics/ المرافق / patches.py. إذا اكتشفت مشكلة ، فيرجى المساعدة في إصلاحها من خلال المساهمة في طلب 🛠️ سحب. شكرا لك 🙏!



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)

اختياريا ، استخدم الشبت لإجراء تسلسل لوظائف lambda حيث لا يفعل المخلل ، مما يضيف المتانة مع 3 عمليات إعادة محاولة و المواجهة الأسية في حالة فشل الحفظ.

البارامترات:

اسم نوع وصف افتراضي
*args tuple

الحجج الموضعية لتمريرها torch.أنقذ.

()
use_dill bool

ما إذا كنت تريد محاولة استخدام الشبت للتسلسل إذا كان ذلك متاحا. الإعدادات الافتراضية إلى صواب.

True
**kwargs any

وسيطات الكلمات الأساسية المراد تمريرها إليها torch.أنقذ.

{}
شفرة المصدر في 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
المؤلفون: برهان-ق (1)، جلين-جوتشر (3)، الضحك-ق (1)