forked from benosteen/Kinect-tracking-code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
osc_hand_viz.py
160 lines (127 loc) · 4.14 KB
/
osc_hand_viz.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
157
158
159
160
#!/usr/bin/env python
from read_data import OSCServer
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *
import liblo
endpoints = [('localhost', 7111)]
server = OSCServer(7110)
rquad = 0.0
rq_diff = 0.15
def resize((width, height)):
if height==0:
height=1
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1.0*width/height, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def init():
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
def emphasis_point(x,y,z, size=0.02, colour=(0.5,0.5,1.0)):
glColor3f(*colour)
glBegin(GL_QUADS)
glVertex3f(x-size, y+size, z)
glVertex3f(x+size, y+size, z)
glVertex3f(x+size, y-size, z)
glVertex3f(x-size, y-size, z)
glEnd()
def draw(server):
global rquad, rq_diff
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(-1.0,-1.0,-3.0)
glRotatef(rquad,0.0,1.0,0.0)
glBegin(GL_LINE_LOOP)
glColor3f(1.0,1.0,1.0)
glVertex3f(0.0,0.0,0.0)
glVertex3f(2.0,0.0,0.0)
glVertex3f(2.0,2.0,0.0)
glVertex3f(0.0,2.0,0.0)
glVertex3f(0.0,0.0,0.0)
glEnd()
glLoadIdentity()
glTranslatef(-1.0,-1.0,-3.0)
glRotatef(rquad,0.0,1.0,0.0)
glBegin(GL_LINE_LOOP)
glColor3f(1.0,1.0,1.0)
glVertex3f(0.0,0.0,-3.0)
glVertex3f(2.0,0.0,-3.0)
glVertex3f(2.0,2.0,-3.0)
glVertex3f(0.0,2.0,-3.0)
glVertex3f(0.0,0.0,-3.0)
glEnd()
glLoadIdentity()
glTranslatef(-1.0,-1.0,-3.0)
glRotatef(rquad,0.0,1.0,0.0)
glBegin(GL_LINES)
glColor3f(1.0,1.0,1.0)
glVertex3f(0.0,0.0,-3.0) # Back bottom left to top left
glVertex3f(0.0,0.0,0.0)
glVertex3f(2.0,0.0,-3.0)
glVertex3f(2.0,0.0,0.0)
glVertex3f(2.0,2.0,-3.0)
glVertex3f(2.0,2.0,0.0)
glVertex3f(0.0,2.0,-3.0)
glVertex3f(0.0,2.0,0.0)
glEnd()
glLoadIdentity()
glTranslatef(0.0,0.0,-3.0)
glTranslatef(-0.5,-0.5,0.0)
for player in server.lh.keys():
glBegin(GL_LINES)
glColor3f(1.0,0.0,1.0)
glVertex3f(*server.rh[player])
glVertex3f(*server.lh[player])
glColor3f(0.8,0.8,0.8)
glVertex3f(*server.lh[player])
glVertex3f(*server.ls[player])
glVertex3f(*server.rh[player])
glVertex3f(*server.rs[player])
glEnd()
x,y,z = server.rh[player]
emphasis_point(x, y, z, colour=(1.0,0.3,0.4))
x,y,z = server.lh[player]
emphasis_point(x, y, z)
# And now, shoulder joints
x,y,z = server.ls[player]
emphasis_point(x, y, z, colour=(0.3, 0.3, 0.7))
x,y,z = server.rs[player]
emphasis_point(x, y, z, colour=(0.7, 0.2, 0.2))
def main():
video_flags = OPENGL|DOUBLEBUF
#target = liblo.Address(7111)
pygame.init()
pygame.display.set_caption("Kinect handtracker demo (#pmrhack)")
# setup targets
targets = []
for ep in endpoints:
targets.append(liblo.Address(ep[0], ep[1]))
pygame.display.set_mode((640,480), video_flags)
resize((640,480))
init()
frames = 0
ticks = pygame.time.get_ticks()
while 1:
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
draw(server)
pygame.display.flip()
server.recv(100)
for player in (x for x in server.lh.keys() if server.lh[x][0] > -10.0):
# Only send real data
for target in targets:
liblo.send(target, "/hands", player, *(server.rh[player] + server.lh[player]))
liblo.send(target, "/shoulders", player, *(server.rs[player] + server.ls[player]))
liblo.send(target, "/combined", player, *(server.rh[player] + server.lh[player]+server.rs[player] + server.ls[player]))
frames = frames+1
print "fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-ticks))
if __name__ == '__main__': main()