def rcnn_resize(im, target_size, max_size, stride=0):
    """
    only resize input image to target size and return scale
    :param im: BGR image input by opencv
    :param target_size: one dimensional size (the short side)
    :param max_size: one dimensional max size (the long side)
    :param stride: if given, pad the image to designated stride
    :return:
    """
    im_shape = im.shape
    im_size_min = np.min(im_shape[0:2])
    im_size_max = np.max(im_shape[0:2])
    im_scale = float(target_size) / float(im_size_min)
    # prevent bigger axis from being more than max_size:
    if np.round(im_scale * im_size_max) > max_size:
        im_scale = float(max_size) / float(im_size_max)
    im = image.resize(im,
                      int(im.shape[1] * float(im_scale)),
                      int(im.shape[0] * float(im_scale)),
                      interp=1)

    if stride == 0:
        return im, im_scale
    else:
        # pad to product of stride
        im_height = int(np.ceil(im.shape[0] / float(stride)) * stride)
        im_width = int(np.ceil(im.shape[1] / float(stride)) * stride)
        im_channel = im.shape[2]
        padded_im = np.zeros((im_height, im_width, im_channel))
        padded_im[:im.shape[0], :im.shape[1], :] = im
        return padded_im, im_scale
Exemplo n.º 2
0
 def _preprocess(self, data):
     input_shape = self.signature['inputs'][0]['data_shape']
     height, width = input_shape[2:]
     img_arr = image.read(data[0])
     img_arr = image.resize(img_arr, width, height)
     img_arr = image.color_normalize(img_arr, nd.array([127.5]),
                                     nd.array([127.5]))
     img_arr = image.transform_shape(img_arr)
     return [img_arr]
Exemplo n.º 3
0
 def _preprocess(self, data):
     img_list = []
     for idx, img in enumerate(data):
         input_shape = self.signature['inputs'][idx]['data_shape']
         # We are assuming input shape is NCHW
         [h, w] = input_shape[2:]
         img_arr = image.read(img)
         img_arr = image.resize(img_arr, w, h)
         img_arr = image.transform_shape(img_arr)
         img_list.append(img_arr)
     return img_list
Exemplo n.º 4
0
 def _preprocess(self, data):
     img_list = []
     for idx, img in enumerate(data):
         input_shape = self.signature['inputs'][idx][
             'data_shape']  # Input shape is NCHW
         [h, w] = input_shape[2:]
         img_arr = image.read(
             img, 0)  #Set flag to 0 for reading grayscale images
         img_arr = image.resize(img_arr, w, h)
         img_arr = image.transform_shape(img_arr)
         img_list.append(img_arr)
     return img_list
Exemplo n.º 5
0
 def _preprocess(self, data):
     img_list = []
     for idx, img in enumerate(data):
         input_shape = self.signature['inputs'][idx]['data_shape']
         # We are assuming input shape is NCHW
         [h, w] = input_shape[2:]
         if input_shape[1] == 1:
             img_arr = image.read(img, 0)
         else:
             img_arr = image.read(img)
         img_arr = image.resize(img_arr, w, h)
         rgb_mean = mx.nd.array([123.68, 116.779, 103.939])
         rgb_std = mx.nd.array([58.395, 57.12, 57.375])
         img_arr = img_arr.astype('float32')
         img_arr = mx.image.color_normalize(img_arr,
                                            mean=rgb_mean,
                                            std=rgb_std)
         img_arr = mx.nd.reshape(img_arr, (w, h, input_shape[1]))
         img_arr = image.transform_shape(img_arr)
         img_list.append(img_arr)
     return img_list
 def test_resize(self):
     input1 = mx.nd.random.uniform(0, 255, shape=(245, 156, 3))
     output1 = image.resize(input1, 128, 256)
     assert output1.shape == (256, 128, 3), "Resize method failed. Got %s shape." % (str(output1.shape))