data.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 augmented_data_image(block, output_folder, prefix_image_name):
  23. rotations = [0, 90, 180, 270]
  24. img_flip_labels = ['original', 'horizontal', 'vertical', 'both']
  25. horizontal_img = block.transpose(Image.FLIP_LEFT_RIGHT)
  26. vertical_img = block.transpose(Image.FLIP_TOP_BOTTOM)
  27. both_img = block.transpose(Image.TRANSPOSE)
  28. flip_images = [block, horizontal_img, vertical_img, both_img]
  29. # rotate and flip image to increase dataset size
  30. for id, flip in enumerate(flip_images):
  31. for rotation in rotations:
  32. rotated_output_img = flip.rotate(rotation)
  33. output_reconstructed_filename = prefix_image_name + post_image_name_separator
  34. output_reconstructed_filename = output_reconstructed_filename + img_flip_labels[id] + '_' + str(rotation) + '.png'
  35. output_reconstructed_path = os.path.join(output_folder, output_reconstructed_filename)
  36. if not os.path.exists(output_reconstructed_path):
  37. rotated_output_img.save(output_reconstructed_path)