[go: nahoru, domu]

Skip to content

Commit

Permalink
fix the co-ordication is minus bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
piginzoo committed May 27, 2020
1 parent 28e76fc commit 41d8b55
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
COMMON:
WORKER : 3 # 多少个进程同时生成图片
TEXT_CREATOR : corpus # 随机生成:random, 语料生成:corpus
TEXT_CREATOR : random # 随机生成:random, 语料生成:corpus
GENERATOR : contour # 保存轮廓的:contour, 只保存标签: text

CORPUS:
Expand Down
7 changes: 6 additions & 1 deletion syntext/augmentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def augument(self, image, bbox_list):
alpha=(0, 3), # 扭曲程度
sigma=(0.8, 1), # 扭曲后的平滑程度
mode="nearest"),
]),
]),
# 模糊
iaa.Sometimes(0.5, [
iaa.OneOf([
iaa.GaussianBlur(sigma=(0, 0.7)),
iaa.AverageBlur(k=(1, 3)),
iaa.MedianBlur(k=(1, 3)),
Expand Down Expand Up @@ -94,6 +99,6 @@ def augument(self, image, bbox_list):
polys = []
for p in polygons_aug.polygons:
polys.append(p.coords)
polys = np.array(polys, np.int32).tolist()
polys = np.array(polys, np.int32).tolist() # (N,2)

return image, polys
19 changes: 16 additions & 3 deletions syntext/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _caculate_position(self, text, font, x_offset, y_offset):
char_bboxes = []
for c in text:
w, h = font.getsize(c)
if c == " ":
if c == " ": # 忽略空格,但是位置要空出来
x_offset += w
continue
char_char_bbox = [
Expand Down Expand Up @@ -94,17 +94,30 @@ def _create_image(self, id, queue, num, dir):
image_path = os.path.join(dir, image_name)
image, text, bboxes = self._create_one_image()
image = self._pil2cv2(image)


image, bboxes = self.augmentor.augument(image, bboxes)

bboxes = self._revise_bboxes(bboxes)

label_data = self.build_label_data(text, bboxes)
cv2.imwrite(image_path, image)
debug_save_image(image_name, image, label_data)

debug_save_image(image_name, image, bboxes)

queue.put({'image': image_path, 'label': label_data})
except Exception as e:
traceback.print_exc()
logger.error("[#%d-%d]样本生成发生错误,忽略此错误[%s],继续...." % (id, i, str(e)))

logger.info("生成进程[%d]生成完毕,合计[%d]张" % (id, num))

def _revise_bboxes(self, bboxes):
bboxes = np.array(bboxes)
minus_indices = bboxes < 0
bboxes[minus_indices] = 0
return bboxes.tolist()

def _save_label(self, queue, total_num):
counter = 0
while True:
Expand Down Expand Up @@ -148,7 +161,7 @@ def _create_one_image(self):
logger.debug("贴文字到背景的位置:(%d,%d)", x, y)
background.paste(image, (x, y), image)

bboxes = self._caculate_position(text, font, x, y)
bboxes = self._caculate_position(text, font, x, y) # 计算每个字的bbox

return background, text, bboxes

Expand Down
11 changes: 11 additions & 0 deletions syntext/generator/generator_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,35 @@
from syntext.text.corpus_creator import CorpusTextCreator
from syntext.text.random_creator import RandomTextCreator
from syntext.augmentor import Augumentor
import logging

logger = logging.getLogger(__name__)

class GeneratorBuilder():

def build(self, config, charset, fonts, backgrounds):
logger.info("创建生成器:")
logger.info("----------------------------------------")

# 图像增强
augmentor = Augumentor(config)
logger.info("\t创建增强器\t:使用imgaug做图像增强")

# 文本创建器
if config.COMMON['TEXT_CREATOR'] == "random":
logger.info("\t创建文本生成器\t:随机产生文本")
text_creator = RandomTextCreator(config, charset)
if config.COMMON['TEXT_CREATOR'] == "corpus":
text_creator = CorpusTextCreator(config)
logger.info("\t创建文本生成器\t:从语料中抽取文本")

# 标签保存器
if config.COMMON['GENERATOR'] == "contour":
generator = ContourGenerator(config, charset, fonts, backgrounds, text_creator, augmentor)
logger.info("\t创建样本保存器\t:保存每个字的坐标")
if config.COMMON['GENERATOR'] == "text":
generator = TextOnlyGenerator(config, charset, fonts, backgrounds, text_creator, augmentor)
logger.info("\t创建样本保存器\t:仅保存字符串")

logger.info("----------------------------------------")
return generator
23 changes: 7 additions & 16 deletions syntext/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def debug(*args):



def debug_save_image(name, image, label):
def debug_save_image(name, image, bboxes): # bboxes[N,M,2]
if not DEBUG: return

image = image.copy()
Expand All @@ -55,21 +55,12 @@ def debug_save_image(name, image, label):
os.makedirs(debug_dir)
debug_image_path = os.path.join(debug_dir, name)

# {
# "label":"你好世界!",
# "pos":[
# {
# "bbox": [[x1,y1],[x1,y1],[x1,y1],[x1,y1]],
# "word": "你"
# },
# ....
# ]
# }
if type(label)==dict and "pos" in label:
for pos in label['pos']:
bboxes = np.array(pos['bbox'])
cv2.polylines(image, [bboxes], isClosed=True, color=(0,0,255))
cv2.imwrite(debug_image_path,image)
for one_word_bboxes in bboxes:
# print(one_word_bboxes)
one_word_bboxes = np.array(one_word_bboxes)
cv2.polylines(image, [one_word_bboxes], isClosed=True, color=(0, 0, 255))

cv2.imwrite(debug_image_path,image)


def dynamic_load(module_name, parent_class):
Expand Down

0 comments on commit 41d8b55

Please sign in to comment.