oh-my-ocr / text_renderer

https://oh-my-ocr.github.io/text_renderer/README.html
MIT License
802 stars 161 forks source link

生成图片的高度设置 #40

Closed GivanTsai closed 2 years ago

GivanTsai commented 2 years ago

现在的代码中最后生成的图片的高度是在base_cfg中height参数控制的,默认32,请问可以变成随机选择吗,比如每生成一张图片,高度在【20,25,30,32】之类中随机选择一个,更加有多样性。

Sanster commented 2 years ago

可以先在配置文件里随机高度,配置文件就是 python 文件,很灵活的

GivanTsai commented 2 years ago

配置文件里

image

你是说这样吗,在配置里随机高度的话,只能做到初始随机一个高度,然后生成的所有图片都是这个高度

Sanster commented 2 years ago

供参考:

import inspect
import os
from pathlib import Path

from text_renderer.config import (
    RenderCfg,
    NormPerspectiveTransformCfg,
    GeneratorCfg,
    FixedTextColorCfg,
)
from text_renderer.corpus import *
from text_renderer.effect import *

CURRENT_DIR = Path(os.path.abspath(os.path.dirname(__file__)))
OUT_DIR = CURRENT_DIR / "output"
DATA_DIR = CURRENT_DIR
BG_DIR = DATA_DIR / "bg"
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),
)

perspective_transform = NormPerspectiveTransformCfg(20, 20, 1.5)

def get_char_corpus():
    return CharCorpus(
        CharCorpusCfg(
            text_paths=[TEXT_DIR / "chn_text.txt", TEXT_DIR / "eng_text.txt"],
            filter_by_chars=True,
            chars_file=CHAR_DIR / "chn.txt",
            length=(5, 10),
            char_spacing=(-0.3, 1.3),
            **font_cfg
        ),
    )

def base_cfg(
    name: str, corpus, corpus_effects=None, layout_effects=None, layout=None, gray=True, height: int = 32
):
    return GeneratorCfg(
        num_image=50,
        save_dir=OUT_DIR / name,
        render_cfg=RenderCfg(
            bg_dir=BG_DIR,
            perspective_transform=perspective_transform,
            gray=gray,
            height=height,
            layout_effects=layout_effects,
            layout=layout,
            corpus=corpus,
            corpus_effects=corpus_effects,
        ),
    )

def diff_height_data(height):
    return base_cfg(
        inspect.currentframe().f_code.co_name,
        corpus=get_char_corpus(),
        corpus_effects=Effects(
            [
                Line(0.5, color_cfg=FixedTextColorCfg()),
                OneOf([DropoutRand(), DropoutVertical()]),
            ]
        ),
        height=height
    )

configs = [
    diff_height_data(height=32),
    diff_height_data(height=24),
    diff_height_data(height=28),
]
tingyuyan commented 1 year ago

./text_renderer/config/init.py

class RenderCfg:
    corpus: Union["Corpus", List["Corpus"]]
    corpus_effects: Union[Effects, List[Effects]] = None
    bg_dir: Path = None
    pre_load_bg_img: bool = True
    layout: Layout = SameLineLayout()
    perspective_transform: PerspectiveTransformCfg = None
    layout_effects: Effects = None
    render_effects: Effects = None
    height: tuple = (30, 32)

./example_data/example.py

font_cfg = dict(
    font_dir=FONT_DIR,
    font_list_file=FONT_LIST_DIR / "vertical.txt",
    font_size=(20, 36),#这里设置图像高度的范围
)

./text_renderer/render.py

    def norm(self, image: np.ndarray) -> np.ndarray:
        if self.cfg.gray:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        font_size = self.cfg.height
        new_h = random.randint(font_size[0], font_size[1])
        if new_h != -1 and new_h != image.shape[0]:
            height, width = image.shape[:2]
            width = int(width // (height / new_h))
            image = cv2.resize(
                image, (width, new_h), interpolation=cv2.INTER_CUBIC
            )

        return image