Esempio n. 1
0
    def _image(path_or_bytes):

        # im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        # The OpenCV cannot handle gif format images, it will return None.
        # if im is None:
        path_or_stream = io.BytesIO(path_or_bytes) if isinstance(
            path_or_bytes, bytes) else path_or_bytes
        pil_image = PIL.Image.open(path_or_stream)
        rgb = pil_image.split()
        size = pil_image.size

        if len(rgb) > 3 and REPLACE_TRANSPARENT:
            background = PIL.Image.new('RGB', pil_image.size, (255, 255, 255))
            background.paste(pil_image, (0, 0, size[0], size[1]), pil_image)
            pil_image = background

        if IMAGE_CHANNEL == 1:
            pil_image = pil_image.convert('L')

        im = np.array(pil_image)
        im = preprocessing(im, BINARYZATION, SMOOTH, BLUR).astype(np.float32)
        im = cv2.resize(im, (RESIZE[0], RESIZE[1]))
        im = im.swapaxes(0, 1)
        return np.array(
            (im[:, :, np.newaxis] if IMAGE_CHANNEL == 1 else im[:, :]) / 255.)
Esempio n. 2
0
    def load_image(image_bytes):
        data_stream = io.BytesIO(image_bytes)
        pil_image = PIL_Image.open(data_stream)
        rgb = pil_image.split()
        size = pil_image.size

        if len(rgb) > 3 and REPLACE_TRANSPARENT:
            background = PIL_Image.new('RGB', pil_image.size, (255, 255, 255))
            background.paste(pil_image, (0, 0, size[0], size[1]), pil_image)
            pil_image = background

        if IMAGE_CHANNEL == 1:
            pil_image = pil_image.convert('L')

        im = np.array(pil_image)
        im = preprocessing(im, BINARYZATION, SMOOTH, BLUR).astype(np.float32)
        if RESIZE[0] == -1:
            ratio = RESIZE[1] / size[1]
            resize_width = int(ratio * size[0])
            im = cv2.resize(im, (resize_width, RESIZE[1]))
        else:
            im = cv2.resize(im, (RESIZE[0], RESIZE[1]))
        im = im.swapaxes(0, 1)
        return (im[:, :,
                   np.newaxis] if IMAGE_CHANNEL == 1 else im[:, :]) / 255.
Esempio n. 3
0
        def load_image(image_bytes):
            data_stream = io.BytesIO(image_bytes)
            pil_image = PIL_Image.open(data_stream)
            rgb = pil_image.split()
            size = pil_image.size

            if len(rgb) > 3 and model.replace_transparent:
                background = PIL_Image.new('RGB', pil_image.size,
                                           (255, 255, 255))
                background.paste(pil_image, (0, 0, size[0], size[1]),
                                 pil_image)
                pil_image = background

            if model.image_channel == 1:
                pil_image = pil_image.convert('L')

            # image = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2GRAY)
            image = preprocessing(np.asarray(pil_image), model.binaryzation,
                                  model.smooth, model.blur).astype(np.float32)
            if model.resize[0] == -1:
                ratio = model.resize[1] / size[1]
                resize_width = int(ratio * size[0])
                image = cv2.resize(image, (resize_width, model.resize[1]))
            else:
                image = cv2.resize(image, (model.resize[0], model.resize[1]))
            image = image.swapaxes(0, 1)
            return (image[:, :, np.newaxis]
                    if model.image_channel == 1 else image[:, :]) / 255.
Esempio n. 4
0
        def load_image(image_bytes):
            if isinstance(image_bytes, bytes):
                nparr = np.fromstring(image_bytes, np.uint8)
                im = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
            else:
                im = cv2.imread(image_bytes, cv2.IMREAD_GRAYSCALE)
            # The OpenCV cannot handle gif format images, it will return None.
            if im is None:
                data_stream = io.BytesIO(image_bytes)
                pil_image = PIL_Image.open(data_stream)
                rgb = pil_image.split()
                size = pil_image.size

                if len(rgb) > 3 and model.replace_transparent:
                    background = PIL_Image.new('RGB', pil_image.size,
                                               (255, 255, 255))
                    background.paste(pil_image, (0, 0, size[0], size[1]),
                                     pil_image)
                    pil_image = background

                if model.image_channel == 1:
                    pil_image = pil_image.convert('L')
                im = np.array(pil_image)
            image = preprocessing(im, model.gamma, model.binaryzation,
                                  model.smooth, model.blur).astype(np.float32)
            if model.resize[0] == -1:
                ratio = model.resize[1] / size[1]
                resize_width = int(ratio * size[0])
                image = cv2.resize(image, (resize_width, model.resize[1]))
            else:
                image = cv2.resize(image, (model.resize[0], model.resize[1]))
            image = image.swapaxes(0, 1)
            return (image[:, :, np.newaxis]
                    if model.image_channel == 1 else image[:, :]) / 255.
Esempio n. 5
0
        def load_image(image_bytes):
            data_stream = io.BytesIO(image_bytes)
            pil_image = PIL_Image.open(data_stream)
            if model.pre_concat_frames == -1 and model.pre_concat_frames == -1:
                pil_image = pil_image.convert('RGB')

            # if pil_image.mode == 'P':
            #     pil_image = pil_image.convert('RGB')

            rgb = pil_image.split()
            size = pil_image.size

            gif_handle = model.pre_concat_frames != -1 or model.pre_blend_frames != -1

            if (len(rgb) > 3
                    and model.pre_replace_transparent) and not gif_handle:
                background = PIL_Image.new('RGB', pil_image.size,
                                           (255, 255, 255))
                background.paste(pil_image, (0, 0, size[0], size[1]),
                                 pil_image)
                pil_image = background

            if model.pre_concat_frames != -1:
                im = concat_frames(pil_image, model.pre_concat_frames)
            elif model.pre_blend_frames != -1:
                im = blend_frame(pil_image, model.pre_blend_frames)
            else:
                im = np.asarray(pil_image)

            if extract_rgb:
                im = rgb_filter(im, extract_rgb)

            im = preprocessing_by_func(exec_map=model.exec_map,
                                       key=param_key,
                                       src_arr=im)

            if model.image_channel == 1 and len(im.shape) == 3:
                im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)

            im = preprocessing(
                image=im,
                binaryzation=model.pre_binaryzation,
            )

            if model.pre_horizontal_stitching:
                up_slice = im[0:int(size[1] / 2), 0:size[0]]
                down_slice = im[int(size[1] / 2):size[1], 0:size[0]]
                im = np.concatenate((up_slice, down_slice), axis=1)

            image = im.astype(np.float32)
            if model.resize[0] == -1:
                ratio = model.resize[1] / size[1]
                resize_width = int(ratio * size[0])
                image = cv2.resize(image, (resize_width, model.resize[1]))
            else:
                image = cv2.resize(image, (model.resize[0], model.resize[1]))
            image = image.swapaxes(0, 1)
            return (image[:, :, np.newaxis]
                    if model.image_channel == 1 else image[:, :]) / 255.
Esempio n. 6
0
 def load_image(image_bytes):
     data_stream = io.BytesIO(image_bytes)
     pil_image = PIL_Image.open(data_stream).convert('RGB')
     image = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2GRAY)
     image = preprocessing(image, model.binaryzation, model.smooth,
                           model.blur).astype(np.float32)
     image = cv2.resize(image, (model.resize[0], model.resize[1]))
     image = image.swapaxes(0, 1)
     return image[:, :, np.newaxis] / 255.
Esempio n. 7
0
def _image(path):

    im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    if im is None:
        pil_image = PIL.Image.open(path).convert("RGB")
        im = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2GRAY)
    im = preprocessing(im, BINARYZATION, SMOOTH, BLUR)
    im = cv2.resize(im, (IMAGE_WIDTH, IMAGE_HEIGHT))
    return im
Esempio n. 8
0
    def image(self, path_or_bytes):
        """针对图片类型的输入的编码"""
        # im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        # The OpenCV cannot handle gif format images, it will return None.
        # if im is None:
        path_or_stream = io.BytesIO(path_or_bytes) if isinstance(
            path_or_bytes, bytes) else path_or_bytes
        pil_image = PIL.Image.open(path_or_stream)
        rgb = pil_image.split()
        if len(rgb) == 1 and self.model_conf.image_channel == 3:
            return "The number of image channels {} is inconsistent with the number of configured channels {}.".format(
                len(rgb), self.model_conf.image_channel)

        size = pil_image.size

        if len(rgb) > 3 and self.model_conf.replace_transparent:
            background = PIL.Image.new('RGBA', pil_image.size, (255, 255, 255))
            background.paste(pil_image, (0, 0, size[0], size[1]), pil_image)
            background.convert('RGB')
            pil_image = background

        if self.model_conf.image_channel == 1:
            pil_image = pil_image.convert('L')

        im = np.array(pil_image)

        if self.mode == RunMode.Trains and bool(random.getrandbits(1)):
            im = preprocessing(
                image=im,
                binaryzation=self.model_conf.binaryzation,
                median_blur=self.model_conf.median_blur,
                gaussian_blur=self.model_conf.gaussian_blur,
                equalize_hist=self.model_conf.equalize_hist,
                laplacian=self.model_conf.laplace,
                rotate=self.model_conf.rotate,
                warp_perspective=self.model_conf.warp_perspective,
                sp_noise=self.model_conf.sp_noise,
            ).astype(np.float32)

        else:
            im = im.astype(np.float32)
        if self.model_conf.resize[0] == -1:
            # random_ratio = random.choice([2.5, 3, 3.5, 3.2, 2.7, 2.75])
            ratio = self.model_conf.resize[1] / size[1]
            # random_width = int(random_ratio * RESIZE[1])
            resize_width = int(ratio * size[0])
            # resize_width = random_width if is_random else resize_width
            im = cv2.resize(im, (resize_width, self.model_conf.resize[1]))
        else:
            im = cv2.resize(
                im, (self.model_conf.resize[0], self.model_conf.resize[1]))
        im = im.swapaxes(0, 1)

        if self.model_conf.image_channel == 1:
            return np.array((im[:, :, np.newaxis]) / 255.)
        else:
            return np.array(im[:, :]) / 255.
Esempio n. 9
0
    def _image(path):

        im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        # The OpenCV cannot handle gif format images, it will return None.
        if im is None:
            pil_image = PIL.Image.open(path).convert("RGB")
            im = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2GRAY)
        im = preprocessing(im, BINARYZATION, SMOOTH, BLUR).astype(np.float32) / 255.
        im = cv2.resize(im, (IMAGE_WIDTH, IMAGE_HEIGHT))
        return np.reshape(im, [IMAGE_HEIGHT, IMAGE_WIDTH, 1])
Esempio n. 10
0
    def _image(path):

        # im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        # The OpenCV cannot handle gif format images, it will return None.
        # if im is None:
        pil_image = PIL.Image.open(path).convert("RGB")
        im = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2GRAY)
        im = preprocessing(im, BINARYZATION, SMOOTH, BLUR).astype(np.float32)
        im = cv2.resize(im, (RESIZE[0], RESIZE[1]))
        im = im.swapaxes(0, 1)
        return np.array(im[:, :, np.newaxis] / 255.)
Esempio n. 11
0
        def load_image(image_bytes, color=None):

            if color and color in ['red', 'blue', 'black', 'green', 'yellow']:
                image = np.asarray(bytearray(image_bytes), dtype="uint8")
                image = cv2.imdecode(image, -1)
                image = separate_color(image, color)
            else:
                data_stream = io.BytesIO(image_bytes)
                pil_image = PIL_Image.open(data_stream).convert('RGB')
                image = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2GRAY)
            image = preprocessing(image, model.binaryzation, model.smooth,
                                  model.blur).astype(np.float32)
            image = cv2.resize(image, (model.resize[0], model.resize[1]))
            image = image.swapaxes(0, 1)
            return image[:, :, np.newaxis] / 255.
Esempio n. 12
0
        def load_image(image_bytes):
            data_stream = io.BytesIO(image_bytes)
            pil_image = PIL_Image.open(data_stream)
            rgb = pil_image.split()
            size = pil_image.size

            gif_handle = model.pre_concat_frames != -1 or model.pre_blend_frames != -1

            if len(rgb) > 3 and model.pre_replace_transparent and gif_handle:
                background = PIL_Image.new('RGB', pil_image.size,
                                           (255, 255, 255))
                background.paste(pil_image, (0, 0, size[0], size[1]),
                                 pil_image)
                pil_image = background

            if model.pre_concat_frames != -1:
                im = concat_frames(pil_image, model.pre_concat_frames)
            elif model.pre_blend_frames != -1:
                im = blend_frame(pil_image, model.pre_blend_frames)
            else:
                im = np.array(pil_image)

            if model.image_channel == 1 and len(im.shape) == 3:
                im = im.mean(axis=2).astype(np.float32)

            im = preprocessing(
                image=im,
                binaryzation=model.pre_binaryzation,
            )

            if model.pre_horizontal_stitching:
                up_slice = im[0:int(size[1] / 2), 0:size[0]]
                down_slice = im[int(size[1] / 2):size[1], 0:size[0]]
                im = np.concatenate((up_slice, down_slice), axis=1)

            image = im.astype(np.float32)
            if model.resize[0] == -1:
                ratio = model.resize[1] / size[1]
                resize_width = int(ratio * size[0])
                image = cv2.resize(image, (resize_width, model.resize[1]))
            else:
                image = cv2.resize(image, (model.resize[0], model.resize[1]))
            image = image.swapaxes(0, 1)
            return (image[:, :, np.newaxis]
                    if model.image_channel == 1 else image[:, :]) / 255.
Esempio n. 13
0
        def load_image(image_bytes):
            data_stream = io.BytesIO(image_bytes)
            pil_image = PIL_Image.open(data_stream)
            rgb = pil_image.split()
            size = pil_image.size

            if len(rgb) > 3 and model.replace_transparent:
                background = PIL_Image.new('RGB', pil_image.size,
                                           (255, 255, 255))
                background.paste(pil_image, (0, 0, size[0], size[1]),
                                 pil_image)
                pil_image = background

            if model.image_channel == 1:
                pil_image = pil_image.convert('L')

            im = np.asarray(pil_image)
            if model.horizontal_stitching:
                up_slice = im[0:int(size[1] / 2), 0:size[0]]
                down_slice = im[int(size[1] / 2):size[1], 0:size[0]]
                im = np.concatenate((up_slice, down_slice), axis=1)
            # image = cv2.cvtColor(np.asarray(pil_image), cv2.COLOR_RGB2GRAY)
            image = preprocessing(im, model.binaryzation, model.smooth,
                                  model.blur).astype(np.float32)

            if model.resize[0] == -1:
                ratio = model.resize[1] / size[1]
                resize_width = int(ratio * size[0])
                image = cv2.resize(image, (resize_width, model.resize[1]))
            else:
                image = cv2.resize(image, (model.resize[0], model.resize[1]))
            if model.padding:
                image = tf.keras.preprocessing.sequence.pad_sequences(
                    sequences=image,
                    maxlen=model.padding if model.lower_padding
                    and model.resize[0] < model.lower_padding else None,
                    dtype='float32',
                    padding='post',
                    truncating='post',
                    value=0)
            image = image.swapaxes(0, 1)
            return (image[:, :, np.newaxis]
                    if model.image_channel == 1 else image[:, :]) / 255.
Esempio n. 14
0
    def _image(path_or_bytes, is_random=False):
        if isinstance(path_or_bytes, bytes):
            nparr = np.fromstring(path_or_bytes, np.uint8)
            im = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
        else:
            im = cv2.imread(path_or_bytes, cv2.IMREAD_GRAYSCALE)
        # The OpenCV cannot handle gif format images, it will return None.
        if im is None:
            path_or_stream = io.BytesIO(path_or_bytes) if isinstance(
                path_or_bytes, bytes) else path_or_bytes
            pil_image = PIL.Image.open(path_or_stream)
            rgb = pil_image.split()
            size = pil_image.size

            if len(rgb) > 3 and REPLACE_TRANSPARENT:
                background = PIL.Image.new('RGB', pil_image.size,
                                           (255, 255, 255))
                background.paste(pil_image, (0, 0, size[0], size[1]),
                                 pil_image)
            pil_image = background

            if IMAGE_CHANNEL == 1:
                pil_image = pil_image.convert('L')

            im = np.array(pil_image)
        im = preprocessing(im, GAMMA, BINARYZATION, SMOOTH,
                           BLUR).astype(np.float32)

        if RESIZE[0] == -1:
            random_ratio = random.choice([2.5, 3, 3.5, 3.2, 2.7, 2.75])
            ratio = RESIZE[1] / size[1]
            random_width = int(random_ratio * RESIZE[1])
            resize_width = int(ratio * size[0])
            resize_width = random_width if is_random else resize_width
            im = cv2.resize(im, (resize_width, RESIZE[1]))
        else:
            im = cv2.resize(im, (RESIZE[0], RESIZE[1]))
        im = im.swapaxes(0, 1)
        return np.array(
            (im[:, :, np.newaxis] if IMAGE_CHANNEL == 1 else im[:, :]) / 255.)
Esempio n. 15
0
    def image(self, path_or_bytes):
        """针对图片类型的输入的编码"""
        # im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        # The OpenCV cannot handle gif format images, it will return None.
        # if im is None:

        path_or_stream = io.BytesIO(path_or_bytes) if isinstance(path_or_bytes, bytes) else path_or_bytes
        if not path_or_stream:
            return "Picture is corrupted: {}".format(path_or_bytes)
        try:
            pil_image = PIL.Image.open(path_or_stream)
        except OSError as e:
            return "{} - {}".format(e, path_or_bytes)

        use_compress = False

        gif_handle = self.model_conf.pre_concat_frames != -1 or self.model_conf.pre_blend_frames != -1

        if pil_image.mode == 'P' and not gif_handle:
            pil_image = pil_image.convert('RGB')

        rgb = pil_image.split()

        # if self.mode == RunMode.Trains and use_compress:
        #     img_compress = io.BytesIO()
        #
        #     pil_image.convert('RGB').save(img_compress, format='JPEG', quality=random.randint(75, 100))
        #     img_compress_bytes = img_compress.getvalue()
        #     img_compress.close()
        #     path_or_stream = io.BytesIO(img_compress_bytes)
        #     pil_image = PIL.Image.open(path_or_stream)

        if len(rgb) == 1 and self.model_conf.image_channel == 3:
            return "The number of image channels {} is inconsistent with the number of configured channels {}.".format(
                len(rgb), self.model_conf.image_channel
            )

        size = pil_image.size

        # if self.mode == RunMode.Trains and len(rgb) == 3 and use_compress:
        #     new_size = [size[0] + random.randint(5, 10), size[1] + random.randint(5, 10)]
        #     background = PIL.Image.new(
        #         'RGB', new_size, (255, 255, 255)
        #     )
        #     random_offset_w = random.randint(0, 5)
        #     random_offset_h = random.randint(0, 5)
        #     background.paste(
        #         pil_image,
        #         (
        #             random_offset_w,
        #             random_offset_h,
        #             size[0] + random_offset_w,
        #             size[1] + random_offset_h
        #         ),
        #         None
        #     )
        #     background.convert('RGB')
        #     pil_image = background

        if len(rgb) > 3 and self.model_conf.pre_replace_transparent and not gif_handle and not use_compress:
            background = PIL.Image.new('RGBA', pil_image.size, (255, 255, 255))
            try:
                background.paste(pil_image, (0, 0, size[0], size[1]), pil_image)
                background.convert('RGB')
                pil_image = background
            except:
                pil_image = pil_image.convert('RGB')

        if len(pil_image.split()) > 3 and self.model_conf.image_channel == 3:
            pil_image = pil_image.convert('RGB')

        if self.model_conf.pre_concat_frames != -1:
            im = concat_frames(pil_image, need_frame=self.model_conf.pre_concat_frames)
        elif self.model_conf.pre_blend_frames != -1:
            im = blend_frame(pil_image, need_frame=self.model_conf.pre_blend_frames)
        else:
            im = np.array(pil_image)

        if isinstance(im, list):
            return None

        im = preprocessing_by_func(
            exec_map=self.model_conf.pre_exec_map,
            src_arr=im
        )

        if self.model_conf.image_channel == 1 and len(im.shape) == 3:
            if self.mode == RunMode.Trains:
                im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY if bool(random.getrandbits(1)) else cv2.COLOR_BGR2GRAY)
            else:
                im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)

        im = preprocessing(
            image=im,
            binaryzation=self.model_conf.pre_binaryzation,
        )

        if self.model_conf.pre_horizontal_stitching:
            up_slice = im[0: int(size[1] / 2), 0: size[0]]
            down_slice = im[int(size[1] / 2): size[1], 0: size[0]]
            im = np.concatenate((up_slice, down_slice), axis=1)

        if self.mode == RunMode.Trains and bool(random.getrandbits(1)):
            im = preprocessing(
                image=im,
                binaryzation=self.model_conf.da_binaryzation,
                median_blur=self.model_conf.da_median_blur,
                gaussian_blur=self.model_conf.da_gaussian_blur,
                equalize_hist=self.model_conf.da_equalize_hist,
                laplacian=self.model_conf.da_laplace,
                rotate=self.model_conf.da_rotate,
                warp_perspective=self.model_conf.da_warp_perspective,
                sp_noise=self.model_conf.da_sp_noise,
                random_brightness=self.model_conf.da_brightness,
                random_saturation=self.model_conf.da_saturation,
                random_hue=self.model_conf.da_hue,
                random_gamma=self.model_conf.da_gamma,
                random_channel_swap=self.model_conf.da_channel_swap,
                random_blank=self.model_conf.da_random_blank,
                random_transition=self.model_conf.da_random_transition,
            ).astype(np.float32)

        else:
            im = im.astype(np.float32)
        if self.model_conf.resize[0] == -1:
            # random_ratio = random.choice([2.5, 3, 3.5, 3.2, 2.7, 2.75])
            ratio = self.model_conf.resize[1] / size[1]
            # random_width = int(random_ratio * RESIZE[1])
            resize_width = int(ratio * size[0])
            # resize_width = random_width if is_random else resize_width
            im = cv2.resize(im, (resize_width, self.model_conf.resize[1]))
        else:
            im = cv2.resize(im, (self.model_conf.resize[0], self.model_conf.resize[1]))
        im = im.swapaxes(0, 1)

        if self.model_conf.image_channel == 1:
            return np.array((im[:, :, np.newaxis]) / 255.)
        else:
            return np.array(im[:, :]) / 255.
Esempio n. 16
0
    def image(self, path_or_bytes):
        """针对图片类型的输入的编码"""
        # im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        # The OpenCV cannot handle gif format images, it will return None.
        # if im is None:

        path_or_stream = io.BytesIO(path_or_bytes) if isinstance(path_or_bytes, bytes) else path_or_bytes
        if not path_or_stream:
            return "Picture is corrupted: {}".format(path_or_bytes)
        try:
            pil_image = PIL.Image.open(path_or_stream)
        except OSError as e:
            return "{} - {}".format(e, path_or_bytes)

        if pil_image.mode == 'P':
            pil_image = pil_image.convert('RGB')

        rgb = pil_image.split()
        if len(rgb) == 1 and self.model_conf.image_channel == 3:
            return "The number of image channels {} is inconsistent with the number of configured channels {}.".format(
                len(rgb), self.model_conf.image_channel
            )

        size = pil_image.size

        gif_handle = self.model_conf.pre_concat_frames != -1 or self.model_conf.pre_blend_frames != -1

        if len(rgb) > 3 and self.model_conf.pre_replace_transparent and not gif_handle:
            background = PIL.Image.new('RGBA', pil_image.size, (255, 255, 255))
            background.paste(pil_image, (0, 0, size[0], size[1]), pil_image)
            background.convert('RGB')
            pil_image = background

        if self.model_conf.pre_concat_frames != -1:
            im = concat_frames(pil_image, need_frame=self.model_conf.pre_concat_frames)
        elif self.model_conf.pre_blend_frames != -1:
            im = blend_frame(pil_image, need_frame=self.model_conf.pre_blend_frames)
        else:
            im = np.array(pil_image)

        if isinstance(im, list):
            return None

        if self.model_conf.image_channel == 1 and len(im.shape) == 3:
            if self.mode == RunMode.Trains:
                im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY if bool(random.getrandbits(1)) else cv2.COLOR_BGR2GRAY)
            else:
                im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)

        im = preprocessing(
            image=im,
            binaryzation=self.model_conf.pre_binaryzation,
        )

        if self.mode == RunMode.Trains and bool(random.getrandbits(1)):
            im = preprocessing(
                image=im,
                binaryzation=self.model_conf.da_binaryzation,
                median_blur=self.model_conf.da_median_blur,
                gaussian_blur=self.model_conf.da_gaussian_blur,
                equalize_hist=self.model_conf.da_equalize_hist,
                laplacian=self.model_conf.da_laplace,
                rotate=self.model_conf.da_rotate,
                warp_perspective=self.model_conf.da_warp_perspective,
                sp_noise=self.model_conf.da_sp_noise,
                random_brightness=self.model_conf.da_brightness,
                random_saturation=self.model_conf.da_saturation,
                random_hue=self.model_conf.da_hue,
                random_gamma=self.model_conf.da_gamma,
                random_channel_swap=self.model_conf.da_channel_swap,
                random_blank=self.model_conf.da_random_blank,
                random_transition=self.model_conf.da_random_transition,
            ).astype(np.float32)

        else:
            im = im.astype(np.float32)
        if self.model_conf.resize[0] == -1:
            # random_ratio = random.choice([2.5, 3, 3.5, 3.2, 2.7, 2.75])
            ratio = self.model_conf.resize[1] / size[1]
            # random_width = int(random_ratio * RESIZE[1])
            resize_width = int(ratio * size[0])
            # resize_width = random_width if is_random else resize_width
            im = cv2.resize(im, (resize_width, self.model_conf.resize[1]))
        else:
            im = cv2.resize(im, (self.model_conf.resize[0], self.model_conf.resize[1]))
        im = im.swapaxes(0, 1)

        if self.model_conf.image_channel == 1:
            return np.array((im[:, :, np.newaxis]) / 255.)
        else:
            return np.array(im[:, :]) / 255.