基于text render生成文字识别训练样本

text_renderer Github链接

示例文件

./text_renderer-master/example_data/example.py
生成效果展示:
在这里插入图片描述

参数修改

1. 输入文件路径参数的修改

CURRENT_DIR = Path(os.path.abspath(os.path.dirname(__file__)))
OUT_DIR = CURRENT_DIR / "output"
DATA_DIR = CURRENT_DIR
BG_DIR = DATA_DIR / "bg_gray"
CHAR_DIR = DATA_DIR / "char"
FONT_DIR = DATA_DIR / "font"
FONT_LIST_DIR = DATA_DIR / "font_list"
TEXT_DIR = DATA_DIR / "text"

font_cfg = dict(
    font_dir=FONT_DIR,
    font_list_file=FONT_LIST_DIR / "font_list.txt",
    font_size=(30, 31),
)

2. 生成数据的相关参数修改

def base_cfg(
    name: str, corpus, corpus_effects=None, layout_effects=None, layout=None, gray=True
):
    return GeneratorCfg(

        # 生成图片的数量
        num_image=21,
        save_dir=OUT_DIR / name,
        # RenderCfg:可以设置生成图片的高度
        render_cfg=RenderCfg(
            text_color_cfg=SimpleTextColorCfg(),
            height=48,
            bg_dir=BG_DIR,
            # perspective_transform : Apply Perspective Transform
            perspective_transform=perspective_transform,
            # gray : Save image as gray image
            gray=gray,
            # layout_effects : Effects apply on merged text mask image output by Layout.
            layout_effects=layout_effects,
            # layout : Layout will applied if corpus is a List
            layout=layout,
            corpus=corpus,
            corpus_effects=corpus_effects,
        ),
    )

3. 生成功能的函数构建

def enum_data():
    return base_cfg(
        inspect.currentframe().f_code.co_name,
        # 即使开启灰度,也只有50%的概率产生灰度图,灰度的概率在render.py的norm函数中设置
        gray=True,
        corpus=EnumCorpus(
            EnumCorpusCfg(
                text_paths=[TEXT_DIR / "corpus_generate.txt"],
                filter_by_chars=False,
                chars_file=CHAR_DIR / "keys_v1.txt",
                **font_cfg
            ),
        ),
        corpus_effects=Effects(
            [
                
                # Padding(p=0.8, w_ratio=[0.2, 0.21], h_ratio=[0.7, 0.71], center=True),
                Padding(p=0.4, w_ratio=[0.1, 0.19], h_ratio=[0.7, 0.71], center=True),
                Curve(p=0.3, period=180, amplitude=(4, 5)),
                ImgAugEffect(p=0.5,aug=iaa.Emboss(alpha=(0.9, 1.0), strength=(1.5, 1.6))),
                Line(p=0.5, color_cfg=SimpleTextColorCfg()),# FixedTextColorCfg
                # OneOf([DropoutRand(), DropoutVertical(),DropoutHorizontal()]),
                DropoutRand(p=0.6),
                DropoutVertical(p=0.2),
                DropoutHorizontal(p=0.2),
                # Padding(),
            ]
        ),
        layout=ExtraTextLineLayout(),#SameLineLayout()
        layout_effects=Effects(Line(p=0.5)),
    )

启动

执行命令:

python3 main.py --config example_data/example.py --dataset img --num_processes 1 --log_period 10

补充

1. 随机生成用于训练横向识别和纵向识别的样本:在./text_renderer-master/text_renderer/render.py文件中

gen_single_corpus函数内加入一句话:

    def gen_single_corpus(self) -> Tuple[PILImage, str, PILImage, PILImage]:
        # 水平就True  纵向就False各50%概率
        self.corpus.cfg.horizontal=random.choice([False,True])

2. 随机生成灰度图:在./text_renderer-master/text_renderer/render.py文件中

norm函数内:

    def norm(self, image: np.ndarray) -> np.ndarray:
        if self.cfg.gray:
            # 加了下面这一个判断,即便config中gray是True,也只有一半的概率生成灰度图!
            if random.choice([True, False]):
                print("gray")
                image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

3. 实现逐行读取已经准备好的语料库:在./text_renderer-master/text_renderer/corpus/enum_corpus.py文件中

EnumCorpus(Corpus)类下将原来的get_text函数注释掉,新增下面的函数。

这里注意!修改后执行命令中的线程数必须为1!

# 定义一个全局变量来跟踪当前索引
current_index = 0
class EnumCorpus(Corpus):
	# 修改后的,逐行读enum_text
    def get_text(self):
        global current_index  # 使用全局变量
        if current_index >= len(self.texts):  # 如果索引超出范围,重置为0
            current_index = 0
        text=self.texts[current_index]
        current_index += 1  # 移动到下一个索引
        return text

4. 生成的图片包含深色底,浅色字:在text_renderer/config/__init__.py中修改class SimpleTextColorCfg(TextColorCfg)

注:如果用这个类了才在这个类里面改!

@dataclass
class SimpleTextColorCfg(TextColorCfg):
    """
    Randomly use mean value of background image
    """

    alpha: Tuple[int, int] = (110, 255)

    def get_color(self, bg_img: PILImage) -> Tuple[int, int, int, int]:
        np_img = np.array(bg_img)
        mean = np.mean(np_img)

        alpha = np.random.randint(*self.alpha)
        if mean<200:
            r = np.random.randint(200,255)
            g = np.random.randint(200,255)
            b = np.random.randint(200,255)
        else:
            r = np.random.randint(0, int(mean * 0.7))
            g = np.random.randint(0, int(mean * 0.7))
            b = np.random.randint(0, int(mean * 0.7))
        text_color = (r, g, b, alpha)

        return text_color

生成样本示例:

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值