|
@@ -1,25 +1,26 @@
|
|
|
-from ipfml import processing, metrics, utils
|
|
|
-from modules.utils.config import *
|
|
|
+# main imports
|
|
|
+import numpy as np
|
|
|
+import sys
|
|
|
|
|
|
+# image transform imports
|
|
|
from PIL import Image
|
|
|
from skimage import color
|
|
|
from sklearn.decomposition import FastICA
|
|
|
from sklearn.decomposition import IncrementalPCA
|
|
|
from sklearn.decomposition import TruncatedSVD
|
|
|
from numpy.linalg import svd as lin_svd
|
|
|
-
|
|
|
from scipy.signal import medfilt2d, wiener, cwt
|
|
|
import pywt
|
|
|
-
|
|
|
import cv2
|
|
|
-import numpy as np
|
|
|
|
|
|
+from ipfml.processing import transform, compression, segmentation
|
|
|
+from ipfml import utils
|
|
|
|
|
|
-_scenes_names_prefix = '_scenes_names'
|
|
|
-_scenes_indices_prefix = '_scenes_indices'
|
|
|
+# modules and config imports
|
|
|
+sys.path.insert(0, '') # trick to enable import of main folder module
|
|
|
|
|
|
-# store all variables from current module context
|
|
|
-context_vars = vars()
|
|
|
+import custom_config as cfg
|
|
|
+from modules.utils import data as dt
|
|
|
|
|
|
|
|
|
def get_svd_data(data_type, block):
|
|
@@ -31,11 +32,11 @@ def get_svd_data(data_type, block):
|
|
|
|
|
|
block_file_path = '/tmp/lab_img.png'
|
|
|
block.save(block_file_path)
|
|
|
- data = processing.get_LAB_L_SVD_s(Image.open(block_file_path))
|
|
|
+ data = transform.get_LAB_L_SVD_s(Image.open(block_file_path))
|
|
|
|
|
|
if data_type == 'mscn':
|
|
|
|
|
|
- img_mscn_revisited = processing.rgb_to_mscn(block)
|
|
|
+ img_mscn_revisited = transform.rgb_to_mscn(block)
|
|
|
|
|
|
# save tmp as img
|
|
|
img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
|
|
@@ -44,47 +45,47 @@ def get_svd_data(data_type, block):
|
|
|
img_block = Image.open(mscn_revisited_file_path)
|
|
|
|
|
|
# extract from temp image
|
|
|
- data = metrics.get_SVD_s(img_block)
|
|
|
+ data = compression.get_SVD_s(img_block)
|
|
|
|
|
|
"""if data_type == 'mscn':
|
|
|
|
|
|
img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
|
|
|
- img_mscn = processing.calculate_mscn_coefficients(img_gray, 7)
|
|
|
- img_mscn_norm = processing.normalize_2D_arr(img_mscn)
|
|
|
+ img_mscn = transform.calculate_mscn_coefficients(img_gray, 7)
|
|
|
+ img_mscn_norm = transform.normalize_2D_arr(img_mscn)
|
|
|
|
|
|
img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')
|
|
|
|
|
|
- data = metrics.get_SVD_s(img_mscn_gray)
|
|
|
+ data = compression.get_SVD_s(img_mscn_gray)
|
|
|
"""
|
|
|
|
|
|
if data_type == 'low_bits_6':
|
|
|
|
|
|
- low_bits_6 = processing.rgb_to_LAB_L_low_bits(block, 6)
|
|
|
- data = metrics.get_SVD_s(low_bits_6)
|
|
|
+ low_bits_6 = transform.rgb_to_LAB_L_low_bits(block, 6)
|
|
|
+ data = compression.get_SVD_s(low_bits_6)
|
|
|
|
|
|
if data_type == 'low_bits_5':
|
|
|
|
|
|
- low_bits_5 = processing.rgb_to_LAB_L_low_bits(block, 5)
|
|
|
- data = metrics.get_SVD_s(low_bits_5)
|
|
|
+ low_bits_5 = transform.rgb_to_LAB_L_low_bits(block, 5)
|
|
|
+ data = compression.get_SVD_s(low_bits_5)
|
|
|
|
|
|
if data_type == 'low_bits_4':
|
|
|
|
|
|
- low_bits_4 = processing.rgb_to_LAB_L_low_bits(block, 4)
|
|
|
- data = metrics.get_SVD_s(low_bits_4)
|
|
|
+ low_bits_4 = transform.rgb_to_LAB_L_low_bits(block, 4)
|
|
|
+ data = compression.get_SVD_s(low_bits_4)
|
|
|
|
|
|
if data_type == 'low_bits_3':
|
|
|
|
|
|
- low_bits_3 = processing.rgb_to_LAB_L_low_bits(block, 3)
|
|
|
- data = metrics.get_SVD_s(low_bits_3)
|
|
|
+ low_bits_3 = transform.rgb_to_LAB_L_low_bits(block, 3)
|
|
|
+ data = compression.get_SVD_s(low_bits_3)
|
|
|
|
|
|
if data_type == 'low_bits_2':
|
|
|
|
|
|
- low_bits_2 = processing.rgb_to_LAB_L_low_bits(block, 2)
|
|
|
- data = metrics.get_SVD_s(low_bits_2)
|
|
|
+ low_bits_2 = transform.rgb_to_LAB_L_low_bits(block, 2)
|
|
|
+ data = compression.get_SVD_s(low_bits_2)
|
|
|
|
|
|
if data_type == 'low_bits_4_shifted_2':
|
|
|
|
|
|
- data = metrics.get_SVD_s(processing.rgb_to_LAB_L_bits(block, (3, 6)))
|
|
|
+ data = compression.get_SVD_s(transform.rgb_to_LAB_L_bits(block, (3, 6)))
|
|
|
|
|
|
if data_type == 'sub_blocks_stats':
|
|
|
|
|
@@ -92,14 +93,14 @@ def get_svd_data(data_type, block):
|
|
|
width, height, _= block.shape
|
|
|
sub_width, sub_height = int(width / 4), int(height / 4)
|
|
|
|
|
|
- sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
+ sub_blocks = segmentation.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
|
|
|
data = []
|
|
|
|
|
|
for sub_b in sub_blocks:
|
|
|
|
|
|
# by default use the whole lab L canal
|
|
|
- l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
|
|
|
+ l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))
|
|
|
|
|
|
# get information we want from svd
|
|
|
data.append(np.mean(l_svd_data))
|
|
@@ -120,14 +121,14 @@ def get_svd_data(data_type, block):
|
|
|
width, height, _= block.shape
|
|
|
sub_width, sub_height = int(width / 4), int(height / 4)
|
|
|
|
|
|
- sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
+ sub_blocks = segmentation.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
|
|
|
data = []
|
|
|
|
|
|
for sub_b in sub_blocks:
|
|
|
|
|
|
# by default use the whole lab L canal
|
|
|
- l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
|
|
|
+ l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))
|
|
|
|
|
|
# get information we want from svd
|
|
|
data.append(np.mean(l_svd_data))
|
|
@@ -145,14 +146,14 @@ def get_svd_data(data_type, block):
|
|
|
width, height, _= block.shape
|
|
|
sub_width, sub_height = int(width / 8), int(height / 8)
|
|
|
|
|
|
- sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
+ sub_blocks = segmentation.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
|
|
|
data = []
|
|
|
|
|
|
for sub_b in sub_blocks:
|
|
|
|
|
|
# by default use the whole lab L canal
|
|
|
- l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
|
|
|
+ l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))
|
|
|
|
|
|
area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
|
|
|
data.append(area_under_curve)
|
|
@@ -166,14 +167,14 @@ def get_svd_data(data_type, block):
|
|
|
width, height, _= block.shape
|
|
|
sub_width, sub_height = int(width / 8), int(height / 8)
|
|
|
|
|
|
- sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
+ sub_blocks = segmentation.divide_in_blocks(block, (sub_width, sub_height))
|
|
|
|
|
|
data = []
|
|
|
|
|
|
for sub_b in sub_blocks:
|
|
|
|
|
|
# by default use the whole lab L canal
|
|
|
- l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
|
|
|
+ l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))
|
|
|
l_svd_data = utils.normalize_arr(l_svd_data)
|
|
|
|
|
|
area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
|
|
@@ -211,7 +212,7 @@ def get_svd_data(data_type, block):
|
|
|
data = data[indices]
|
|
|
|
|
|
if data_type == 'ica_diff':
|
|
|
- current_image = metrics.get_LAB_L(block)
|
|
|
+ current_image = transform.get_LAB_L(block)
|
|
|
|
|
|
ica = FastICA(n_components=50)
|
|
|
ica.fit(current_image)
|
|
@@ -222,14 +223,14 @@ def get_svd_data(data_type, block):
|
|
|
final_image = utils.normalize_2D_arr(image_restored)
|
|
|
final_image = np.array(final_image * 255, 'uint8')
|
|
|
|
|
|
- sv_values = utils.normalize_arr(metrics.get_SVD_s(current_image))
|
|
|
- ica_sv_values = utils.normalize_arr(metrics.get_SVD_s(final_image))
|
|
|
+ sv_values = utils.normalize_arr(compression.get_SVD_s(current_image))
|
|
|
+ ica_sv_values = utils.normalize_arr(compression.get_SVD_s(final_image))
|
|
|
|
|
|
data = abs(np.array(sv_values) - np.array(ica_sv_values))
|
|
|
|
|
|
if data_type == 'svd_trunc_diff':
|
|
|
|
|
|
- current_image = metrics.get_LAB_L(block)
|
|
|
+ current_image = transform.get_LAB_L(block)
|
|
|
|
|
|
svd = TruncatedSVD(n_components=30, n_iter=100, random_state=42)
|
|
|
transformed_image = svd.fit_transform(current_image)
|
|
@@ -237,12 +238,12 @@ def get_svd_data(data_type, block):
|
|
|
|
|
|
reduced_image = (current_image - restored_image)
|
|
|
|
|
|
- U, s, V = metrics.get_SVD(reduced_image)
|
|
|
+ U, s, V = compression.get_SVD(reduced_image)
|
|
|
data = s
|
|
|
|
|
|
if data_type == 'ipca_diff':
|
|
|
|
|
|
- current_image = metrics.get_LAB_L(block)
|
|
|
+ current_image = transform.get_LAB_L(block)
|
|
|
|
|
|
transformer = IncrementalPCA(n_components=20, batch_size=25)
|
|
|
transformed_image = transformer.fit_transform(current_image)
|
|
@@ -250,7 +251,7 @@ def get_svd_data(data_type, block):
|
|
|
|
|
|
reduced_image = (current_image - restored_image)
|
|
|
|
|
|
- U, s, V = metrics.get_SVD(reduced_image)
|
|
|
+ U, s, V = compression.get_SVD(reduced_image)
|
|
|
data = s
|
|
|
|
|
|
if data_type == 'svd_reconstruct':
|
|
@@ -258,7 +259,7 @@ def get_svd_data(data_type, block):
|
|
|
reconstructed_interval = (90, 200)
|
|
|
begin, end = reconstructed_interval
|
|
|
|
|
|
- lab_img = metrics.get_LAB_L(block)
|
|
|
+ lab_img = transform.get_LAB_L(block)
|
|
|
lab_img = np.array(lab_img, 'uint8')
|
|
|
|
|
|
U, s, V = lin_svd(lab_img, full_matrices=True)
|
|
@@ -269,12 +270,12 @@ def get_svd_data(data_type, block):
|
|
|
|
|
|
output_img = np.array(output_img, 'uint8')
|
|
|
|
|
|
- data = metrics.get_SVD_s(output_img)
|
|
|
+ data = compression.get_SVD_s(output_img)
|
|
|
|
|
|
if 'sv_std_filters' in data_type:
|
|
|
|
|
|
# convert into lab by default to apply filters
|
|
|
- lab_img = metrics.get_LAB_L(block)
|
|
|
+ lab_img = transform.get_LAB_L(block)
|
|
|
arr = np.array(lab_img)
|
|
|
images = []
|
|
|
|
|
@@ -285,12 +286,12 @@ def get_svd_data(data_type, block):
|
|
|
images.append(wiener(arr, [5, 5]))
|
|
|
|
|
|
# By default computation of current block image
|
|
|
- s_arr = metrics.get_SVD_s(arr)
|
|
|
+ s_arr = compression.get_SVD_s(arr)
|
|
|
sv_vector = [s_arr]
|
|
|
|
|
|
# for each new image apply SVD and get SV
|
|
|
for img in images:
|
|
|
- s = metrics.get_SVD_s(img)
|
|
|
+ s = compression.get_SVD_s(img)
|
|
|
sv_vector.append(s)
|
|
|
|
|
|
sv_array = np.array(sv_vector)
|
|
@@ -307,10 +308,10 @@ def get_svd_data(data_type, block):
|
|
|
indices = []
|
|
|
|
|
|
if 'lowest' in data_type:
|
|
|
- indices = get_lowest_values(sv_std, 200)
|
|
|
+ indices = utils.get_indices_of_lowest_values(sv_std, 200)
|
|
|
|
|
|
if 'highest' in data_type:
|
|
|
- indices = get_highest_values(sv_std, 200)
|
|
|
+ indices = utils.get_indices_of_highest_values(sv_std, 200)
|
|
|
|
|
|
# data are arranged following std trend computed
|
|
|
data = s_arr[indices]
|
|
@@ -319,7 +320,7 @@ def get_svd_data(data_type, block):
|
|
|
if 'wave_sv_std_filters' in data_type:
|
|
|
|
|
|
# convert into lab by default to apply filters
|
|
|
- lab_img = metrics.get_LAB_L(block)
|
|
|
+ lab_img = transform.get_LAB_L(block)
|
|
|
arr = np.array(lab_img)
|
|
|
images = []
|
|
|
|
|
@@ -335,12 +336,12 @@ def get_svd_data(data_type, block):
|
|
|
images.append(w2d(arr, 'haar', 4))
|
|
|
|
|
|
# By default computation of current block image
|
|
|
- s_arr = metrics.get_SVD_s(arr)
|
|
|
+ s_arr = compression.get_SVD_s(arr)
|
|
|
sv_vector = [s_arr]
|
|
|
|
|
|
# for each new image apply SVD and get SV
|
|
|
for img in images:
|
|
|
- s = metrics.get_SVD_s(img)
|
|
|
+ s = compression.get_SVD_s(img)
|
|
|
sv_vector.append(s)
|
|
|
|
|
|
sv_array = np.array(sv_vector)
|
|
@@ -357,10 +358,10 @@ def get_svd_data(data_type, block):
|
|
|
indices = []
|
|
|
|
|
|
if 'lowest' in data_type:
|
|
|
- indices = get_lowest_values(sv_std, 200)
|
|
|
+ indices = utils.get_indices_of_lowest_values(sv_std, 200)
|
|
|
|
|
|
if 'highest' in data_type:
|
|
|
- indices = get_highest_values(sv_std, 200)
|
|
|
+ indices = utils.get_indices_of_highest_values(sv_std, 200)
|
|
|
|
|
|
# data are arranged following std trend computed
|
|
|
data = s_arr[indices]
|
|
@@ -369,7 +370,7 @@ def get_svd_data(data_type, block):
|
|
|
|
|
|
img_width, img_height = 200, 200
|
|
|
|
|
|
- lab_img = metrics.get_LAB_L(block)
|
|
|
+ lab_img = transform.get_LAB_L(block)
|
|
|
arr = np.array(lab_img)
|
|
|
|
|
|
# compute all filters statistics
|
|
@@ -429,14 +430,6 @@ def get_svd_data(data_type, block):
|
|
|
return data
|
|
|
|
|
|
|
|
|
-def get_highest_values(arr, n):
|
|
|
- return np.array(arr).argsort()[-n:][::-1]
|
|
|
-
|
|
|
-
|
|
|
-def get_lowest_values(arr, n):
|
|
|
- return np.array(arr).argsort()[::-1][-n:][::-1]
|
|
|
-
|
|
|
-
|
|
|
def w2d(arr, mode='haar', level=1):
|
|
|
#convert to float
|
|
|
imArray = arr
|
|
@@ -458,35 +451,14 @@ def w2d(arr, mode='haar', level=1):
|
|
|
|
|
|
def _get_mscn_variance(block, sub_block_size=(50, 50)):
|
|
|
|
|
|
- blocks = processing.divide_in_blocks(block, sub_block_size)
|
|
|
+ blocks = segmentation.divide_in_blocks(block, sub_block_size)
|
|
|
|
|
|
data = []
|
|
|
|
|
|
for block in blocks:
|
|
|
- mscn_coefficients = processing.get_mscn_coefficients(block)
|
|
|
+ mscn_coefficients = transform.get_mscn_coefficients(block)
|
|
|
flat_coeff = mscn_coefficients.flatten()
|
|
|
data.append(np.var(flat_coeff))
|
|
|
|
|
|
return np.sort(data)
|
|
|
|
|
|
-
|
|
|
-def get_renderer_scenes_indices(renderer_name):
|
|
|
-
|
|
|
- if renderer_name not in renderer_choices:
|
|
|
- raise ValueError("Unknown renderer name")
|
|
|
-
|
|
|
- if renderer_name == 'all':
|
|
|
- return scenes_indices
|
|
|
- else:
|
|
|
- return context_vars[renderer_name + _scenes_indices_prefix]
|
|
|
-
|
|
|
-def get_renderer_scenes_names(renderer_name):
|
|
|
-
|
|
|
- if renderer_name not in renderer_choices:
|
|
|
- raise ValueError("Unknown renderer name")
|
|
|
-
|
|
|
- if renderer_name == 'all':
|
|
|
- return scenes_names
|
|
|
- else:
|
|
|
- return context_vars[renderer_name + _scenes_names_prefix]
|
|
|
-
|