Mercurial > hg > mood-conductor
changeset 12:9d9169751aba
new server
author | gyorgyf |
---|---|
date | Fri, 22 Jun 2012 11:48:28 +0100 |
parents | 2c3fe4b24640 |
children | 7c00be58d9cf |
files | mcserver/.DS_Store mcserver/mcserver.py mcserver/static/app.js~ visualclient/visclient.py |
diffstat | 4 files changed, 380 insertions(+), 117 deletions(-) [+] |
line wrap: on
line diff
--- a/mcserver/mcserver.py Fri Jun 22 11:24:19 2012 +0100 +++ b/mcserver/mcserver.py Fri Jun 22 11:48:28 2012 +0100 @@ -7,7 +7,7 @@ Copyright (c) 2012 . All rights reserved. """ -import os,sys,optparse,signal +import os,sys,optparse,signal,time,math import cherrypy as cp from cherrypy.lib import static @@ -25,11 +25,37 @@ sys.exit(-1) +class Input(object): + + def __init__(self,x,y): + self.time = time.time() + self.x=float(x) + self.y=float(y) + self.count = 1 + + def __cmp__(self,other): + d = math.sqrt( math.pow((self.x-other.x),2) + math.pow((self.y-other.y),2) ) + if d < 0.05 : + return 0 + else : + return -1 + + def dead(self): + return time.time()-self.time > 1.5 + + def __repr__(self): + return (self.x,self.y,self.count).__repr__() + + def inc(self): + self.time = time.time() + self.count += 1 + print self.count + + class MoodConductor: def __init__(self): - self.x = 0.0 - self.y = 0.0 + self.inputs = [] def index(self): return "" @@ -37,12 +63,19 @@ @cp.expose def mood(self,x,y): print "Received coordinates", x,y, "\n" - self.x, self.y = x,y - return "" + i = Input(x,y) + if i in self.inputs : + self.inputs[self.inputs.index(i)].inc() + else : + self.inputs.insert(0,i) + self.inputs = self.inputs[:1] + return str() @cp.expose def result(self): - return "(%s,%s)" %(self.x,self.y) + for i in self.inputs : + if i.dead() : self.inputs.remove(i) + return self.inputs.__repr__()
--- a/mcserver/static/app.js~ Fri Jun 22 11:24:19 2012 +0100 +++ b/mcserver/static/app.js~ Fri Jun 22 11:48:28 2012 +0100 @@ -4,12 +4,14 @@ this.marker = document.getElementById('marker'); this.label = document.getElementById('label'); - this.canvas.addEventListener('mousedown', this.onMouseDown.bind(this)); - this.canvas.addEventListener('mousemove', this.onMouseMove.bind(this)); - this.canvas.addEventListener('mouseup', this.onMouseUp.bind(this)); + this.canvas.addEventListener('click', this.onMouseUp.bind(this)); - this.request("/moods.csv", this.loadMoods.bind(this)); + this.request("/moodconductor/moods.csv", this.loadMoods.bind(this)); +<<<<<<< local this.background(); +======= + this.draw(); +>>>>>>> other }, tl: { r: 200, g: 0, b: 0 }, @@ -25,7 +27,7 @@ }; }, - background: function() { + draw: function() { var step = 20; var ctx = this.canvas.getContext("2d"); ctx.clearRect(0, 0, 320, 320); @@ -39,6 +41,8 @@ ctx.fillRect(x, y, step, step); } } + + ctx.beginPath(); ctx.strokeStyle = "rgb(0,0,0)"; ctx.moveTo(0, 160); ctx.lineTo(320, 160); @@ -72,6 +76,13 @@ ctx.font = "16px Arial"; ctx.fillText("Arousal", 0, 0); ctx.restore(); + + if (this.marker) { + ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; + ctx.beginPath(); + ctx.arc(this.marker.x, this.marker.y, 20, 0, Math.PI*2, true); + ctx.fill(); + } }, loadMoods: function(text) { @@ -102,20 +113,31 @@ request.send(null); }, - onMouseDown: function(event) { - this.mouseDown = true; - this.setMarker(event.pageX, event.pageY); + onMouseUp: function(event) { + event.preventDefault(); + this.setMarker(event); + this.sendPosition(event); + this.draw(); }, - onMouseMove: function(event) { - if (this.mouseDown) { - this.setMarker(event.pageX, event.pageY); - } + sendPosition: function(event) { + var x = event.pageX / 320; + var y = 1 - event.pageY / 320; + + this.request("/moodconductor/mood?x=" + x + "&y=" + y); }, - onMouseUp: function(event) { - this.mouseDown = false; - this.setMarker(event.pageX, event.pageY); + setMarker: function(event) { + this.marker = { + x: event.pageX, + y: event.pageY + }; + +<<<<<<< local + sendPosition: function(pageX, pageY) { + var x = pageX / 320; + var y = 1 - pageY / 320; + this.request("/moodconductor/mood?x=" + x + "&y=" + y); }, setMarker: function(pageX, pageY) { @@ -124,22 +146,15 @@ var x = pageX / 320; var y = 1 - pageY / 320; +======= + var x = event.pageX / 320; + var y = 1 - event.pageY / 320; +>>>>>>> other var mood = this.findMood(x, y); - if (mood != this.mood) { - this.label.innerHTML = mood.label; - this.mood = mood; - this.request("/mood?x=" + x + "&y=" + y); - } - }, - - drawMarker: function(x, y) { - var ctx = this.canvas.getContext("2d"); - ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; - ctx.beginPath(); - ctx.arc(x, y, 20, 0, Math.PI*2, true); - ctx.fill(); + this.label.innerHTML = mood.label; + this.mood = mood; }, findMood: function(x, y) { @@ -160,4 +175,4 @@ return this.moods[index]; } -}; +}; \ No newline at end of file
--- a/visualclient/visclient.py Fri Jun 22 11:24:19 2012 +0100 +++ b/visualclient/visclient.py Fri Jun 22 11:48:28 2012 +0100 @@ -7,7 +7,7 @@ Copyright (c) 2012 . All rights reserved. """ -import sys,os +import sys,os,math,time,copy import pygame as pg from pygame.locals import * import httplib as ht @@ -25,92 +25,307 @@ # # create_tag_image(tags, 'cloud_large.png', size=(900, 600), fontname='Lobster') +scol = (0,255,0,255) +ecol = (0,0,0,255) + +# X,Y=1140,900 +X,Y = 600,400 + +class Blob(object): + + def __init__(self,bg,x,y,color=(255,255,255),mood=None): + self.x = x + self.y = y + self.xs = x * X + self.ys = Y - (y * Y) + self.bg = bg + self.size = 20 + self.time = time.time() + self.alpha = 255 + self.c = color + self.visible = True + if mood and mood.color : + self.c = mood.color + + def __cmp__(self,other): + d = math.sqrt( math.pow((self.x-other.x),2) + math.pow((self.y-other.y),2) ) + if d < 0.03 : + return 0 + else : + return -1 + + def draw(self): + 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)) + self.alpha = 255 - int(self.age()*15) + if self.alpha < 5 : + self.alpha = 1 + self.visible = False + + def age(self): + return time.time() - self.time + + def increment(self,count): + self.size = int(self.size * int(count/1.5)) + + def get_distance(self,x,y): + return math.sqrt( math.pow((self.x-x),2) + math.pow((self.y-y),2) ) + + + + +class Mood(): + def __init__(self,word,x,y): + self.word = word + self.x = float(x) + self.y = float(y) + self.color = [] + + def get_distance(self,x,y): + return math.sqrt( math.pow((self.x-x),2) + math.pow((self.y-y),2) ) + + + +class VisualClient(object): + + def __init__(self): + # self.conn = ht.HTTPConnection("192.168.2.184:8030") + self.conn = ht.HTTPConnection("138.37.95.215") + + pg.init() + + # fontObj = pg.font.Font("freesansbold.ttf",18) + white = ( 255, 255, 255) + black = ( 0,0,0) + + self.fpsClock = pg.time.Clock() + self.screen = pg.display.set_mode((X, Y)) + pg.display.set_caption('Mood Conductor') + self.bg = pg.Surface(self.screen.get_size()) + self.bg = self.bg.convert() + self.bg.fill((0,0,0)) + + + self.scol = (0,255,0,255) + self.ecol = (0,0,0,255) + coordstxt = "test" + + self.blobs = [] + self.moods = [] + self.read_mood_data() + # pg.quit() + # sys.exit(-1) + + pass + + def read_mood_data(self): + + with open('moods.csv') as mf: + data = mf.readlines()[1:] + for line in data : + l = line.split(',') + mood = Mood(l[1],l[3],l[5]) + self.moods.append(mood) + with open('feelings.txt') as ff: + data = ff.readlines() + data = map(lambda x: x.split('\t'),data) + for mood in self.moods : + for colors in data : + if mood.word == colors[0] : + mood.color = colors[2] + pass + for mood in self.moods: + if mood.color : + mood.color = map(lambda x: '0x'+str(x).strip(),[mood.color[0:2],mood.color[2:4],mood.color[4:]]) + mood.color = tuple(map(lambda x: int(eval(x)),mood.color)) + # print mood.color + + + def update(self): + + # delete invisibles + for blob in self.blobs : + if not blob.visible : + self.blobs.remove(blob) + + # get new coordinates from the server + self.conn.putrequest("GET","/moodconductor/result", skip_host=True) + self.conn.putheader("Host", "www.isophonics.net") + self.conn.endheaders() + res = self.conn.getresponse() + data = res.read() + print data + data = eval(data) + if not data : + self.conn.close() + return False + for d in data : + print d + coordstxt = "x:%s y:%s c:%s" %d + x = d[0] + y = d[1] + c = d[2] + + cmood = None + d = cd = sys.float_info.max + for mood in self.moods : + d = mood.get_distance(x,y) + if d < cd : + cd = d + cmood = mood + + new = Blob(self.bg,x,y,mood=cmood) + if not new in self.blobs : + self.blobs.insert(0,new) + elif c > 2: + self.blobs[self.blobs.index(new)].increment(c) + + self.conn.close() + self.blobs = self.blobs[:5] + return True + + + def draw(self): + # bg.blit(gradients.radial(19, ecol, ecol), (rect_xp,rect_yp)) + # rect_xp,rect_yp = rect_x,rect_y + self.bg.fill((0,0,0)) + # self.bg.blit(gradients.radial(19, self.scol, self.ecol), (rect_x,rect_y)) + l = copy.copy(self.blobs) + l.reverse() + for blob in l : + blob.draw() + + + def run(self): + # conn = ht.HTTPConnection("192.168.2.184:8030") + self.conn = ht.HTTPConnection("138.37.95.215") + + rect_x,rect_y = 0,0 + counter = 0 + + # main loop + while True : + # pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0) + # screen.blit(gradients.radial(99, scol, ecol), (401, 1)) + + for event in pg.event.get() : + if event.type == QUIT: + conn.close() + pg.quit() + sys.exit() + elif event.type == KEYDOWN : + if event.key == K_ESCAPE : + pg.event.post(pg.event.Event(QUIT)) + + + # Draw + self.draw() + + + # update from the server + counter += 1 + if counter % 12 : + counter = 0 + self.update() + + # try : + # rect_x,rect_y = self.update() + # except : + # pass + + # update display + self.screen.blit(self.bg, (0, 0)) + pg.display.flip() + self.fpsClock.tick(50) + pass def main(): - conn = ht.HTTPConnection("192.168.2.184:8030") + v = VisualClient() + v.run() - pg.init() - fontObj = pg.font.Font("freesansbold.ttf",18) - - white = ( 255, 255, 255) - black = ( 0,0,0) - fpsClock = pg.time.Clock() - screen = pg.display.set_mode((1024, 768)) - rect_x,rect_y=50,50 - rect_xp,rect_yp=50,50 - rect_change_x,rect_change_y=5,5 - counter = 0 - scol = (0,255,0,255) - # ecol = (100,0,50,255) - ecol = (0,0,0,255) - coordstxt = "test" - - while True : - pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0) - # screen.blit(gradients.radial(99, scol, ecol), (401, 1)) - - for event in pg.event.get() : - if event.type == QUIT: - conn.close() - pg.quit() - sys.exit() - elif event.type == KEYDOWN : - if event.key == K_ESCAPE : - pg.event.post(pg.event.Event(QUIT)) - - # put text - txtObj = fontObj.render(coordstxt,True,pg.Color(254,254,254)) - rectObj = txtObj.get_rect() - rectObj.topleft = (10,20) - # rectObj.fill(pg.Color(254,254,254)) - screen.blit(txtObj,rectObj) - - - # Draw the rectangle - # pg.draw.rect(screen,black,[rect_xp,rect_yp,50,50]) - screen.blit(gradients.radial(99, ecol, ecol), (rect_xp,rect_yp)) - - rect_xp,rect_yp = rect_x,rect_y - # pg.draw.rect(screen,white,[rect_x,rect_y,50,50]) - screen.blit(gradients.radial(99, scol, ecol), (rect_x,rect_y)) - - - # Move the rectangle starting point - # rect_x += rect_change_x - # rect_y += rect_change_y - counter += 1 - if counter % 12 : - counter = 0 - try : - conn.request("GET","/moodconductor/result") - res = conn.getresponse() - data = eval(res.read()) - coordstxt = "x:%s y:%s" %data - rect_x = data[0] * 300 - rect_y = data[1] * 1000 - conn.close() - except : - pass - - - - # Bounce the ball if needed - if rect_y > 450 or rect_y < 0: - rect_change_y = rect_change_y * -1 - if rect_x > 650 or rect_x < 0: - rect_change_x = rect_change_x * -1 - - # pg.display.update() - pg.display.flip() - fpsClock.tick(50) - - - # if raw_input("quit?") in ['y'] : - # pg.quit() - - pass + # conn = ht.HTTPConnection("192.168.2.184:8030") + # + # pg.init() + # fontObj = pg.font.Font("freesansbold.ttf",18) + # + # white = ( 255, 255, 255) + # black = ( 0,0,0) + # fpsClock = pg.time.Clock() + # screen = pg.display.set_mode((1024, 768)) + # rect_x,rect_y=50,50 + # rect_xp,rect_yp=50,50 + # rect_change_x,rect_change_y=5,5 + # counter = 0 + # scol = (0,255,0,255) + # # ecol = (100,0,50,255) + # ecol = (0,0,0,255) + # coordstxt = "test" + # + # while True : + # pg.draw.circle(screen, pg.Color(255,0,0), (300,50),20,0) + # # screen.blit(gradients.radial(99, scol, ecol), (401, 1)) + # + # for event in pg.event.get() : + # if event.type == QUIT: + # conn.close() + # pg.quit() + # sys.exit() + # elif event.type == KEYDOWN : + # if event.key == K_ESCAPE : + # pg.event.post(pg.event.Event(QUIT)) + # + # # put text + # # txtObj = fontObj.render(coordstxt,True,pg.Color(254,254,254)) + # # rectObj = txtObj.get_rect() + # # rectObj.topleft = (10,20) + # # # rectObj.fill(pg.Color(254,254,254)) + # # screen.blit(txtObj,rectObj) + # + # + # # Draw the rectangle + # # pg.draw.rect(screen,black,[rect_xp,rect_yp,50,50]) + # screen.blit(gradients.radial(19, ecol, ecol), (rect_xp,rect_yp)) + # + # rect_xp,rect_yp = rect_x,rect_y + # # pg.draw.rect(screen,white,[rect_x,rect_y,50,50]) + # screen.blit(gradients.radial(19, scol, ecol), (rect_x,rect_y)) + # + # + # # Move the rectangle starting point + # # rect_x += rect_change_x + # # rect_y += rect_change_y + # counter += 1 + # if counter % 12 : + # counter = 0 + # try : + # conn.request("GET","/moodconductor/result") + # res = conn.getresponse() + # data = eval(res.read()) + # coordstxt = "x:%s y:%s" %data + # rect_x = data[0] * 300 + # rect_y = data[1] * 1000 + # conn.close() + # except : + # pass + # + # + # + # # Bounce the ball if needed + # if rect_y > 450 or rect_y < 0: + # rect_change_y = rect_change_y * -1 + # if rect_x > 650 or rect_x < 0: + # rect_change_x = rect_change_x * -1 + # + # # pg.display.update() + # pg.display.flip() + # fpsClock.tick(50) + # + # + # # if raw_input("quit?") in ['y'] : + # # pg.quit() + # + # pass if __name__ == '__main__':