Mercurial > hg > mood-conductor
comparison visualclient/visclient.py @ 12:9d9169751aba
new server
author | gyorgyf |
---|---|
date | Fri, 22 Jun 2012 11:48:28 +0100 |
parents | 1adf97ba90c8 |
children | 3bc0521eff28 |
comparison
equal
deleted
inserted
replaced
11:2c3fe4b24640 | 12:9d9169751aba |
---|---|
5 | 5 |
6 Created by George Fazekas on 2012-06-17. | 6 Created by George Fazekas on 2012-06-17. |
7 Copyright (c) 2012 . All rights reserved. | 7 Copyright (c) 2012 . All rights reserved. |
8 """ | 8 """ |
9 | 9 |
10 import sys,os | 10 import sys,os,math,time,copy |
11 import pygame as pg | 11 import pygame as pg |
12 from pygame.locals import * | 12 from pygame.locals import * |
13 import httplib as ht | 13 import httplib as ht |
14 | 14 |
15 import gradients | 15 import gradients |
23 # | 23 # |
24 # tags = make_tags(get_tag_counts(YOUR_TEXT), maxsize=120) | 24 # tags = make_tags(get_tag_counts(YOUR_TEXT), maxsize=120) |
25 # | 25 # |
26 # create_tag_image(tags, 'cloud_large.png', size=(900, 600), fontname='Lobster') | 26 # create_tag_image(tags, 'cloud_large.png', size=(900, 600), fontname='Lobster') |
27 | 27 |
28 scol = (0,255,0,255) | |
29 ecol = (0,0,0,255) | |
30 | |
31 # X,Y=1140,900 | |
32 X,Y = 600,400 | |
33 | |
34 class Blob(object): | |
35 | |
36 def __init__(self,bg,x,y,color=(255,255,255),mood=None): | |
37 self.x = x | |
38 self.y = y | |
39 self.xs = x * X | |
40 self.ys = Y - (y * Y) | |
41 self.bg = bg | |
42 self.size = 20 | |
43 self.time = time.time() | |
44 self.alpha = 255 | |
45 self.c = color | |
46 self.visible = True | |
47 if mood and mood.color : | |
48 self.c = mood.color | |
49 | |
50 def __cmp__(self,other): | |
51 d = math.sqrt( math.pow((self.x-other.x),2) + math.pow((self.y-other.y),2) ) | |
52 if d < 0.03 : | |
53 return 0 | |
54 else : | |
55 return -1 | |
56 | |
57 def draw(self): | |
58 self.bg.blit(gradients.radial(self.size, (self.c[0],self.c[1],self.c[2],self.alpha), (0,0,0,self.alpha)), (self.xs,self.ys)) | |
59 self.alpha = 255 - int(self.age()*15) | |
60 if self.alpha < 5 : | |
61 self.alpha = 1 | |
62 self.visible = False | |
63 | |
64 def age(self): | |
65 return time.time() - self.time | |
66 | |
67 def increment(self,count): | |
68 self.size = int(self.size * int(count/1.5)) | |
69 | |
70 def get_distance(self,x,y): | |
71 return math.sqrt( math.pow((self.x-x),2) + math.pow((self.y-y),2) ) | |
72 | |
73 | |
74 | |
75 | |
76 class Mood(): | |
77 def __init__(self,word,x,y): | |
78 self.word = word | |
79 self.x = float(x) | |
80 self.y = float(y) | |
81 self.color = [] | |
82 | |
83 def get_distance(self,x,y): | |
84 return math.sqrt( math.pow((self.x-x),2) + math.pow((self.y-y),2) ) | |
85 | |
86 | |
87 | |
88 class VisualClient(object): | |
89 | |
90 def __init__(self): | |
91 # self.conn = ht.HTTPConnection("192.168.2.184:8030") | |
92 self.conn = ht.HTTPConnection("138.37.95.215") | |
93 | |
94 pg.init() | |
95 | |
96 # fontObj = pg.font.Font("freesansbold.ttf",18) | |
97 white = ( 255, 255, 255) | |
98 black = ( 0,0,0) | |
99 | |
100 self.fpsClock = pg.time.Clock() | |
101 self.screen = pg.display.set_mode((X, Y)) | |
102 pg.display.set_caption('Mood Conductor') | |
103 self.bg = pg.Surface(self.screen.get_size()) | |
104 self.bg = self.bg.convert() | |
105 self.bg.fill((0,0,0)) | |
106 | |
107 | |
108 self.scol = (0,255,0,255) | |
109 self.ecol = (0,0,0,255) | |
110 coordstxt = "test" | |
111 | |
112 self.blobs = [] | |
113 self.moods = [] | |
114 self.read_mood_data() | |
115 # pg.quit() | |
116 # sys.exit(-1) | |
117 | |
118 pass | |
119 | |
120 def read_mood_data(self): | |
121 | |
122 with open('moods.csv') as mf: | |
123 data = mf.readlines()[1:] | |
124 for line in data : | |
125 l = line.split(',') | |
126 mood = Mood(l[1],l[3],l[5]) | |
127 self.moods.append(mood) | |
128 with open('feelings.txt') as ff: | |
129 data = ff.readlines() | |
130 data = map(lambda x: x.split('\t'),data) | |
131 for mood in self.moods : | |
132 for colors in data : | |
133 if mood.word == colors[0] : | |
134 mood.color = colors[2] | |
135 pass | |
136 for mood in self.moods: | |
137 if mood.color : | |
138 mood.color = map(lambda x: '0x'+str(x).strip(),[mood.color[0:2],mood.color[2:4],mood.color[4:]]) | |
139 mood.color = tuple(map(lambda x: int(eval(x)),mood.color)) | |
140 # print mood.color | |
141 | |
142 | |
143 def update(self): | |
144 | |
145 # delete invisibles | |
146 for blob in self.blobs : | |
147 if not blob.visible : | |
148 self.blobs.remove(blob) | |
149 | |
150 # get new coordinates from the server | |
151 self.conn.putrequest("GET","/moodconductor/result", skip_host=True) | |
152 self.conn.putheader("Host", "www.isophonics.net") | |
153 self.conn.endheaders() | |
154 res = self.conn.getresponse() | |
155 data = res.read() | |
156 print data | |
157 data = eval(data) | |
158 if not data : | |
159 self.conn.close() | |
160 return False | |
161 for d in data : | |
162 print d | |
163 coordstxt = "x:%s y:%s c:%s" %d | |
164 x = d[0] | |
165 y = d[1] | |
166 c = d[2] | |
167 | |
168 cmood = None | |
169 d = cd = sys.float_info.max | |
170 for mood in self.moods : | |
171 d = mood.get_distance(x,y) | |
172 if d < cd : | |
173 cd = d | |
174 cmood = mood | |
175 | |
176 new = Blob(self.bg,x,y,mood=cmood) | |
177 if not new in self.blobs : | |
178 self.blobs.insert(0,new) | |
179 elif c > 2: | |
180 self.blobs[self.blobs.index(new)].increment(c) | |
181 | |
182 self.conn.close() | |
183 self.blobs = self.blobs[:5] | |
184 return True | |
185 | |
186 | |
187 def draw(self): | |
188 # bg.blit(gradients.radial(19, ecol, ecol), (rect_xp,rect_yp)) | |
189 # rect_xp,rect_yp = rect_x,rect_y | |
190 self.bg.fill((0,0,0)) | |
191 # self.bg.blit(gradients.radial(19, self.scol, self.ecol), (rect_x,rect_y)) | |
192 l = copy.copy(self.blobs) | |
193 l.reverse() | |
194 for blob in l : | |
195 blob.draw() | |
196 | |
197 | |
198 def run(self): | |
199 # conn = ht.HTTPConnection("192.168.2.184:8030") | |
200 self.conn = ht.HTTPConnection("138.37.95.215") | |
201 | |
202 rect_x,rect_y = 0,0 | |
203 counter = 0 | |
204 | |
205 # main loop | |
206 while True : | |
207 # pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0) | |
208 # screen.blit(gradients.radial(99, scol, ecol), (401, 1)) | |
209 | |
210 for event in pg.event.get() : | |
211 if event.type == QUIT: | |
212 conn.close() | |
213 pg.quit() | |
214 sys.exit() | |
215 elif event.type == KEYDOWN : | |
216 if event.key == K_ESCAPE : | |
217 pg.event.post(pg.event.Event(QUIT)) | |
218 | |
219 | |
220 # Draw | |
221 self.draw() | |
222 | |
223 | |
224 # update from the server | |
225 counter += 1 | |
226 if counter % 12 : | |
227 counter = 0 | |
228 self.update() | |
229 | |
230 # try : | |
231 # rect_x,rect_y = self.update() | |
232 # except : | |
233 # pass | |
234 | |
235 # update display | |
236 self.screen.blit(self.bg, (0, 0)) | |
237 pg.display.flip() | |
238 self.fpsClock.tick(50) | |
239 pass | |
28 | 240 |
29 | 241 |
30 def main(): | 242 def main(): |
31 | 243 |
32 conn = ht.HTTPConnection("192.168.2.184:8030") | 244 v = VisualClient() |
33 | 245 v.run() |
34 pg.init() | 246 |
35 fontObj = pg.font.Font("freesansbold.ttf",18) | 247 # conn = ht.HTTPConnection("192.168.2.184:8030") |
36 | 248 # |
37 white = ( 255, 255, 255) | 249 # pg.init() |
38 black = ( 0,0,0) | 250 # fontObj = pg.font.Font("freesansbold.ttf",18) |
39 fpsClock = pg.time.Clock() | 251 # |
40 screen = pg.display.set_mode((1024, 768)) | 252 # white = ( 255, 255, 255) |
41 rect_x,rect_y=50,50 | 253 # black = ( 0,0,0) |
42 rect_xp,rect_yp=50,50 | 254 # fpsClock = pg.time.Clock() |
43 rect_change_x,rect_change_y=5,5 | 255 # screen = pg.display.set_mode((1024, 768)) |
44 counter = 0 | 256 # rect_x,rect_y=50,50 |
45 scol = (0,255,0,255) | 257 # rect_xp,rect_yp=50,50 |
46 # ecol = (100,0,50,255) | 258 # rect_change_x,rect_change_y=5,5 |
47 ecol = (0,0,0,255) | 259 # counter = 0 |
48 coordstxt = "test" | 260 # scol = (0,255,0,255) |
49 | 261 # # ecol = (100,0,50,255) |
50 while True : | 262 # ecol = (0,0,0,255) |
51 pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0) | 263 # coordstxt = "test" |
52 # screen.blit(gradients.radial(99, scol, ecol), (401, 1)) | 264 # |
53 | 265 # while True : |
54 for event in pg.event.get() : | 266 # pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0) |
55 if event.type == QUIT: | 267 # # screen.blit(gradients.radial(99, scol, ecol), (401, 1)) |
56 conn.close() | 268 # |
57 pg.quit() | 269 # for event in pg.event.get() : |
58 sys.exit() | 270 # if event.type == QUIT: |
59 elif event.type == KEYDOWN : | 271 # conn.close() |
60 if event.key == K_ESCAPE : | 272 # pg.quit() |
61 pg.event.post(pg.event.Event(QUIT)) | 273 # sys.exit() |
62 | 274 # elif event.type == KEYDOWN : |
63 # put text | 275 # if event.key == K_ESCAPE : |
64 txtObj = fontObj.render(coordstxt,True,pg.Color(254,254,254)) | 276 # pg.event.post(pg.event.Event(QUIT)) |
65 rectObj = txtObj.get_rect() | 277 # |
66 rectObj.topleft = (10,20) | 278 # # put text |
67 # rectObj.fill(pg.Color(254,254,254)) | 279 # # txtObj = fontObj.render(coordstxt,True,pg.Color(254,254,254)) |
68 screen.blit(txtObj,rectObj) | 280 # # rectObj = txtObj.get_rect() |
69 | 281 # # rectObj.topleft = (10,20) |
70 | 282 # # # rectObj.fill(pg.Color(254,254,254)) |
71 # Draw the rectangle | 283 # # screen.blit(txtObj,rectObj) |
72 # pg.draw.rect(screen,black,[rect_xp,rect_yp,50,50]) | 284 # |
73 screen.blit(gradients.radial(99, ecol, ecol), (rect_xp,rect_yp)) | 285 # |
74 | 286 # # Draw the rectangle |
75 rect_xp,rect_yp = rect_x,rect_y | 287 # # pg.draw.rect(screen,black,[rect_xp,rect_yp,50,50]) |
76 # pg.draw.rect(screen,white,[rect_x,rect_y,50,50]) | 288 # screen.blit(gradients.radial(19, ecol, ecol), (rect_xp,rect_yp)) |
77 screen.blit(gradients.radial(99, scol, ecol), (rect_x,rect_y)) | 289 # |
78 | 290 # rect_xp,rect_yp = rect_x,rect_y |
79 | 291 # # pg.draw.rect(screen,white,[rect_x,rect_y,50,50]) |
80 # Move the rectangle starting point | 292 # screen.blit(gradients.radial(19, scol, ecol), (rect_x,rect_y)) |
81 # rect_x += rect_change_x | 293 # |
82 # rect_y += rect_change_y | 294 # |
83 counter += 1 | 295 # # Move the rectangle starting point |
84 if counter % 12 : | 296 # # rect_x += rect_change_x |
85 counter = 0 | 297 # # rect_y += rect_change_y |
86 try : | 298 # counter += 1 |
87 conn.request("GET","/moodconductor/result") | 299 # if counter % 12 : |
88 res = conn.getresponse() | 300 # counter = 0 |
89 data = eval(res.read()) | 301 # try : |
90 coordstxt = "x:%s y:%s" %data | 302 # conn.request("GET","/moodconductor/result") |
91 rect_x = data[0] * 300 | 303 # res = conn.getresponse() |
92 rect_y = data[1] * 1000 | 304 # data = eval(res.read()) |
93 conn.close() | 305 # coordstxt = "x:%s y:%s" %data |
94 except : | 306 # rect_x = data[0] * 300 |
95 pass | 307 # rect_y = data[1] * 1000 |
96 | 308 # conn.close() |
97 | 309 # except : |
98 | 310 # pass |
99 # Bounce the ball if needed | 311 # |
100 if rect_y > 450 or rect_y < 0: | 312 # |
101 rect_change_y = rect_change_y * -1 | 313 # |
102 if rect_x > 650 or rect_x < 0: | 314 # # Bounce the ball if needed |
103 rect_change_x = rect_change_x * -1 | 315 # if rect_y > 450 or rect_y < 0: |
104 | 316 # rect_change_y = rect_change_y * -1 |
105 # pg.display.update() | 317 # if rect_x > 650 or rect_x < 0: |
106 pg.display.flip() | 318 # rect_change_x = rect_change_x * -1 |
107 fpsClock.tick(50) | 319 # |
108 | 320 # # pg.display.update() |
109 | 321 # pg.display.flip() |
110 # if raw_input("quit?") in ['y'] : | 322 # fpsClock.tick(50) |
111 # pg.quit() | 323 # |
112 | 324 # |
113 pass | 325 # # if raw_input("quit?") in ['y'] : |
326 # # pg.quit() | |
327 # | |
328 # pass | |
114 | 329 |
115 | 330 |
116 if __name__ == '__main__': | 331 if __name__ == '__main__': |
117 pass | 332 pass |
118 main() | 333 main() |