f"The backend of Resize only support \"cv2\" or \"PIL\". \"f{backend}\" is unavailable. Use \"cv2\" instead."
)
self.resize_func=cv2.resize
def__call__(self,src,size):
returnself.resize_func(src,size)
classOperatorParamError(ValueError):
""" OperatorParamError
"""
pass
classResizeImage(object):
""" resize image """
def__init__(self,
size=None,
resize_short=None,
interpolation=None,
backend="cv2"):
ifresize_shortisnotNoneandresize_short>0:
self.resize_short=resize_short
self.w=None
self.h=None
elifsizeisnotNone:
self.resize_short=None
self.w=sizeiftype(size)isintelsesize[0]
self.h=sizeiftype(size)isintelsesize[1]
else:
raiseOperatorParamError("invalid params for ReisizeImage for '\
'both 'size' and 'resize_short' are None")
self._resize_func=UnifiedResize(
interpolation=interpolation,backend=backend)
def__call__(self,img):
img_h,img_w=img.shape[:2]
ifself.resize_shortisnotNone:
percent=float(self.resize_short)/min(img_w,img_h)
w=int(round(img_w*percent))
h=int(round(img_h*percent))
else:
w=self.w
h=self.h
returnself._resize_func(img,(w,h))
classCropImage(object):
""" crop image """
def__init__(self,size):
iftype(size)isint:
self.size=(size,size)
else:
self.size=size# (h, w)
def__call__(self,img):
w,h=self.size
img_h,img_w=img.shape[:2]
ifimg_h<horimg_w<w:
raiseException(
f"The size({h}, {w}) of CropImage must be greater than size({img_h}, {img_w}) of image. Please check image original size and size of ResizeImage if used."
)
w_start=(img_w-w)//2
h_start=(img_h-h)//2
w_end=w_start+w
h_end=h_start+h
returnimg[h_start:h_end,w_start:w_end,:]
classNormalizeImage(object):
""" normalize image such as substract mean, divide std
"""
def__init__(self,
scale=None,
mean=None,
std=None,
order='chw',
output_fp16=False,
channel_num=3):
ifisinstance(scale,str):
scale=eval(scale)
assertchannel_numin[
3,4
],"channel number of input image should be set to 3 or 4."