Upload 2 files
Browse files
TMIDIX.py
CHANGED
@@ -4716,6 +4716,12 @@ def create_similarity_matrix(list_of_values, matrix_length=0):
|
|
4716 |
|
4717 |
###################################################################################
|
4718 |
|
|
|
|
|
|
|
|
|
|
|
|
|
4719 |
def augment_enhanced_score_notes(enhanced_score_notes,
|
4720 |
timings_divider=16,
|
4721 |
full_sorting=True,
|
@@ -4725,14 +4731,33 @@ def augment_enhanced_score_notes(enhanced_score_notes,
|
|
4725 |
|
4726 |
esn = copy.deepcopy(enhanced_score_notes)
|
4727 |
|
4728 |
-
|
4729 |
-
|
4730 |
-
|
4731 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4732 |
|
4733 |
if full_sorting:
|
4734 |
|
4735 |
-
# Sorting by patch, pitch
|
4736 |
esn.sort(key=lambda x: x[6])
|
4737 |
esn.sort(key=lambda x: x[4], reverse=True)
|
4738 |
esn.sort(key=lambda x: x[1])
|
@@ -6734,7 +6759,7 @@ def escore_notes_to_escore_matrix(escore_notes,
|
|
6734 |
etype, time, duration, channel, pitch, velocity, patch = note
|
6735 |
|
6736 |
time = max(0, time)
|
6737 |
-
duration = max(
|
6738 |
channel = max(0, min(15, channel))
|
6739 |
pitch = max(0, min(127, pitch))
|
6740 |
velocity = max(0, min(127, velocity))
|
@@ -6874,7 +6899,7 @@ def escore_notes_to_binary_matrix(escore_notes,
|
|
6874 |
etype, time, duration, chan, pitch, velocity, pat = note
|
6875 |
|
6876 |
time = max(0, time)
|
6877 |
-
duration = max(
|
6878 |
chan = max(0, min(15, chan))
|
6879 |
pitch = max(0, min(127, pitch))
|
6880 |
velocity = max(0, min(127, velocity))
|
@@ -8015,6 +8040,326 @@ def summarize_escore_notes(escore_notes,
|
|
8015 |
|
8016 |
###################################################################################
|
8017 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8018 |
# This is the end of the TMIDI X Python module
|
8019 |
|
8020 |
###################################################################################
|
|
|
4716 |
|
4717 |
###################################################################################
|
4718 |
|
4719 |
+
def ceil_with_precision(value, decimal_places):
|
4720 |
+
factor = 10 ** decimal_places
|
4721 |
+
return math.ceil(value * factor) / factor
|
4722 |
+
|
4723 |
+
###################################################################################
|
4724 |
+
|
4725 |
def augment_enhanced_score_notes(enhanced_score_notes,
|
4726 |
timings_divider=16,
|
4727 |
full_sorting=True,
|
|
|
4731 |
|
4732 |
esn = copy.deepcopy(enhanced_score_notes)
|
4733 |
|
4734 |
+
pe = enhanced_score_notes[0]
|
4735 |
+
|
4736 |
+
abs_time = max(0, int(enhanced_score_notes[0][1] / timings_divider))
|
4737 |
+
|
4738 |
+
for i, e in enumerate(esn):
|
4739 |
+
|
4740 |
+
dtime = (e[1] / timings_divider) - (pe[1] / timings_divider)
|
4741 |
+
|
4742 |
+
if 0.5 < dtime < 1:
|
4743 |
+
dtime = 1
|
4744 |
+
|
4745 |
+
else:
|
4746 |
+
dtime = int(dtime)
|
4747 |
+
|
4748 |
+
e[1] = max(0, abs_time + timings_shift)
|
4749 |
+
|
4750 |
+
e[2] = max(1, int(e[2] / timings_divider)) + timings_shift
|
4751 |
+
|
4752 |
+
e[4] = max(1, min(127, e[4] + pitch_shift))
|
4753 |
+
|
4754 |
+
abs_time += dtime
|
4755 |
+
|
4756 |
+
pe = enhanced_score_notes[i]
|
4757 |
|
4758 |
if full_sorting:
|
4759 |
|
4760 |
+
# Sorting by patch, reverse pitch and start-time
|
4761 |
esn.sort(key=lambda x: x[6])
|
4762 |
esn.sort(key=lambda x: x[4], reverse=True)
|
4763 |
esn.sort(key=lambda x: x[1])
|
|
|
6759 |
etype, time, duration, channel, pitch, velocity, patch = note
|
6760 |
|
6761 |
time = max(0, time)
|
6762 |
+
duration = max(1, duration)
|
6763 |
channel = max(0, min(15, channel))
|
6764 |
pitch = max(0, min(127, pitch))
|
6765 |
velocity = max(0, min(127, velocity))
|
|
|
6899 |
etype, time, duration, chan, pitch, velocity, pat = note
|
6900 |
|
6901 |
time = max(0, time)
|
6902 |
+
duration = max(1, duration)
|
6903 |
chan = max(0, min(15, chan))
|
6904 |
pitch = max(0, min(127, pitch))
|
6905 |
velocity = max(0, min(127, velocity))
|
|
|
8040 |
|
8041 |
###################################################################################
|
8042 |
|
8043 |
+
def compress_patches_in_escore_notes(escore_notes,
|
8044 |
+
num_patches=4,
|
8045 |
+
group_patches=False
|
8046 |
+
):
|
8047 |
+
|
8048 |
+
if num_patches > 4:
|
8049 |
+
n_patches = 4
|
8050 |
+
elif num_patches < 1:
|
8051 |
+
n_patches = 1
|
8052 |
+
else:
|
8053 |
+
n_patches = num_patches
|
8054 |
+
|
8055 |
+
if group_patches:
|
8056 |
+
patches_set = sorted(set([e[6] for e in c]))
|
8057 |
+
trg_patch_list = []
|
8058 |
+
seen = []
|
8059 |
+
for p in patches_set:
|
8060 |
+
if p // 8 not in seen:
|
8061 |
+
trg_patch_list.append(p)
|
8062 |
+
seen.append(p // 8)
|
8063 |
+
|
8064 |
+
trg_patch_list = sorted(trg_patch_list)
|
8065 |
+
|
8066 |
+
else:
|
8067 |
+
trg_patch_list = sorted(set([e[6] for e in c]))
|
8068 |
+
|
8069 |
+
if 128 in trg_patch_list and n_patches > 1:
|
8070 |
+
trg_patch_list = trg_patch_list[:n_patches-1] + [128]
|
8071 |
+
else:
|
8072 |
+
trg_patch_list = trg_patch_list[:n_patches]
|
8073 |
+
|
8074 |
+
new_escore_notes = []
|
8075 |
+
|
8076 |
+
for e in escore_notes:
|
8077 |
+
if e[6] in trg_patch_list:
|
8078 |
+
new_escore_notes.append(e)
|
8079 |
+
|
8080 |
+
return new_escore_notes
|
8081 |
+
|
8082 |
+
###################################################################################
|
8083 |
+
|
8084 |
+
def compress_patches_in_escore_notes_chords(escore_notes,
|
8085 |
+
max_num_patches_per_chord=4,
|
8086 |
+
group_patches=True,
|
8087 |
+
root_grouped_patches=False
|
8088 |
+
):
|
8089 |
+
|
8090 |
+
if max_num_patches_per_chord > 4:
|
8091 |
+
n_patches = 4
|
8092 |
+
elif max_num_patches_per_chord < 1:
|
8093 |
+
n_patches = 1
|
8094 |
+
else:
|
8095 |
+
n_patches = max_num_patches_per_chord
|
8096 |
+
|
8097 |
+
cscore = chordify_score([1000, sorted(escore_notes, key=lambda x: (x[1], x[6]))])
|
8098 |
+
|
8099 |
+
new_escore_notes = []
|
8100 |
+
|
8101 |
+
for c in cscore:
|
8102 |
+
|
8103 |
+
if group_patches:
|
8104 |
+
patches_set = sorted(set([e[6] for e in c]))
|
8105 |
+
trg_patch_list = []
|
8106 |
+
seen = []
|
8107 |
+
for p in patches_set:
|
8108 |
+
if p // 8 not in seen:
|
8109 |
+
trg_patch_list.append(p)
|
8110 |
+
seen.append(p // 8)
|
8111 |
+
|
8112 |
+
trg_patch_list = sorted(trg_patch_list)
|
8113 |
+
|
8114 |
+
else:
|
8115 |
+
trg_patch_list = sorted(set([e[6] for e in c]))
|
8116 |
+
|
8117 |
+
if 128 in trg_patch_list and n_patches > 1:
|
8118 |
+
trg_patch_list = trg_patch_list[:n_patches-1] + [128]
|
8119 |
+
else:
|
8120 |
+
trg_patch_list = trg_patch_list[:n_patches]
|
8121 |
+
|
8122 |
+
for ccc in c:
|
8123 |
+
|
8124 |
+
cc = copy.deepcopy(ccc)
|
8125 |
+
|
8126 |
+
if group_patches:
|
8127 |
+
if cc[6] // 8 in [t // 8 for t in trg_patch_list]:
|
8128 |
+
if root_grouped_patches:
|
8129 |
+
cc[6] = (cc[6] // 8) * 8
|
8130 |
+
new_escore_notes.append(cc)
|
8131 |
+
|
8132 |
+
else:
|
8133 |
+
if cc[6] in trg_patch_list:
|
8134 |
+
new_escore_notes.append(cc)
|
8135 |
+
|
8136 |
+
return new_escore_notes
|
8137 |
+
|
8138 |
+
###################################################################################
|
8139 |
+
|
8140 |
+
def escore_notes_to_image_matrix(escore_notes,
|
8141 |
+
num_img_channels=3,
|
8142 |
+
filter_out_zero_rows=False,
|
8143 |
+
filter_out_duplicate_rows=False,
|
8144 |
+
flip_matrix=False,
|
8145 |
+
reverse_matrix=False
|
8146 |
+
):
|
8147 |
+
|
8148 |
+
escore_notes = sorted(escore_notes, key=lambda x: (x[1], x[6]))
|
8149 |
+
|
8150 |
+
if num_img_channels > 1:
|
8151 |
+
n_mat_channels = 3
|
8152 |
+
else:
|
8153 |
+
n_mat_channels = 1
|
8154 |
+
|
8155 |
+
if escore_notes:
|
8156 |
+
last_time = escore_notes[-1][1]
|
8157 |
+
last_notes = [e for e in escore_notes if e[1] == last_time]
|
8158 |
+
max_last_dur = max([e[2] for e in last_notes])
|
8159 |
+
|
8160 |
+
time_range = last_time+max_last_dur
|
8161 |
+
|
8162 |
+
escore_matrix = []
|
8163 |
+
|
8164 |
+
escore_matrix = [[0] * 128 for _ in range(time_range)]
|
8165 |
+
|
8166 |
+
for note in escore_notes:
|
8167 |
+
|
8168 |
+
etype, time, duration, chan, pitch, velocity, pat = note
|
8169 |
+
|
8170 |
+
time = max(0, time)
|
8171 |
+
duration = max(2, duration)
|
8172 |
+
chan = max(0, min(15, chan))
|
8173 |
+
pitch = max(0, min(127, pitch))
|
8174 |
+
velocity = max(0, min(127, velocity))
|
8175 |
+
patch = max(0, min(128, pat))
|
8176 |
+
|
8177 |
+
if chan != 9:
|
8178 |
+
pat = patch + 128
|
8179 |
+
else:
|
8180 |
+
pat = 127
|
8181 |
+
|
8182 |
+
seen_pats = []
|
8183 |
+
|
8184 |
+
for t in range(time, min(time + duration, time_range)):
|
8185 |
+
|
8186 |
+
mat_value = escore_matrix[t][pitch]
|
8187 |
+
|
8188 |
+
mat_value_0 = (mat_value // (256 * 256)) % 256
|
8189 |
+
mat_value_1 = (mat_value // 256) % 256
|
8190 |
+
|
8191 |
+
cur_num_chans = 0
|
8192 |
+
|
8193 |
+
if 0 < mat_value < 256 and pat not in seen_pats:
|
8194 |
+
cur_num_chans = 1
|
8195 |
+
elif 256 < mat_value < (256 * 256) and pat not in seen_pats:
|
8196 |
+
cur_num_chans = 2
|
8197 |
+
|
8198 |
+
if cur_num_chans < n_mat_channels:
|
8199 |
+
|
8200 |
+
if n_mat_channels == 1:
|
8201 |
+
|
8202 |
+
escore_matrix[t][pitch] = pat
|
8203 |
+
seen_pats.append(pat)
|
8204 |
+
|
8205 |
+
elif n_mat_channels == 3:
|
8206 |
+
|
8207 |
+
if cur_num_chans == 0:
|
8208 |
+
escore_matrix[t][pitch] = pat
|
8209 |
+
seen_pats.append(pat)
|
8210 |
+
elif cur_num_chans == 1:
|
8211 |
+
escore_matrix[t][pitch] = (256 * 256 * mat_value_0) + (256 * pat)
|
8212 |
+
seen_pats.append(pat)
|
8213 |
+
elif cur_num_chans == 2:
|
8214 |
+
escore_matrix[t][pitch] = (256 * 256 * mat_value_0) + (256 * mat_value_1) + pat
|
8215 |
+
seen_pats.append(pat)
|
8216 |
+
|
8217 |
+
if filter_out_zero_rows:
|
8218 |
+
escore_matrix = [e for e in escore_matrix if sum(e) != 0]
|
8219 |
+
|
8220 |
+
if filter_out_duplicate_rows:
|
8221 |
+
|
8222 |
+
dd_escore_matrix = []
|
8223 |
+
|
8224 |
+
pr = [-1] * 128
|
8225 |
+
for e in escore_matrix:
|
8226 |
+
if e != pr:
|
8227 |
+
dd_escore_matrix.append(e)
|
8228 |
+
pr = e
|
8229 |
+
|
8230 |
+
escore_matrix = dd_escore_matrix
|
8231 |
+
|
8232 |
+
if flip_matrix:
|
8233 |
+
|
8234 |
+
temp_matrix = []
|
8235 |
+
|
8236 |
+
for m in escore_matrix:
|
8237 |
+
temp_matrix.append(m[::-1])
|
8238 |
+
|
8239 |
+
escore_matrix = temp_matrix
|
8240 |
+
|
8241 |
+
if reverse_matrix:
|
8242 |
+
escore_matrix = escore_matrix[::-1]
|
8243 |
+
|
8244 |
+
return escore_matrix
|
8245 |
+
|
8246 |
+
else:
|
8247 |
+
return None
|
8248 |
+
|
8249 |
+
###################################################################################
|
8250 |
+
|
8251 |
+
def find_value_power(value, number):
|
8252 |
+
return math.floor(math.log(value, number))
|
8253 |
+
|
8254 |
+
###################################################################################
|
8255 |
+
|
8256 |
+
def image_matrix_to_original_escore_notes(image_matrix,
|
8257 |
+
velocity=-1
|
8258 |
+
):
|
8259 |
+
|
8260 |
+
result = []
|
8261 |
+
|
8262 |
+
for j in range(len(image_matrix[0])):
|
8263 |
+
|
8264 |
+
count = 1
|
8265 |
+
|
8266 |
+
for i in range(1, len(image_matrix)):
|
8267 |
+
|
8268 |
+
if image_matrix[i][j] != 0 and image_matrix[i][j] == image_matrix[i-1][j]:
|
8269 |
+
count += 1
|
8270 |
+
|
8271 |
+
else:
|
8272 |
+
if count > 1:
|
8273 |
+
result.append([i-count, count, j, image_matrix[i-1][j]])
|
8274 |
+
|
8275 |
+
else:
|
8276 |
+
if image_matrix[i-1][j] != 0:
|
8277 |
+
result.append([i-count, count, j, image_matrix[i-1][j]])
|
8278 |
+
|
8279 |
+
count = 1
|
8280 |
+
|
8281 |
+
if count > 1:
|
8282 |
+
result.append([len(image_matrix)-count, count, j, image_matrix[-1][j]])
|
8283 |
+
|
8284 |
+
else:
|
8285 |
+
if image_matrix[i-1][j] != 0:
|
8286 |
+
result.append([i-count, count, j, image_matrix[i-1][j]])
|
8287 |
+
|
8288 |
+
result.sort(key=lambda x: (x[0], -x[2]))
|
8289 |
+
|
8290 |
+
original_escore_notes = []
|
8291 |
+
|
8292 |
+
vel = velocity
|
8293 |
+
|
8294 |
+
for r in result:
|
8295 |
+
|
8296 |
+
if velocity == -1:
|
8297 |
+
vel = max(40, r[2])
|
8298 |
+
|
8299 |
+
ptc0 = 0
|
8300 |
+
ptc1 = 0
|
8301 |
+
ptc2 = 0
|
8302 |
+
|
8303 |
+
if find_value_power(r[3], 256) == 0:
|
8304 |
+
ptc0 = r[3] % 256
|
8305 |
+
|
8306 |
+
elif find_value_power(r[3], 256) == 1:
|
8307 |
+
ptc0 = r[3] // 256
|
8308 |
+
ptc1 = (r[3] // 256) % 256
|
8309 |
+
|
8310 |
+
elif find_value_power(r[3], 256) == 2:
|
8311 |
+
ptc0 = (r[3] // 256) // 256
|
8312 |
+
ptc1 = (r[3] // 256) % 256
|
8313 |
+
ptc2 = r[3] % 256
|
8314 |
+
|
8315 |
+
ptcs = [ptc0, ptc1, ptc2]
|
8316 |
+
patches = [p for p in ptcs if p != 0]
|
8317 |
+
|
8318 |
+
for i, p in enumerate(patches):
|
8319 |
+
|
8320 |
+
if p < 128:
|
8321 |
+
patch = 128
|
8322 |
+
channel = 9
|
8323 |
+
|
8324 |
+
else:
|
8325 |
+
patch = p % 128
|
8326 |
+
chan = p // 8
|
8327 |
+
|
8328 |
+
if chan == 9:
|
8329 |
+
chan += 1
|
8330 |
+
|
8331 |
+
channel = min(15, chan)
|
8332 |
+
|
8333 |
+
original_escore_notes.append(['note', r[0], r[1], channel, r[2], vel, patch])
|
8334 |
+
|
8335 |
+
output_score = sorted(original_escore_notes, key=lambda x: (x[1], -x[4], x[6]))
|
8336 |
+
|
8337 |
+
adjust_score_velocities(output_score, 127)
|
8338 |
+
|
8339 |
+
return output_score
|
8340 |
+
|
8341 |
+
###################################################################################
|
8342 |
+
|
8343 |
+
def escore_notes_delta_times(escore_notes,
|
8344 |
+
timings_index=1,
|
8345 |
+
channels_index=3,
|
8346 |
+
omit_zeros=False,
|
8347 |
+
omit_drums=False
|
8348 |
+
):
|
8349 |
+
|
8350 |
+
if omit_drums:
|
8351 |
+
|
8352 |
+
score = [e for e in escore_notes if e[channels_index] != 9]
|
8353 |
+
dtimes = [score[0][timings_index]] + [b[timings_index]-a[timings_index] for a, b in zip(score[:-1], score[1:])]
|
8354 |
+
|
8355 |
+
else:
|
8356 |
+
dtimes = [escore_notes[0][timings_index]] + [b[timings_index]-a[timings_index] for a, b in zip(escore_notes[:-1], escore_notes[1:])]
|
8357 |
+
|
8358 |
+
if omit_zeros:
|
8359 |
+
dtimes = [d for d in dtimes if d != 0]
|
8360 |
+
|
8361 |
+
return dtimes
|
8362 |
+
|
8363 |
# This is the end of the TMIDI X Python module
|
8364 |
|
8365 |
###################################################################################
|
TPLOTS.py
CHANGED
@@ -38,7 +38,7 @@ r'''############################################################################
|
|
38 |
# !pip install numpy
|
39 |
# !pip install scipy
|
40 |
# !pip install matplotlib
|
41 |
-
# !pip install networkx
|
42 |
# !pip3 install scikit-learn
|
43 |
#
|
44 |
################################################################################
|
@@ -894,10 +894,14 @@ def binary_matrix_to_images(matrix,
|
|
894 |
|
895 |
image_array = []
|
896 |
|
897 |
-
for i in range(0, max(1, matrix.shape[0]
|
898 |
|
899 |
submatrix = matrix[i:i+step, :]
|
900 |
-
|
|
|
|
|
|
|
|
|
901 |
img = Image.fromarray(submatrix * 255).convert('1')
|
902 |
|
903 |
if save_to_array:
|
@@ -909,7 +913,7 @@ def binary_matrix_to_images(matrix,
|
|
909 |
if verbose:
|
910 |
print('Done!')
|
911 |
print('=' * 70)
|
912 |
-
print('Saved', (matrix.shape[0]
|
913 |
print('=' * 70)
|
914 |
|
915 |
if save_to_array:
|
@@ -930,6 +934,162 @@ def images_to_binary_matrix(list_of_images):
|
|
930 |
|
931 |
return original_matrix
|
932 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
933 |
################################################################################
|
934 |
# [WIP] Future dev functions
|
935 |
################################################################################
|
|
|
38 |
# !pip install numpy
|
39 |
# !pip install scipy
|
40 |
# !pip install matplotlib
|
41 |
+
# !pip install networkx
|
42 |
# !pip3 install scikit-learn
|
43 |
#
|
44 |
################################################################################
|
|
|
894 |
|
895 |
image_array = []
|
896 |
|
897 |
+
for i in range(0, max(1, matrix.shape[0]), overlap):
|
898 |
|
899 |
submatrix = matrix[i:i+step, :]
|
900 |
+
|
901 |
+
if submatrix.shape[0] < 128:
|
902 |
+
zeros_array = np.zeros((128-submatrix.shape[0], 128))
|
903 |
+
submatrix = np.vstack((submatrix, zeros_array))
|
904 |
+
|
905 |
img = Image.fromarray(submatrix * 255).convert('1')
|
906 |
|
907 |
if save_to_array:
|
|
|
913 |
if verbose:
|
914 |
print('Done!')
|
915 |
print('=' * 70)
|
916 |
+
print('Saved', (matrix.shape[0] // min(step, overlap))+1, 'imges!')
|
917 |
print('=' * 70)
|
918 |
|
919 |
if save_to_array:
|
|
|
934 |
|
935 |
return original_matrix
|
936 |
|
937 |
+
################################################################################
|
938 |
+
|
939 |
+
def square_image_matrix(image_matrix,
|
940 |
+
matrix_size=128,
|
941 |
+
num_pca_components=5,
|
942 |
+
filter_out_zero_rows=False,
|
943 |
+
return_square_matrix_points=False
|
944 |
+
):
|
945 |
+
|
946 |
+
"""
|
947 |
+
Reduces an arbitrary image matrix to a square image matrix
|
948 |
+
"""
|
949 |
+
|
950 |
+
matrix = np.array(image_matrix)
|
951 |
+
|
952 |
+
if filter_out_zero_rows:
|
953 |
+
matrix = matrix[~np.all(matrix == 0, axis=1)]
|
954 |
+
|
955 |
+
target_rows = matrix_size
|
956 |
+
|
957 |
+
rows_per_group = matrix.shape[0] // target_rows
|
958 |
+
|
959 |
+
compressed_matrix = np.zeros((target_rows, matrix.shape[1]), dtype=np.int32)
|
960 |
+
|
961 |
+
for i in range(target_rows):
|
962 |
+
start_row = i * rows_per_group
|
963 |
+
end_row = (i + 1) * rows_per_group
|
964 |
+
group = matrix[start_row:end_row, :]
|
965 |
+
|
966 |
+
pca = PCA(n_components=num_pca_components)
|
967 |
+
pca.fit(group)
|
968 |
+
|
969 |
+
principal_component = pca.components_[0]
|
970 |
+
contributions = np.dot(group, principal_component)
|
971 |
+
selected_row_index = np.argmax(contributions)
|
972 |
+
|
973 |
+
compressed_matrix[i, :] = group[selected_row_index, :]
|
974 |
+
|
975 |
+
if return_square_matrix_points:
|
976 |
+
filtered_matrix = compressed_matrix[~np.all(compressed_matrix == 0, axis=1)]
|
977 |
+
|
978 |
+
row_indexes, col_indexes = np.where(filtered_matrix != 0)
|
979 |
+
points = np.column_stack((row_indexes, filtered_matrix[row_indexes, col_indexes])).tolist()
|
980 |
+
|
981 |
+
return points
|
982 |
+
|
983 |
+
else:
|
984 |
+
return compressed_matrix.tolist()
|
985 |
+
|
986 |
+
################################################################################
|
987 |
+
|
988 |
+
def image_matrix_to_images(image_matrix,
|
989 |
+
step,
|
990 |
+
overlap,
|
991 |
+
num_img_channels=3,
|
992 |
+
output_folder='./Dataset/',
|
993 |
+
output_img_prefix='image',
|
994 |
+
output_img_ext='.png',
|
995 |
+
save_to_array=False,
|
996 |
+
verbose=True
|
997 |
+
):
|
998 |
+
|
999 |
+
if num_img_channels > 1:
|
1000 |
+
n_mat_channels = 3
|
1001 |
+
|
1002 |
+
else:
|
1003 |
+
n_mat_channels = 1
|
1004 |
+
|
1005 |
+
if not save_to_array:
|
1006 |
+
|
1007 |
+
if verbose:
|
1008 |
+
print('=' * 70)
|
1009 |
+
print('Checking output folder dir...')
|
1010 |
+
|
1011 |
+
os.makedirs(os.path.dirname(output_folder), exist_ok=True)
|
1012 |
+
|
1013 |
+
if verbose:
|
1014 |
+
print('Done!')
|
1015 |
+
|
1016 |
+
if verbose:
|
1017 |
+
print('=' * 70)
|
1018 |
+
print('Writing images...')
|
1019 |
+
|
1020 |
+
matrix = np.array(image_matrix)
|
1021 |
+
|
1022 |
+
image_array = []
|
1023 |
+
|
1024 |
+
for i in range(0, max(1, matrix.shape[0]), overlap):
|
1025 |
+
|
1026 |
+
submatrix = matrix[i:i+step, :]
|
1027 |
+
|
1028 |
+
if submatrix.shape[0] < 128:
|
1029 |
+
zeros_array = np.zeros((128-submatrix.shape[0], 128))
|
1030 |
+
submatrix = np.vstack((submatrix, zeros_array))
|
1031 |
+
|
1032 |
+
if n_mat_channels == 3:
|
1033 |
+
|
1034 |
+
r = (submatrix // (256*256)) % 256
|
1035 |
+
g = (submatrix // 256) % 256
|
1036 |
+
b = submatrix % 256
|
1037 |
+
|
1038 |
+
rgb_image = np.stack((r, g, b), axis=-1).astype(np.uint8)
|
1039 |
+
img = Image.fromarray(rgb_image, 'RGB')
|
1040 |
+
|
1041 |
+
else:
|
1042 |
+
grayscale_image = submatrix.astype(np.uint8)
|
1043 |
+
img = Image.fromarray(grayscale_image, 'L')
|
1044 |
+
|
1045 |
+
if save_to_array:
|
1046 |
+
image_array.append(np.array(img))
|
1047 |
+
|
1048 |
+
else:
|
1049 |
+
img.save(output_folder + output_img_prefix + '_' + str(matrix.shape[1]) + '_' + str(i).zfill(7) + output_img_ext)
|
1050 |
+
|
1051 |
+
if verbose:
|
1052 |
+
print('Done!')
|
1053 |
+
print('=' * 70)
|
1054 |
+
print('Saved', (matrix.shape[0] // min(step, overlap))+1, 'imges!')
|
1055 |
+
print('=' * 70)
|
1056 |
+
|
1057 |
+
if save_to_array:
|
1058 |
+
return np.array(image_array).tolist()
|
1059 |
+
|
1060 |
+
################################################################################
|
1061 |
+
|
1062 |
+
def images_to_image_matrix(list_of_images,
|
1063 |
+
num_img_channels=3
|
1064 |
+
):
|
1065 |
+
|
1066 |
+
if num_img_channels > 1:
|
1067 |
+
n_mat_channels = 3
|
1068 |
+
|
1069 |
+
else:
|
1070 |
+
n_mat_channels = 1
|
1071 |
+
|
1072 |
+
image_array = np.array(list_of_images)
|
1073 |
+
|
1074 |
+
original_matrix = []
|
1075 |
+
|
1076 |
+
for img in image_array:
|
1077 |
+
|
1078 |
+
if num_img_channels == 3:
|
1079 |
+
|
1080 |
+
rgb_array = np.array(img)
|
1081 |
+
|
1082 |
+
matrix = (rgb_array[..., 0].astype(np.int64) * 256*256 +
|
1083 |
+
rgb_array[..., 1].astype(np.int64) * 256 +
|
1084 |
+
rgb_array[..., 2].astype(np.int64))
|
1085 |
+
|
1086 |
+
else:
|
1087 |
+
matrix = np.array(img)
|
1088 |
+
|
1089 |
+
original_matrix.extend(matrix)
|
1090 |
+
|
1091 |
+
return original_matrix
|
1092 |
+
|
1093 |
################################################################################
|
1094 |
# [WIP] Future dev functions
|
1095 |
################################################################################
|