-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChrome.py
156 lines (118 loc) · 4.15 KB
/
Chrome.py
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
# We will use this file for image creation
import random, math
from random import randint
from PIL import Image, ImageFilter
from statistics import mean
from numpy import random, array_split, array
random.seed()
#
# Z = random.random((50, 50)) # Test data
#
# imshow(Z, cmap=get_cmap("Emotion"), interpolation='nearest')
# show()
emotion_dict = {
'love': 1,
'acceptance': 2,
'trust': 3,
'admiration': 4,
'fear': 5,
'awe': 6,
'sadness': 7,
'remorse': 8,
'boredom': 9,
'annoyance': 10,
'aggressiveness': 11,
'interest': 12,
'anticipation': 13,
'serenity': 14,
'joy': 15,
'ecstasy': 16,
'optimism': 17,
'surprise': 18
}
#
# colours = []
# for i in range(31):
# colours.append('#%06X' % randint(0, 0xFFFFFF))
class X:
def eval(self, x, y):
return x
def __str__(self):
return "x"
class Y:
def eval(self, x, y):
return y
def __str__(self):
return "y"
class SinPi:
def __init__(self, prob):
self.arg = build_expression(prob * prob)
def __str__(self):
return "sin(pi*" + str(self.arg) + ")"
def eval(self, x, y):
return math.sin(math.pi * self.arg.eval(x, y))
class CosPi:
def __init__(self, prob):
self.arg = build_expression(prob * prob)
def __str__(self):
return "cos(pi*" + str(self.arg) + ")"
def eval(self, x, y):
return math.cos(math.pi * self.arg.eval(x, y))
class Times:
def __init__(self, prob):
self.lhs = build_expression(prob * prob)
self.rhs = build_expression(prob * prob)
def __str__(self):
return str(self.lhs) + "*" + str(self.rhs)
def eval(self, x, y):
return self.lhs.eval(x, y) * self.rhs.eval(x, y)
def build_expression(prob=0.99):
if random.random() < prob:
return random.choice([SinPi, CosPi, Times])(prob)
else:
return random.choice([X, Y])()
def plot_intensity(emotions, exp, pixels_per_unit=150):
canvas_width = 2 * pixels_per_unit + 1
canvas = Image.new("L", (canvas_width, canvas_width))
for py in range(canvas_width):
for px in range(canvas_width):
# Convert pixel location to [-1,1] coordinates
x = float(px - pixels_per_unit) / pixels_per_unit
y = -float(py - pixels_per_unit) / pixels_per_unit
z = exp.eval(x, y)
# Scale [-1,1] result to [0,255] by taking average over the given emotion labels
intensities = []
for index in range(len(emotions)):
# Improve this formula!
result = int(z * emotions[index] + 118.5*z + 118.5)
intensities.append(result)
intensity = mean(intensities)
xy = (px, py)
canvas.putpixel(xy, int(intensity))
return canvas
def plot_color(emotions, red_exp, green_exp, blue_exp, pixels_per_unit=150):
palettes = array_split(array(emotions), 3)
red_plane = plot_intensity(palettes[0].tolist(), red_exp, pixels_per_unit)
green_plane = plot_intensity(palettes[1].tolist(), green_exp, pixels_per_unit)
blue_plane = plot_intensity(palettes[2].tolist(), blue_exp, pixels_per_unit)
return Image.merge("RGB", (red_plane, green_plane, blue_plane))
def get_emotions_from_labels(labels):
emotions = []
for i in range(len(labels)):
emotion = list(emotion_dict.keys())[list(emotion_dict.values()).index(labels[i])]
emotions.append(emotion)
return emotions
def build_image(labels, num_pics=20):
emotions = get_emotions_from_labels(labels)
print("The emotions found in the song are: ", emotions)
with open("eqns.txt", 'w') as eqnsFile:
for i in range(num_pics):
red_exp = build_expression()
green_exp = build_expression()
blue_exp = build_expression()
eqnsFile.write("img" + str(i) + ":\n")
eqnsFile.write("red = " + str(red_exp) + "\n")
eqnsFile.write("green = " + str(green_exp) + "\n")
eqnsFile.write("blue = " + str(blue_exp) + "\n\n")
image = plot_color(labels, red_exp, green_exp, blue_exp)
image.save("Images/img" + str(i) + ".png", "PNG")