Esempio n. 1
0
 def apply_to_mask(self,
                   img,
                   pad_top=0,
                   pad_bottom=0,
                   pad_left=0,
                   pad_right=0,
                   **params):
     return F.pad_with_params(img,
                              pad_top,
                              pad_bottom,
                              pad_left,
                              pad_right,
                              border_mode=self.mask_border_mode,
                              value=self.mask_value)
 def apply(self,
           img,
           pad_top=0,
           pad_bottom=0,
           pad_left=0,
           pad_right=0,
           **params):
     img = ALF.pad_with_params(img,
                               pad_top,
                               pad_bottom,
                               pad_left,
                               pad_right,
                               border_mode=self.border_mode,
                               value=self.value)
     img = cv2.resize(img, self.size)
     return img
Esempio n. 3
0
def pad_to_square(img, fill: Union[Number, str, tuple] = 0):
    h, w = img.shape[:2]

    if w != h:
        diff = max(w, h) - min(w, h)
        d1 = int(diff / 2)
        d2 = diff - d1

        left, top, right, bottom = (0, d1, 0, d2) if w > h else (d1, 0, d2, 0)
        img = AF.pad_with_params(img,
                                 h_pad_top=top,
                                 h_pad_bottom=bottom,
                                 w_pad_left=left,
                                 w_pad_right=right,
                                 border_mode=cv2.BORDER_CONSTANT,
                                 value=fill)

    return img
    def create_text_image(self, index, etc_text_params=None):
        if random.random() <= self.same_text_in_batch_prob:
            cur_unicode_list = self.cur_unicode_list
        else:
            cur_unicode_list = self._create_random_text()
            self.cur_text_ing_before = "".join(
                [chr(c) for c in cur_unicode_list])

        if random.random() <= self.same_font_size_in_batch_prob:
            font_size = self.cur_font_size
        else:
            font_size = random.choice(self.font_size_range)

        text = "".join([chr(c) for c in cur_unicode_list])
        self.cur_text_ing = text

        if random.random() <= self.same_text_params_in_batch_prob:
            char_params = self.cur_text_params
        else:
            char_params = self._get_text_params()

        char_params['font_size'] = font_size

        self.cur_params = char_params
        self.cur_font = self.font_list[index]
        # import uuid
        # char_params['output_path'] = "../testimage/{}_{}_{}.jpg".format(self.cur_unicode_list[0], index,
        #                                                                 str(uuid.uuid4()))
        # text_image_maker.create_text_image(text, self.font_list[index], **char_params)
        char_params['output_path'] = None
        char_params['auto_chance_color_when_same'] = True
        char_params['raise_exception'] = False
        char_params['return_mask'] = self.return_mask
        # import json
        # print(json.dumps(char_params))
        # char_params = json.loads('{"color_mode": "RGB", "paddings": {"right": 0.1050272078196586, "top": 0.26090272932922254, "bottom": 0.17015320517900254, "left": 0.25837411687366774}, "text_border": null, "fg_color": [209, 235, 98, 166], "text_italic": false, "font_size": 15, "bg_img_path": "/home/irelin/resource/font_recognition/bgs/294.winterwax-500x500.jpg", "text_shadow": null, "use_img_persp_trans": true, "pos_ratio": [0.3, 0.2], "text_persp_trans_params": [-0.009976076882772873, 0.036253351174196216], "text_rotate": 14, "bg_img_width_ratio": 1.2, "text_blur": 0, "bg_img_height_ratio": 1.1, "use_bg_color": false, "bg_img_scale": 0.5601430892743575, "use_text_persp_trans": true, "use_binarize": false, "img_persp_trans_params": [-0.02296148027430462, 0.004213654695530833], "text_width_ratio": 1.0, "text_gradient": null, "output_path": null, "auto_chance_color_when_same": true, "text_height_ratio": 1.0}')
        # if self.use_debug:
        #     print(os.path.basename(self.font_list[index]), text)
        if etc_text_params:
            char_params.update(etc_text_params)
        img = text_image_maker.create_text_image(text, self.font_list[index],
                                                 **char_params)
        if self.return_mask:
            img, mask = img

        if self.use_same_random_crop_in_batch:
            height, width = img.shape[:2]
            if width >= height:
                img = F.smallest_max_size(img,
                                          max_size=self.input_size,
                                          interpolation=cv2.INTER_LINEAR)
            else:
                img = F.longest_max_size(img,
                                         max_size=self.input_size,
                                         interpolation=cv2.INTER_LINEAR)
                pad_width = self.input_size - img.shape[:2][1]
                left = pad_width // 2
                right = pad_width - left
                img = F.pad_with_params(img,
                                        0,
                                        0,
                                        left,
                                        right,
                                        border_mode=cv2.BORDER_CONSTANT,
                                        value=0)

            height, width = img.shape[:2]
            if width > self.input_size:
                last_index = width - self.input_size
                start_index = int(self.crop_start_ratio * last_index)
                img = img[:, start_index:start_index + self.input_size, :]

        if self.transform is not None:
            img = self.transform(image=img)['image']

        results = [img]

        if self.return_mask:
            results.append(mask)

        if self.return_text:
            results.append(text)

        if len(results) > 1:
            return results
        else:
            return img