synthesisers.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 numpy as np
  16. import cv2
  17. from utils.config import ArgsParser, load_config, override_config
  18. from utils.logging import get_logger
  19. from engine import style_samplers, corpus_generators, text_drawers, predictors, writers
  20. class ImageSynthesiser(object):
  21. def __init__(self):
  22. self.FLAGS = ArgsParser().parse_args()
  23. self.config = load_config(self.FLAGS.config)
  24. self.config = override_config(self.config, options=self.FLAGS.override)
  25. self.output_dir = self.config["Global"]["output_dir"]
  26. if not os.path.exists(self.output_dir):
  27. os.mkdir(self.output_dir)
  28. self.logger = get_logger(
  29. log_file='{}/predict.log'.format(self.output_dir))
  30. self.text_drawer = text_drawers.StdTextDrawer(self.config)
  31. predictor_method = self.config["Predictor"]["method"]
  32. assert predictor_method is not None
  33. self.predictor = getattr(predictors, predictor_method)(self.config)
  34. def synth_image(self, corpus, style_input, language="en"):
  35. corpus_list, text_input_list = self.text_drawer.draw_text(
  36. corpus, language, style_input_width=style_input.shape[1])
  37. synth_result = self.predictor.predict(style_input, text_input_list)
  38. return synth_result
  39. class DatasetSynthesiser(ImageSynthesiser):
  40. def __init__(self):
  41. super(DatasetSynthesiser, self).__init__()
  42. self.tag = self.FLAGS.tag
  43. self.output_num = self.config["Global"]["output_num"]
  44. corpus_generator_method = self.config["CorpusGenerator"]["method"]
  45. self.corpus_generator = getattr(corpus_generators,
  46. corpus_generator_method)(self.config)
  47. style_sampler_method = self.config["StyleSampler"]["method"]
  48. assert style_sampler_method is not None
  49. self.style_sampler = style_samplers.DatasetSampler(self.config)
  50. self.writer = writers.SimpleWriter(self.config, self.tag)
  51. def synth_dataset(self):
  52. for i in range(self.output_num):
  53. style_data = self.style_sampler.sample()
  54. style_input = style_data["image"]
  55. corpus_language, text_input_label = self.corpus_generator.generate()
  56. text_input_label_list, text_input_list = self.text_drawer.draw_text(
  57. text_input_label,
  58. corpus_language,
  59. style_input_width=style_input.shape[1])
  60. text_input_label = "".join(text_input_label_list)
  61. synth_result = self.predictor.predict(style_input, text_input_list)
  62. fake_fusion = synth_result["fake_fusion"]
  63. self.writer.save_image(fake_fusion, text_input_label)
  64. self.writer.save_label()
  65. self.writer.merge_label()