162 lines
5.0 KiB
Python
162 lines
5.0 KiB
Python
import copy
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
|
|
def decode_image(img_path):
|
|
if isinstance(img_path, str):
|
|
with open(img_path, "rb") as f:
|
|
im_read = f.read()
|
|
data = np.frombuffer(im_read, dtype="uint8")
|
|
else:
|
|
assert isinstance(img_path, np.ndarray)
|
|
data = img_path
|
|
|
|
im = cv2.imdecode(data, 1) # BGR mode, but need RGB mode
|
|
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
|
img_info = {
|
|
"im_shape": np.array(im.shape[:2], dtype=np.float32),
|
|
"scale_factor": np.array([1.0, 1.0], dtype=np.float32),
|
|
}
|
|
return im, img_info
|
|
|
|
|
|
class Resize(object):
|
|
"""resize image by target_size and max_size
|
|
Args:
|
|
target_size (int): the target size of image
|
|
keep_ratio (bool): whether keep_ratio or not, default true
|
|
interp (int): method of resize
|
|
"""
|
|
|
|
def __init__(self, target_size, keep_ratio=True, interp=cv2.INTER_LINEAR):
|
|
if isinstance(target_size, int):
|
|
target_size = [target_size, target_size]
|
|
self.target_size = target_size
|
|
self.keep_ratio = keep_ratio
|
|
self.interp = interp
|
|
|
|
def __call__(self, im, im_info):
|
|
"""
|
|
Args:
|
|
im (np.ndarray): image (np.ndarray)
|
|
im_info (dict): info of image
|
|
Returns:
|
|
im (np.ndarray): processed image (np.ndarray)
|
|
im_info (dict): info of processed image
|
|
"""
|
|
assert len(self.target_size) == 2
|
|
assert self.target_size[0] > 0 and self.target_size[1] > 0
|
|
im_channel = im.shape[2]
|
|
im_scale_y, im_scale_x = self.generate_scale(im)
|
|
im = cv2.resize(im, None, None, fx=im_scale_x, fy=im_scale_y, interpolation=self.interp)
|
|
im_info["im_shape"] = np.array(im.shape[:2]).astype("float32")
|
|
im_info["scale_factor"] = np.array([im_scale_y, im_scale_x]).astype("float32")
|
|
return im, im_info
|
|
|
|
def generate_scale(self, im):
|
|
"""
|
|
Args:
|
|
im (np.ndarray): image (np.ndarray)
|
|
Returns:
|
|
im_scale_x: the resize ratio of X
|
|
im_scale_y: the resize ratio of Y
|
|
"""
|
|
origin_shape = im.shape[:2]
|
|
im_c = im.shape[2]
|
|
if self.keep_ratio:
|
|
im_size_min = np.min(origin_shape)
|
|
im_size_max = np.max(origin_shape)
|
|
target_size_min = np.min(self.target_size)
|
|
target_size_max = np.max(self.target_size)
|
|
im_scale = float(target_size_min) / float(im_size_min)
|
|
if np.round(im_scale * im_size_max) > target_size_max:
|
|
im_scale = float(target_size_max) / float(im_size_max)
|
|
im_scale_x = im_scale
|
|
im_scale_y = im_scale
|
|
else:
|
|
resize_h, resize_w = self.target_size
|
|
im_scale_y = resize_h / float(origin_shape[0])
|
|
im_scale_x = resize_w / float(origin_shape[1])
|
|
return im_scale_y, im_scale_x
|
|
|
|
|
|
class NormalizeImage(object):
|
|
"""normalize image
|
|
Args:
|
|
mean (list): im - mean
|
|
std (list): im / std
|
|
is_scale (bool): whether need im / 255
|
|
norm_type (str): type in ['mean_std', 'none']
|
|
"""
|
|
|
|
def __init__(self, mean, std, is_scale=True, norm_type="mean_std"):
|
|
self.mean = mean
|
|
self.std = std
|
|
self.is_scale = is_scale
|
|
self.norm_type = norm_type
|
|
|
|
def __call__(self, im, im_info):
|
|
"""
|
|
Args:
|
|
im (np.ndarray): image (np.ndarray)
|
|
im_info (dict): info of image
|
|
Returns:
|
|
im (np.ndarray): processed image (np.ndarray)
|
|
im_info (dict): info of processed image
|
|
"""
|
|
im = im.astype(np.float32, copy=False)
|
|
if self.is_scale:
|
|
scale = 1.0 / 255.0
|
|
im *= scale
|
|
|
|
if self.norm_type == "mean_std":
|
|
mean = np.array(self.mean)[np.newaxis, np.newaxis, :]
|
|
std = np.array(self.std)[np.newaxis, np.newaxis, :]
|
|
im -= mean
|
|
im /= std
|
|
return im, im_info
|
|
|
|
|
|
class Permute(object):
|
|
"""permute image
|
|
Args:
|
|
to_bgr (bool): whether convert RGB to BGR
|
|
channel_first (bool): whether convert HWC to CHW
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
):
|
|
super(Permute, self).__init__()
|
|
|
|
def __call__(self, im, im_info):
|
|
"""
|
|
Args:
|
|
im (np.ndarray): image (np.ndarray)
|
|
im_info (dict): info of image
|
|
Returns:
|
|
im (np.ndarray): processed image (np.ndarray)
|
|
im_info (dict): info of processed image
|
|
"""
|
|
im = im.transpose((2, 0, 1)).copy()
|
|
return im, im_info
|
|
|
|
|
|
class Compose:
|
|
def __init__(self, transforms):
|
|
self.transforms = []
|
|
for op_info in transforms:
|
|
new_op_info = op_info.copy()
|
|
op_type = new_op_info.pop("type")
|
|
self.transforms.append(eval(op_type)(**new_op_info))
|
|
|
|
def __call__(self, img_path):
|
|
img, im_info = decode_image(img_path)
|
|
for t in self.transforms:
|
|
img, im_info = t(img, im_info)
|
|
inputs = copy.deepcopy(im_info)
|
|
inputs["image"] = img
|
|
return inputs
|