-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·336 lines (291 loc) · 11.6 KB
/
Copy pathmain.py
File metadata and controls
executable file
·336 lines (291 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python3
#
import numpy as np
from midiutil import MIDIFile
from PIL import Image
import matplotlib.image as mpimg
import random
import gdal
import os
from pyproj import Proj, transform
import osr
import operator
import subprocess
def arpeggio(x):
dic = {0:0,
1:4,
2:7,
3:12,
4:16,
5:12,
6:7,
7:4,
8:0,
9:4,
10:7,
11:12,
12:16,
13:12,
14:7,
15:4}
return dic[x]
# Generates a thematic song in MIDI from input raster data. Four themes are available with more to come: A calming water song (WATER), a celtic-inspired country grassland style (GRASSLAND), soothing harp (FOREST), and an intimidating mountain war march (MOUNTAIN). Further fine-tuning can be done within the method.
def generate_song(image, theme="WATER", output="/tmp/default.mid"):
rhythms_lst = {"WATER":[ [1, 1, 1, 0.5, 0.5],
[2, 2],
[0.5, 0.5, 0.5, 1.5, 0.5, 0.25, 0.25],
[1, 1, 1, 1]],
"MOUNTAIN":[ [0.25, 0.25, 0.25, 0.25, 0.5, 0.5, 1, 0.5, 0.5],
[1, 1, 1, 0.5, 0.5],
[1, -2.5, 0.5],
[0.25, 0.25, 0.25, 1.25, -0.5, 0.5, 0.25, 0.25, 0.25, 0.25]],
"GRASSLAND":[[1, 0.5, 1, 0.5, 1, 0.5, 1.5],
[1.5, 1.5, 0.5, 0.5, 0.5, 1.5],
[6]],
"FOREST":[ [0.5, -0.5, 0.5, -0.5, 0.25, 0.75],
[-0.5, 0.5, -0.5, 0.25, 0.25, 0.25, 0.25, 0.5, -0.5, 0.5]],
"ARCTIC":[ [3],
[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25],
[-3],
[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25]]
}
accompaniment_lst = {"WATER":[[3, 1],
[1, 1, 1, 1],
[1.5, 0.5, 0.5, 1],
[2, 2]],
"MOUNTAIN":[ [0.5, 0.5, -1.5, 0.5, 0.5 -0.5],
[0.5, 0.5, 1, 0.5,0.5, 1],
[-1, 0.25,0.25,0.25,0.25, -0.5, 0.5],
[1.5, 0.5, 0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25]],
"GRASSLAND":[[3, 3],
[6],
[2, 1, 2, 1]],
"FOREST":[ [0.25] * 16,
[0.25] * 16],
"ARCTIC":[ [0.5, 0.25, 0.25, 0.5, 0.25, 0.25],
[0.5, 0.25, 0.25, 0.5, 0.25, 0.25],
[0.5, 0.25, 0.25, 0.5, 0.25, 0.25],
[0.5, 0.25, 0.25, 0.5, 0.25, 0.25]]
}
# Defines how many scale degrees will be used in each theme
degrees_lst = {"WATER":5,
"MOUNTAIN":6,
"GRASSLAND":4,
"FOREST":3,
"ARCTIC":5}
# Set the tempo of each theme
tempo={"WATER":130,
"MOUNTAIN":100,
"GRASSLAND":130,
"FOREST":60,
"ARCTIC":110}
rhythms = rhythms_lst[theme]
accompaniment = accompaniment_lst[theme]
# Initialize MIDI object
MyMIDI = MIDIFile(1)
track = 0
time = 0
MyMIDI.addTrackName(track, time, "SONGSAT")
MyMIDI.addTempo(track, time, tempo[theme])
# Read in raster data
landsat_clip_path = image
img = mpimg.imread(landsat_clip_path).flatten()
# Eliminate any possible nodata values included to avoid widely repeated notes
pentatonic = list(filter(lambda x: x != 255, img))
# Normalize raster data to the number of degrees of the scale
pentatonic = list(map(lambda x: (x % degrees_lst[theme]) + 1, pentatonic))
# Shorten the data to be a reasonable song length
melody = pentatonic[::int(len(pentatonic) / 650)]
# Initialize the scale used for the thematic tune
scales = {"WATER":{1:0, 2:2, 3:4, 4:7, 5:9},
"MOUNTAIN":{1:0, 2:2, 3:3, 4:4, 5:5, 6:8},
"GRASSLAND":{1:0, 2:4, 3:7, 4:9},
"FOREST":{1:0, 2:2, 3:4},
"ARCTIC":{1:0, 2:2, 3:3, 4:7, 5:11}}
scale = scales[theme]
x = 0
# The base octave that this song begins at. Set to an octave above middle C (60) with modifier.
octaves = {"WATER":60 + 12,
"MOUNTAIN":60 - 12,
"GRASSLAND":60 + 6,
"FOREST":60,
"ARCTIC":60 + 16}
octave = octaves[theme] + random.randint(-6, 6)
# Keep track of when midi events happen
time_upper = 0
time_lower = 0
# Loop through the melody notes and apply rhythms
while x < len(melody):
try:
rhythm_indice = random.randint(0, len(rhythms) - 1)
r = rhythms[rhythm_indice]
b = accompaniment[rhythm_indice]
starting_note = scale[melody[x]]
last_note = scale[melody[x]] + octave
for i in range(0, len(r)):
note = scale[melody[x]] + octave
#Ugly jump - keep melody smooth.
if abs(note - last_note) > 8:
if note > last_note:
note -= 12
octave -= 12
else:
note += 12
octave += 12
if note > 255:
note -= 12
octave -= 12
duration = r[i]
last_note = note
if duration > 0:
MyMIDI.addNote(track, 0, note + 12, time_upper, duration, 100)
# Mountain sounds great with octave doubling.
if theme == "MOUNTAIN":
MyMIDI.addNote(track, 0, note, time_upper, duration, 100)
else:
# Rests are denoted with negative beats.
duration *= -1
time_upper += duration
x+=1
for i in range(0, len(b)):
if theme != "MOUNTAIN":
lnote = int(starting_note + octave - 12 - ((i % 2) * 12))
else:
lnote = int(starting_note + octave - 12)
if lnote < 0:
lnote += 12
if lnote > 255:
lnote -= 12
#print(lnote)
if b[i] > 0:
if theme != "FOREST" or theme != "ARCTIC":
MyMIDI.addNote(track, 1,
lnote,
time_lower, b[i], 100)
elif theme == "ARCTIC":
v = i % 3
n = octave - 12
if v == 1:
n = n + 7
elif v == 2:
n = n + 12
MyMIDI.addNote(track, 1,
v, time_lower, b[1], 100)
else:
MyMIDI.addNote(track, 1,
lnote + arpeggio(i),
time_lower, b[i], 100)
else:
b[i] *= -1
time_lower += b[i]
except Exception as e:
print(e)
# Add nice chord to end of water theme
if theme == "WATER":
MyMIDI.addNote(track, 0, 60, time_upper, 4, 100)
MyMIDI.addNote(track, 0, 67, time_upper, 4, 100)
MyMIDI.addNote(track, 0, 72, time_upper, 4, 100)
MyMIDI.addNote(track, 0, 84, time_upper, 4, 100)
# Write out midi file to specified output.
binfile = open(output, 'wb')
MyMIDI.writeFile(binfile)
binfile.close()
def GetExtent(gt,cols,rows):
''' Return list of corner coordinates from a geotransform
@type gt: C{tuple/list}
@param gt: geotransform
@type cols: C{int}
@param cols: number of columns in the dataset
@type rows: C{int}
@param rows: number of rows in the dataset
@rtype: C{[float,...,float]}
@return: coordinates of each corner
'''
ext=[]
xarr=[0,cols]
yarr=[0,rows]
for px in xarr:
for py in yarr:
x=gt[0]+(px*gt[1])+(py*gt[2])
y=gt[3]+(px*gt[4])+(py*gt[5])
ext.append([x,y])
yarr.reverse()
return ext
def getCorners(image_path):
ds=gdal.Open(image_path)
proj = osr.SpatialReference(wkt=ds.GetProjection())
projection_str = "epsg:"+ str(proj.GetAttrValue('AUTHORITY',1))
inProj = Proj(init=projection_str)
outProj = Proj(init='epsg:4326')
gt=ds.GetGeoTransform()
cols = ds.RasterXSize
rows = ds.RasterYSize
ext=GetExtent(gt,cols,rows)
ul_lon = ext[0][0]
ul_lat = ext[0][1]
lr_lon = ext[2][0]
lr_lat = ext[2][1]
ul = inProj(ul_lon, ul_lat, inverse=True)
lr = inProj(lr_lon, lr_lat, inverse=True)
return [[ul[0], ul[1]], [lr[0], lr[1]]]
def getCentroid(image_path):
ext = getCorners(image_path)
ul = ext[0]
lr = ext[1]
md_lat = (ul[0] + lr[0]) / 2.0 + 1
md_lon = (ul[1] + lr[1]) / 2.0 + 1
print(md_lon)
# r = inProj(md_lon, md_lat, inverse=True)
#print(r)
print("{}, {}".format(md_lat, md_lon))
#md_lat_wgs, md_lon_wgs = transform(inProj, outProj, md_lat, md_lon)
return [md_lon, md_lat]
def getClassificationValue(loc):
s = [os.getcwd() + "/getClass/getclass", \
str(loc[0]) , \
str(loc[1])]
p = subprocess.check_output(s)
p = p.decode('UTF-8').replace("\n", "").replace("Value:", "").strip()
return p
def getClassification(image_path, samples=0):
# Samples defines the number of random samples placed in the image to generate
image_location = getCentroid(image_path)
if samples > 0:
p_dict = {}
corners = getCorners(image_path)
ul = corners[0]
lr = corners[1]
random_coords = [[float(random.uniform(ul[0], lr[0])),
float(random.uniform(ul[1], lr[1]))] for _ in range(samples)]
random_coords.append(image_location)
for c in random_coords:
p = getClassificationValue(list(c))
if p not in p_dict.keys():
p_dict[p] = 1
else:
p_dict[p] += 1
#print(p_dict)
return max(p_dict.items(), key=operator.itemgetter(1))[0]
else:
p = getClassificationValue(image_location)
return p
def songSAT(image_path, output_midi="/tmp/OUT.mid"):
p = getClassification(image_path)
if p == "MOUNTAIN":
theme = "MOUNTAIN"
elif p == "0":
theme = "WATER"
elif p == "1":
theme = "FOREST"
elif p == "6" or p == "12" or p == "14" or p == "15":
theme = "GRASSLAND"
else:
theme = "FAILED"
if theme == "FAILED":
print("Sorry! This land cover is not yet supported by SongSAT! Check back again at a later time, or bug one of the developers on the project to implement it!")
else:
generate_song(image_path, theme,output_midi)
#songSAT("/home/alex/Documents/spaceapp/landsat/img.TIF")
#print(getClassification("/home/alex/Documents/spaceapp/landsat/img.TIF", 300))
generate_song("/home/alex/Pictures/52759611_325928751391358_5931559945576120320_n.jpg",
"ARCTIC", "/tmp/out.mid")