Exemplo n.º 1
0
def test_fun_max_size():
    target_width = 256

    img = np.empty((330, 49), dtype=np.uint8)
    out = F.smallest_max_size(img, target_width, interpolation=cv2.INTER_LINEAR)

    assert out.shape == (1724, target_width)
Exemplo n.º 2
0
def test_smallest_max_size(target):
    img = np.array(
        [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]], dtype=np.uint8
    )
    expected = np.array([[2, 4, 5, 7], [10, 11, 13, 14], [17, 19, 20, 22]], dtype=np.uint8)

    img, expected = convert_2d_to_target_format([img, expected], target=target)
    scaled = F.smallest_max_size(img, max_size=3, interpolation=cv2.INTER_LINEAR)
    assert np.array_equal(scaled, expected)
    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