data.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from ipfml import processing, metrics
  2. from modules.utils.config import *
  3. from PIL import Image
  4. from skimage import color
  5. import numpy as np
  6. _scenes_names_prefix = '_scenes_names'
  7. _scenes_indices_prefix = '_scenes_indices'
  8. # store all variables from current module context
  9. context_vars = vars()
  10. def get_svd_data(data_type, block):
  11. """
  12. Method which returns the data type expected
  13. """
  14. if data_type == 'lab':
  15. block_file_path = '/tmp/lab_img.png'
  16. block.save(block_file_path)
  17. data = processing.get_LAB_L_SVD_s(Image.open(block_file_path))
  18. if data_type == 'mscn_revisited':
  19. img_mscn_revisited = processing.rgb_to_mscn(block)
  20. # save tmp as img
  21. img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
  22. mscn_revisited_file_path = '/tmp/mscn_revisited_img.png'
  23. img_output.save(mscn_revisited_file_path)
  24. img_block = Image.open(mscn_revisited_file_path)
  25. # extract from temp image
  26. data = metrics.get_SVD_s(img_block)
  27. if data_type == 'mscn':
  28. img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
  29. img_mscn = processing.calculate_mscn_coefficients(img_gray, 7)
  30. img_mscn_norm = processing.normalize_2D_arr(img_mscn)
  31. img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')
  32. data = metrics.get_SVD_s(img_mscn_gray)
  33. if data_type == 'low_bits_6':
  34. low_bits_6 = processing.rgb_to_LAB_L_low_bits(block, 6)
  35. data = metrics.get_SVD_s(low_bits_6)
  36. if data_type == 'low_bits_5':
  37. low_bits_5 = processing.rgb_to_LAB_L_low_bits(block, 5)
  38. data = metrics.get_SVD_s(low_bits_5)
  39. if data_type == 'low_bits_4':
  40. low_bits_4 = processing.rgb_to_LAB_L_low_bits(block, 4)
  41. data = metrics.get_SVD_s(low_bits_4)
  42. if data_type == 'low_bits_3':
  43. low_bits_3 = processing.rgb_to_LAB_L_low_bits(block, 3)
  44. data = metrics.get_SVD_s(low_bits_3)
  45. if data_type == 'low_bits_2':
  46. low_bits_2 = processing.rgb_to_LAB_L_low_bits(block, 2)
  47. data = metrics.get_SVD_s(low_bits_2)
  48. if data_type == 'low_bits_4_shifted_2':
  49. data = metrics.get_SVD_s(processing.rgb_to_LAB_L_bits(block, (3, 6)))
  50. return data
  51. def get_renderer_scenes_indices(renderer_name):
  52. if renderer_name not in renderer_choices:
  53. raise ValueError("Unknown renderer name")
  54. if renderer_name == 'all':
  55. return scenes_indices
  56. else:
  57. return context_vars[renderer_name + _scenes_indices_prefix]
  58. def get_renderer_scenes_names(renderer_name):
  59. if renderer_name not in renderer_choices:
  60. raise ValueError("Unknown renderer name")
  61. if renderer_name == 'all':
  62. return scenes_names
  63. else:
  64. return context_vars[renderer_name + _scenes_names_prefix]