idcard_det_rec_generate_forward.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import argparse
  2. import os.path
  3. import cv2
  4. from PIL import Image, ImageDraw, ImageFont
  5. from tqdm import tqdm
  6. from img_augmentation import img_augmentation_light as light
  7. from img_augmentation import img_augmentation_noise as noise
  8. def generate_idcard(info_dir, save_dir):
  9. if not os.path.exists(save_dir + '/crop_imgs'):
  10. os.makedirs(save_dir + '/crop_imgs')
  11. with open(info_dir, 'r', encoding='utf-8') as fp:
  12. text_lines = fp.readlines()
  13. cnt = 0
  14. for text_line in tqdm(text_lines):
  15. info_list = text_line.rstrip('\n').split(',')
  16. bg_path = "usedres/tw_idcard_bg_1.jpg"
  17. if 20 < cnt <= 60:
  18. bg_path = "usedres/tw_idcard_bg_2.png"
  19. if cnt > 60:
  20. bg_path = "usedres/tw_idcard_bg_color.jpg"
  21. pil_image = Image.open(bg_path)
  22. # pil_image 接收住这个图片对象
  23. # width 为图片的宽, height为图片的高
  24. width, height = pil_image.size
  25. # 生成一张尺寸为 width * height 背景色为白色的图片
  26. bg = Image.new('RGB', (width, height), color=(0, 0, 0))
  27. bg.paste(pil_image, (0, 0)) # 写入底图
  28. # 第一个参数为图片
  29. # 第二个参数为图片的要从哪里开始写入,
  30. name = info_list[0] # 写入的文字
  31. year = info_list[1]
  32. month = info_list[2]
  33. day = info_list[3]
  34. birth = '民國 ' + year + ' 年 ' + month + ' 月 ' + day + ' 日'
  35. addr = info_list[4]
  36. sex = info_list[5]
  37. id = info_list[6]
  38. icon_addr = info_list[7]
  39. name_font_size = 45
  40. temp_str = ''
  41. if len(name) <= 6:
  42. num_of_space = (4 * (6 - len(name)))/(len(name)-1)
  43. for i in range(0, len(name) - 1):
  44. temp_str += name[i]
  45. for j in range(0, int(num_of_space)):
  46. temp_str += ' '
  47. temp_str += name[len(name)-1]
  48. name = temp_str
  49. elif 6 < len(name) < 9:
  50. name_font_size = 35
  51. elif 9 <= len(name) <= 12:
  52. name_font_size = 25
  53. font = "./usedres/R-PMingLiU-TW-2.ttf" # 一个字体文件
  54. name_font = ImageFont.truetype(font, name_font_size) # 设置名字字体和大小
  55. addr_font = ImageFont.truetype(font, 22)
  56. other_font = ImageFont.truetype(font, 26) # 设置字体和大小
  57. id_font = ImageFont.truetype(font, 34)
  58. t_font = ImageFont.truetype(font, 25)
  59. # 计算出要写入的文字占用的像素
  60. # 创建一个可以在给定图像上绘图的对象
  61. draw = ImageDraw.Draw(bg)
  62. draw.text((110, 195), name, fill="#1E1E1E", font=name_font)
  63. draw.text((110, 290), birth, fill="#1E1E1E", font=other_font)
  64. draw.text((570, 295), sex, fill="#1E1E1E", font=t_font)
  65. draw.text((110, 365), addr, fill="#1E1E1E", font=addr_font)
  66. draw.text((465, 365), id, fill="#B4495C", font=id_font)
  67. path_list = []
  68. # 保存画布
  69. img_path = save_dir + '/idcard_' + str(cnt) + '_' + icon_addr.split('/')[-1].split('.')[0] + '.png'
  70. # img_path = save_dir + '/idcard_' + str(cnt) + '_mask.png'
  71. bg.save(img_path, "PNG")
  72. path_list.append(img_path)
  73. # 增亮、變暗
  74. light.work(img_path, '')
  75. path_list.append(str(img_path)[0:-4] + '_dark.png')
  76. path_list.append(str(img_path)[0:-4] + '_bright.png')
  77. # 高斯噪聲、椒鹽噪聲
  78. noise.add_noise(img_path, '')
  79. path_list.append(str(img_path)[0:-4] + "_guassian.png")
  80. path_list.append(str(img_path)[0:-4] + "_sp.png")
  81. cnt += 1
  82. boxes = []
  83. box_name_0 = [105, 195, 400, 245]
  84. box0 = []
  85. box0.append(box_name_0)
  86. box0.append(name.replace(' ', ''))
  87. box0.append('name')
  88. boxes.append(box0)
  89. box_birth_0 = [105, 290, 420, 320]
  90. box1 = []
  91. box1.append(box_birth_0)
  92. box1.append(birth.replace(' ', ''))
  93. box1.append('birth')
  94. boxes.append(box1)
  95. box_sex_0 = [570, 295, 595, 325]
  96. box2 = []
  97. box2.append(box_sex_0)
  98. box2.append(sex.replace(' ', ''))
  99. box2.append('sex')
  100. boxes.append(box2)
  101. box_addr_0 = [105, 365, 420, 390]
  102. box3 = []
  103. box3.append(box_addr_0)
  104. box3.append(addr.replace(' ', ''))
  105. box3.append('date')
  106. boxes.append(box3)
  107. box_id_0 = [465, 365, 640, 400]
  108. box4 = []
  109. box4.append(box_id_0)
  110. box4.append(id)
  111. box4.append('id')
  112. boxes.append(box4)
  113. num = 0
  114. fp = open(save_dir + '/rec_gt.txt', 'a', encoding='utf-8')
  115. fp1 = open(save_dir + '/Label.txt', 'a', encoding='utf-8')
  116. for path in path_list:
  117. # print(path)
  118. jishu = 1
  119. anno = str(path) + '\t['
  120. for box in boxes:
  121. if jishu <= 5:
  122. if jishu == 5:
  123. s = '{"transcription": "' + box[1] + '", "points": [[' + str(box[0][0]) + ', ' + str(box[0][1]) + '], [' + str(box[0][2]) + ', ' + str(box[0][1]) + '], [' + str(box[0][2]) + ', ' + str(box[0][3]) + '], [' + str(box[0][0]) + ', ' + str(box[0][3]) + ']], "difficult": false, "key_cls": "' + box[2] + '"}'
  124. else:
  125. s = '{"transcription": "' + box[1] + '", "points": [[' + str(box[0][0]) + ', ' + str(
  126. box[0][1]) + '], [' + str(box[0][2]) + ', ' + str(box[0][1]) + '], [' + str(
  127. box[0][2]) + ', ' + str(box[0][3]) + '], [' + str(box[0][0]) + ', ' + str(
  128. box[0][3]) + ']], "difficult": false, "key_cls": "' + box[2] + '"}, '
  129. anno += s
  130. image_cut_save(path, box[0][0], box[0][1], box[0][2], box[0][3], save_dir + '/crop_imgs/' + str(path).split('/')[-1].split('.')[0] + '_' + str(num) + '.png')
  131. line = 'crop_imgs/' + str(path).split('/')[-1].split('.')[0] + '_' + str(num) + '.png' + '\t' + box[1] + '\n'
  132. fp.write(line)
  133. num += 1
  134. jishu += 1
  135. anno += ']\n'
  136. fp1.write(anno)
  137. # print(anno)
  138. fp1.close()
  139. fp.close()
  140. def image_cut_save(path, left, upper, right, lower, save_path):
  141. """
  142. 所截区域图片保存
  143. :param path: 图片路径
  144. :param left: 区块左上角位置的像素点离图片左边界的距离
  145. :param upper:区块左上角位置的像素点离图片上边界的距离
  146. :param right:区块右下角位置的像素点离图片左边界的距离
  147. :param lower:区块右下角位置的像素点离图片上边界的距离
  148. 故需满足:lower > upper、right > left
  149. :param save_path: 所截图片保存位置
  150. """
  151. img = cv2.imread(path) # 打开图像
  152. cropped = img[upper:lower, left:right]
  153. cv2.imwrite(save_path, cropped)
  154. if __name__ == '__main__':
  155. parser = argparse.ArgumentParser()
  156. parser.add_argument('--info_dir', type=str, default='val.txt')
  157. parser.add_argument('--save_dir', type=str, default='train')
  158. args = parser.parse_args()
  159. generate_idcard(args.info_dir, args.save_dir)
  160. # image_cut_save('./idcard_0_02.png', 445, 340, 540, 370, 'result.png')