synth_image.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import cv2
  16. import sys
  17. import glob
  18. __dir__ = os.path.dirname(os.path.abspath(__file__))
  19. sys.path.append(__dir__)
  20. sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
  21. from utils.config import ArgsParser
  22. from engine.synthesisers import ImageSynthesiser
  23. def synth_image():
  24. args = ArgsParser().parse_args()
  25. image_synthesiser = ImageSynthesiser()
  26. style_image_path = args.style_image
  27. img = cv2.imread(style_image_path)
  28. text_corpus = args.text_corpus
  29. language = args.language
  30. synth_result = image_synthesiser.synth_image(text_corpus, img, language)
  31. fake_fusion = synth_result["fake_fusion"]
  32. fake_text = synth_result["fake_text"]
  33. fake_bg = synth_result["fake_bg"]
  34. cv2.imwrite("fake_fusion.jpg", fake_fusion)
  35. cv2.imwrite("fake_text.jpg", fake_text)
  36. cv2.imwrite("fake_bg.jpg", fake_bg)
  37. def batch_synth_images():
  38. image_synthesiser = ImageSynthesiser()
  39. corpus_file = "../StyleTextRec_data/test_20201208/test_text_list.txt"
  40. style_data_dir = "../StyleTextRec_data/test_20201208/style_images/"
  41. save_path = "./output_data/"
  42. corpus_list = []
  43. with open(corpus_file, "rb") as fin:
  44. lines = fin.readlines()
  45. for line in lines:
  46. substr = line.decode("utf-8").strip("\n").split("\t")
  47. corpus_list.append(substr)
  48. style_img_list = glob.glob("{}/*.jpg".format(style_data_dir))
  49. corpus_num = len(corpus_list)
  50. style_img_num = len(style_img_list)
  51. for cno in range(corpus_num):
  52. for sno in range(style_img_num):
  53. corpus, lang = corpus_list[cno]
  54. style_img_path = style_img_list[sno]
  55. img = cv2.imread(style_img_path)
  56. synth_result = image_synthesiser.synth_image(corpus, img, lang)
  57. fake_fusion = synth_result["fake_fusion"]
  58. fake_text = synth_result["fake_text"]
  59. fake_bg = synth_result["fake_bg"]
  60. for tp in range(2):
  61. if tp == 0:
  62. prefix = "%s/c%d_s%d_" % (save_path, cno, sno)
  63. else:
  64. prefix = "%s/s%d_c%d_" % (save_path, sno, cno)
  65. cv2.imwrite("%s_fake_fusion.jpg" % prefix, fake_fusion)
  66. cv2.imwrite("%s_fake_text.jpg" % prefix, fake_text)
  67. cv2.imwrite("%s_fake_bg.jpg" % prefix, fake_bg)
  68. cv2.imwrite("%s_input_style.jpg" % prefix, img)
  69. print(cno, corpus_num, sno, style_img_num)
  70. if __name__ == '__main__':
  71. # batch_synth_images()
  72. synth_image()