|
|
|
"""Monkey patches to update/extend functionality of existing functions.""" |
|
|
|
import time |
|
from pathlib import Path |
|
|
|
import cv2 |
|
import numpy as np |
|
import torch |
|
|
|
|
|
_imshow = cv2.imshow |
|
|
|
|
|
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) |
|
|
|
|
|
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 |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
_torch_save = torch.save |
|
|
|
|
|
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): |
|
try: |
|
return _torch_save(*args, **kwargs) |
|
except RuntimeError as e: |
|
if i == 3: |
|
raise e |
|
time.sleep((2**i) / 2) |
|
|