data.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. from ipfml import processing, metrics, utils
  2. from modules.utils.config import *
  3. from PIL import Image
  4. from skimage import color
  5. from sklearn.decomposition import FastICA
  6. from sklearn.decomposition import IncrementalPCA
  7. from sklearn.decomposition import TruncatedSVD
  8. from numpy.linalg import svd as lin_svd
  9. from scipy.signal import medfilt2d, wiener, cwt
  10. import pywt
  11. import cv2
  12. import numpy as np
  13. _scenes_names_prefix = '_scenes_names'
  14. _scenes_indices_prefix = '_scenes_indices'
  15. # store all variables from current module context
  16. context_vars = vars()
  17. def get_svd_data(data_type, block):
  18. """
  19. Method which returns the data type expected
  20. """
  21. if data_type == 'lab':
  22. block_file_path = '/tmp/lab_img.png'
  23. block.save(block_file_path)
  24. data = processing.get_LAB_L_SVD_s(Image.open(block_file_path))
  25. if data_type == 'mscn':
  26. img_mscn_revisited = processing.rgb_to_mscn(block)
  27. # save tmp as img
  28. img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
  29. mscn_revisited_file_path = '/tmp/mscn_revisited_img.png'
  30. img_output.save(mscn_revisited_file_path)
  31. img_block = Image.open(mscn_revisited_file_path)
  32. # extract from temp image
  33. data = metrics.get_SVD_s(img_block)
  34. """if data_type == 'mscn':
  35. img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
  36. img_mscn = processing.calculate_mscn_coefficients(img_gray, 7)
  37. img_mscn_norm = processing.normalize_2D_arr(img_mscn)
  38. img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')
  39. data = metrics.get_SVD_s(img_mscn_gray)
  40. """
  41. if data_type == 'low_bits_6':
  42. low_bits_6 = processing.rgb_to_LAB_L_low_bits(block, 6)
  43. data = metrics.get_SVD_s(low_bits_6)
  44. if data_type == 'low_bits_5':
  45. low_bits_5 = processing.rgb_to_LAB_L_low_bits(block, 5)
  46. data = metrics.get_SVD_s(low_bits_5)
  47. if data_type == 'low_bits_4':
  48. low_bits_4 = processing.rgb_to_LAB_L_low_bits(block, 4)
  49. data = metrics.get_SVD_s(low_bits_4)
  50. if data_type == 'low_bits_3':
  51. low_bits_3 = processing.rgb_to_LAB_L_low_bits(block, 3)
  52. data = metrics.get_SVD_s(low_bits_3)
  53. if data_type == 'low_bits_2':
  54. low_bits_2 = processing.rgb_to_LAB_L_low_bits(block, 2)
  55. data = metrics.get_SVD_s(low_bits_2)
  56. if data_type == 'low_bits_4_shifted_2':
  57. data = metrics.get_SVD_s(processing.rgb_to_LAB_L_bits(block, (3, 6)))
  58. if data_type == 'sub_blocks_stats':
  59. block = np.asarray(block)
  60. width, height, _= block.shape
  61. sub_width, sub_height = int(width / 4), int(height / 4)
  62. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  63. data = []
  64. for sub_b in sub_blocks:
  65. # by default use the whole lab L canal
  66. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  67. # get information we want from svd
  68. data.append(np.mean(l_svd_data))
  69. data.append(np.median(l_svd_data))
  70. data.append(np.percentile(l_svd_data, 25))
  71. data.append(np.percentile(l_svd_data, 75))
  72. data.append(np.var(l_svd_data))
  73. area_under_curve = utils.integral_area_trapz(l_svd_data, dx=100)
  74. data.append(area_under_curve)
  75. # convert into numpy array after computing all stats
  76. data = np.asarray(data)
  77. if data_type == 'sub_blocks_stats_reduced':
  78. block = np.asarray(block)
  79. width, height, _= block.shape
  80. sub_width, sub_height = int(width / 4), int(height / 4)
  81. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  82. data = []
  83. for sub_b in sub_blocks:
  84. # by default use the whole lab L canal
  85. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  86. # get information we want from svd
  87. data.append(np.mean(l_svd_data))
  88. data.append(np.median(l_svd_data))
  89. data.append(np.percentile(l_svd_data, 25))
  90. data.append(np.percentile(l_svd_data, 75))
  91. data.append(np.var(l_svd_data))
  92. # convert into numpy array after computing all stats
  93. data = np.asarray(data)
  94. if data_type == 'sub_blocks_area':
  95. block = np.asarray(block)
  96. width, height, _= block.shape
  97. sub_width, sub_height = int(width / 8), int(height / 8)
  98. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  99. data = []
  100. for sub_b in sub_blocks:
  101. # by default use the whole lab L canal
  102. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  103. area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
  104. data.append(area_under_curve)
  105. # convert into numpy array after computing all stats
  106. data = np.asarray(data)
  107. if data_type == 'sub_blocks_area_normed':
  108. block = np.asarray(block)
  109. width, height, _= block.shape
  110. sub_width, sub_height = int(width / 8), int(height / 8)
  111. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  112. data = []
  113. for sub_b in sub_blocks:
  114. # by default use the whole lab L canal
  115. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  116. l_svd_data = utils.normalize_arr(l_svd_data)
  117. area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
  118. data.append(area_under_curve)
  119. # convert into numpy array after computing all stats
  120. data = np.asarray(data)
  121. if data_type == 'mscn_var_4':
  122. data = _get_mscn_variance(block, (100, 100))
  123. if data_type == 'mscn_var_16':
  124. data = _get_mscn_variance(block, (50, 50))
  125. if data_type == 'mscn_var_64':
  126. data = _get_mscn_variance(block, (25, 25))
  127. if data_type == 'mscn_var_16_max':
  128. data = _get_mscn_variance(block, (50, 50))
  129. data = np.asarray(data)
  130. size = int(len(data) / 4)
  131. indices = data.argsort()[-size:][::-1]
  132. data = data[indices]
  133. if data_type == 'mscn_var_64_max':
  134. data = _get_mscn_variance(block, (25, 25))
  135. data = np.asarray(data)
  136. size = int(len(data) / 4)
  137. indices = data.argsort()[-size:][::-1]
  138. data = data[indices]
  139. if data_type == 'ica_diff':
  140. current_image = metrics.get_LAB_L(block)
  141. ica = FastICA(n_components=50)
  142. ica.fit(current_image)
  143. image_ica = ica.fit_transform(current_image)
  144. image_restored = ica.inverse_transform(image_ica)
  145. final_image = utils.normalize_2D_arr(image_restored)
  146. final_image = np.array(final_image * 255, 'uint8')
  147. sv_values = utils.normalize_arr(metrics.get_SVD_s(current_image))
  148. ica_sv_values = utils.normalize_arr(metrics.get_SVD_s(final_image))
  149. data = abs(np.array(sv_values) - np.array(ica_sv_values))
  150. if data_type == 'svd_trunc_diff':
  151. current_image = metrics.get_LAB_L(block)
  152. svd = TruncatedSVD(n_components=30, n_iter=100, random_state=42)
  153. transformed_image = svd.fit_transform(current_image)
  154. restored_image = svd.inverse_transform(transformed_image)
  155. reduced_image = (current_image - restored_image)
  156. U, s, V = metrics.get_SVD(reduced_image)
  157. data = s
  158. if data_type == 'ipca_diff':
  159. current_image = metrics.get_LAB_L(block)
  160. transformer = IncrementalPCA(n_components=20, batch_size=25)
  161. transformed_image = transformer.fit_transform(current_image)
  162. restored_image = transformer.inverse_transform(transformed_image)
  163. reduced_image = (current_image - restored_image)
  164. U, s, V = metrics.get_SVD(reduced_image)
  165. data = s
  166. if data_type == 'svd_reconstruct':
  167. reconstructed_interval = (90, 200)
  168. begin, end = reconstructed_interval
  169. lab_img = metrics.get_LAB_L(block)
  170. lab_img = np.array(lab_img, 'uint8')
  171. U, s, V = lin_svd(lab_img, full_matrices=True)
  172. smat = np.zeros((end-begin, end-begin), dtype=complex)
  173. smat[:, :] = np.diag(s[begin:end])
  174. output_img = np.dot(U[:, begin:end], np.dot(smat, V[begin:end, :]))
  175. output_img = np.array(output_img, 'uint8')
  176. data = metrics.get_SVD_s(output_img)
  177. if 'sv_std_filters' in data_type:
  178. # convert into lab by default to apply filters
  179. lab_img = metrics.get_LAB_L(block)
  180. arr = np.array(lab_img)
  181. images = []
  182. # Apply list of filter on arr
  183. images.append(medfilt2d(arr, [3, 3]))
  184. images.append(medfilt2d(arr, [5, 5]))
  185. images.append(wiener(arr, [3, 3]))
  186. images.append(wiener(arr, [5, 5]))
  187. # By default computation of current block image
  188. s_arr = metrics.get_SVD_s(arr)
  189. sv_vector = [s_arr]
  190. # for each new image apply SVD and get SV
  191. for img in images:
  192. s = metrics.get_SVD_s(img)
  193. sv_vector.append(s)
  194. sv_array = np.array(sv_vector)
  195. _, len = sv_array.shape
  196. sv_std = []
  197. # normalize each SV vectors and compute standard deviation for each sub vectors
  198. for i in range(len):
  199. sv_array[:, i] = utils.normalize_arr(sv_array[:, i])
  200. sv_std.append(np.std(sv_array[:, i]))
  201. indices = []
  202. if 'lowest' in data_type:
  203. indices = get_lowest_values(sv_std, 200)
  204. if 'highest' in data_type:
  205. indices = get_highest_values(sv_std, 200)
  206. # data are arranged following std trend computed
  207. data = s_arr[indices]
  208. # with the use of wavelet
  209. if 'wave_sv_std_filters' in data_type:
  210. # convert into lab by default to apply filters
  211. lab_img = metrics.get_LAB_L(block)
  212. arr = np.array(lab_img)
  213. images = []
  214. # Apply list of filter on arr
  215. images.append(medfilt2d(arr, [3, 3]))
  216. images.append(medfilt2d(arr, [5, 5]))
  217. images.append(medfilt2d(arr, [7, 7]))
  218. images.append(wiener(arr, [3, 3]))
  219. images.append(wiener(arr, [4, 4]))
  220. images.append(wiener(arr, [5, 5]))
  221. images.append(w2d(arr, 'haar', 2))
  222. images.append(w2d(arr, 'haar', 3))
  223. images.append(w2d(arr, 'haar', 4))
  224. # By default computation of current block image
  225. s_arr = metrics.get_SVD_s(arr)
  226. sv_vector = [s_arr]
  227. # for each new image apply SVD and get SV
  228. for img in images:
  229. s = metrics.get_SVD_s(img)
  230. sv_vector.append(s)
  231. sv_array = np.array(sv_vector)
  232. _, len = sv_array.shape
  233. sv_std = []
  234. # normalize each SV vectors and compute standard deviation for each sub vectors
  235. for i in range(len):
  236. sv_array[:, i] = utils.normalize_arr(sv_array[:, i])
  237. sv_std.append(np.std(sv_array[:, i]))
  238. indices = []
  239. if 'lowest' in data_type:
  240. indices = get_lowest_values(sv_std, 200)
  241. if 'highest' in data_type:
  242. indices = get_highest_values(sv_std, 200)
  243. # data are arranged following std trend computed
  244. data = s_arr[indices]
  245. if 'filters_statistics' in data_type:
  246. img_width, img_height = 200, 200
  247. lab_img = metrics.get_LAB_L(block)
  248. arr = np.array(lab_img)
  249. # compute all filters statistics
  250. def get_stats(arr, I_filter):
  251. e1 = np.abs(arr - I_filter)
  252. L = np.array(e1)
  253. mu0 = np.mean(L)
  254. A = L - mu0
  255. H = A * A
  256. E = np.sum(H) / (img_width * img_height)
  257. P = np.sqrt(E)
  258. return mu0, P
  259. stats = []
  260. kernel = np.ones((3,3),np.float32)/9
  261. stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
  262. kernel = np.ones((5,5),np.float32)/25
  263. stats.append(get_stats(arr, cv2.filter2D(arr,-1,kernel)))
  264. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 0.5)))
  265. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1)))
  266. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1.5)))
  267. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 0.5)))
  268. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1)))
  269. stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1.5)))
  270. stats.append(get_stats(arr, medfilt2d(arr, [3, 3])))
  271. stats.append(get_stats(arr, medfilt2d(arr, [5, 5])))
  272. stats.append(get_stats(arr, wiener(arr, [3, 3])))
  273. stats.append(get_stats(arr, wiener(arr, [5, 5])))
  274. wave = w2d(arr, 'db1', 2)
  275. stats.append(get_stats(arr, np.array(wave, 'float64')))
  276. data = []
  277. for stat in stats:
  278. data.append(stat[0])
  279. for stat in stats:
  280. data.append(stat[1])
  281. data = np.array(data)
  282. return data
  283. def get_highest_values(arr, n):
  284. return np.array(arr).argsort()[-n:][::-1]
  285. def get_lowest_values(arr, n):
  286. return np.array(arr).argsort()[::-1][-n:][::-1]
  287. def w2d(arr, mode='haar', level=1):
  288. #convert to float
  289. imArray = arr
  290. np.divide(imArray, 255)
  291. # compute coefficients
  292. coeffs=pywt.wavedec2(imArray, mode, level=level)
  293. #Process Coefficients
  294. coeffs_H=list(coeffs)
  295. coeffs_H[0] *= 0
  296. # reconstruction
  297. imArray_H = pywt.waverec2(coeffs_H, mode)
  298. imArray_H *= 255
  299. imArray_H = np.uint8(imArray_H)
  300. return imArray_H
  301. def _get_mscn_variance(block, sub_block_size=(50, 50)):
  302. blocks = processing.divide_in_blocks(block, sub_block_size)
  303. data = []
  304. for block in blocks:
  305. mscn_coefficients = processing.get_mscn_coefficients(block)
  306. flat_coeff = mscn_coefficients.flatten()
  307. data.append(np.var(flat_coeff))
  308. return np.sort(data)
  309. def get_renderer_scenes_indices(renderer_name):
  310. if renderer_name not in renderer_choices:
  311. raise ValueError("Unknown renderer name")
  312. if renderer_name == 'all':
  313. return scenes_indices
  314. else:
  315. return context_vars[renderer_name + _scenes_indices_prefix]
  316. def get_renderer_scenes_names(renderer_name):
  317. if renderer_name not in renderer_choices:
  318. raise ValueError("Unknown renderer name")
  319. if renderer_name == 'all':
  320. return scenes_names
  321. else:
  322. return context_vars[renderer_name + _scenes_names_prefix]