OCR -- 生成与背景有差异字体颜色

训练OCR模型,生成字体是必不可少的一步,有时候字体颜色和背景颜色一样、或者很相近肉眼都看不出来,这样会使得训练出现问题。

下面是生成文字与背景有差异的样本代码:

看一下效果:

    def get_word_size(font, word):
        """
        Get word size removed offset
        :param font: truetype
        :param word:
        :return:
            size: word size, removed offset (width, height)
        """
        offset = font.getoffset(word)
        size = font.getsize(word)
        size = (size[0] - offset[0], size[1] - offset[1])
        return size

..........
        bg_height = bg.shape[0]
        bg_width = bg.shape[1]

        word_size = get_word_size(font, word)
        word_height = word_size[1]
        word_width = word_size[0]


        # Draw text in the center of bg
        text_x = int((bg_width - word_width) / 2)
        text_y = int((bg_height - word_height) / 2)
..........

    def get_word_color(bg, text_x, text_y, word_height, word_width):
        """
        Only use word roi area to get word color
        """
        offset = 10
        ymin = text_y - offset
        ymax = text_y + word_height + offset
        xmin = text_x - offset
        xmax = text_x + word_width + offset

        word_roi_bg = bg[ymin: ymax, xmin: xmax]
     
        # bg_mean = int(np.mean(word_roi_bg) * (2 / 3))
        # word_color = random.randint(0, bg_mean)
        mean_bg = np.mean(word_roi_bg)
        if mean_bg > 128:
             bg_mean = int (mean_bg * (2 / 3))
             word_color = random.randint(0, bg_mean)
        else:
            bg_mean = int (mean_bg * (5 / 3))
            word_color = random.randint(bg_mean, 255)

        return word_color

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券