data_type.py 2.9 KB

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