data.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. from PIL import Image
  3. from ..config.cnn_config import *
  4. _scenes_names_prefix = '_scenes_names'
  5. _scenes_indices_prefix = '_scenes_indices'
  6. # store all variables from current module context
  7. context_vars = vars()
  8. def get_renderer_scenes_indices(renderer_name):
  9. if renderer_name not in renderer_choices:
  10. raise ValueError("Unknown renderer name")
  11. if renderer_name == 'all':
  12. return scenes_indices
  13. else:
  14. return context_vars[renderer_name + _scenes_indices_prefix]
  15. def get_renderer_scenes_names(renderer_name):
  16. if renderer_name not in renderer_choices:
  17. raise ValueError("Unknown renderer name")
  18. if renderer_name == 'all':
  19. return scenes_names
  20. else:
  21. return context_vars[renderer_name + _scenes_names_prefix]
  22. def get_scene_image_quality(img_path):
  23. img_postfix = img_path.split('/')[-1].split(scene_image_quality_separator)[-1]
  24. img_quality = img_postfix.replace(scene_image_extension, '')
  25. return img_quality
  26. def augmented_data_image(block, output_folder, prefix_image_name):
  27. rotations = [0, 90, 180, 270]
  28. img_flip_labels = ['original', 'horizontal', 'vertical', 'both']
  29. horizontal_img = block.transpose(Image.FLIP_LEFT_RIGHT)
  30. vertical_img = block.transpose(Image.FLIP_TOP_BOTTOM)
  31. both_img = block.transpose(Image.TRANSPOSE)
  32. flip_images = [block, horizontal_img, vertical_img, both_img]
  33. # rotate and flip image to increase dataset size
  34. for id, flip in enumerate(flip_images):
  35. for rotation in rotations:
  36. rotated_output_img = flip.rotate(rotation)
  37. output_reconstructed_filename = prefix_image_name + post_image_name_separator
  38. output_reconstructed_filename = output_reconstructed_filename + img_flip_labels[id] + '_' + str(rotation) + '.png'
  39. output_reconstructed_path = os.path.join(output_folder, output_reconstructed_filename)
  40. if not os.path.exists(output_reconstructed_path):
  41. rotated_output_img.save(output_reconstructed_path)