前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OCR -- 生成与背景有差异字体颜色

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

作者头像
MachineLP
发布2019-05-26 15:29:39
8310
发布2019-05-26 15:29:39
举报
文章被收录于专栏:小鹏的专栏小鹏的专栏

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

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

看一下效果:

代码语言:javascript
复制
    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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年10月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档