-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyGameDemo.py
More file actions
222 lines (181 loc) · 7.15 KB
/
PyGameDemo.py
File metadata and controls
222 lines (181 loc) · 7.15 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
# PyKinect
# Copyright(c) Microsoft Corporation
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the License); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
# IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
# MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
import thread
import itertools
import ctypes
import pykinect
from pykinect import nui
from pykinect.nui import JointId
import pygame
from pygame.color import THECOLORS
from pygame.locals import *
KINECTEVENT = pygame.USEREVENT
DEPTH_WINSIZE = 320, 240
VIDEO_WINSIZE = 640, 480
pygame.init()
SKELETON_COLORS = [THECOLORS["red"],
THECOLORS["blue"],
THECOLORS["green"],
THECOLORS["orange"],
THECOLORS["purple"],
THECOLORS["yellow"],
THECOLORS["violet"]]
LEFT_ARM = (JointId.ShoulderCenter,
JointId.ShoulderLeft,
JointId.ElbowLeft,
JointId.WristLeft,
JointId.HandLeft)
RIGHT_ARM = (JointId.ShoulderCenter,
JointId.ShoulderRight,
JointId.ElbowRight,
JointId.WristRight,
JointId.HandRight)
LEFT_LEG = (JointId.HipCenter,
JointId.HipLeft,
JointId.KneeLeft,
JointId.AnkleLeft,
JointId.FootLeft)
RIGHT_LEG = (JointId.HipCenter,
JointId.HipRight,
JointId.KneeRight,
JointId.AnkleRight,
JointId.FootRight)
SPINE = (JointId.HipCenter,
JointId.Spine,
JointId.ShoulderCenter,
JointId.Head)
skeleton_to_depth_image = nui.SkeletonEngine.skeleton_to_depth_image
def draw_skeleton_data(pSkelton, index, positions, width=4):
start = pSkelton.SkeletonPositions[positions[0]]
for position in itertools.islice(positions, 1, None):
next = pSkelton.SkeletonPositions[position.value]
curstart = skeleton_to_depth_image(start, dispInfo.current_w, dispInfo.current_h)
curend = skeleton_to_depth_image(next, dispInfo.current_w, dispInfo.current_h)
pygame.draw.line(screen, SKELETON_COLORS[index], curstart, curend, width)
start = next
# recipe to get address of surface: http://archives.seul.org/pygame/users/Apr-2008/msg00218.html
if hasattr(ctypes.pythonapi, 'Py_InitModule4'):
Py_ssize_t = ctypes.c_int
elif hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
Py_ssize_t = ctypes.c_int64
else:
raise TypeError("Cannot determine type of Py_ssize_t")
_PyObject_AsWriteBuffer = ctypes.pythonapi.PyObject_AsWriteBuffer
_PyObject_AsWriteBuffer.restype = ctypes.c_int
_PyObject_AsWriteBuffer.argtypes = [ctypes.py_object,
ctypes.POINTER(ctypes.c_void_p),
ctypes.POINTER(Py_ssize_t)]
def surface_to_array(surface):
buffer_interface = surface.get_buffer()
address = ctypes.c_void_p()
size = Py_ssize_t()
_PyObject_AsWriteBuffer(buffer_interface,
ctypes.byref(address), ctypes.byref(size))
bytes = (ctypes.c_byte * size.value).from_address(address.value)
bytes.object = buffer_interface
return bytes
def draw_skeletons(skeletons):
for index, data in enumerate(skeletons):
# draw the Head
HeadPos = skeleton_to_depth_image(data.SkeletonPositions[JointId.Head], dispInfo.current_w, dispInfo.current_h)
draw_skeleton_data(data, index, SPINE, 10)
pygame.draw.circle(screen, SKELETON_COLORS[index], (int(HeadPos[0]), int(HeadPos[1])), 20, 0)
# drawing the limbs
draw_skeleton_data(data, index, LEFT_ARM)
draw_skeleton_data(data, index, RIGHT_ARM)
draw_skeleton_data(data, index, LEFT_LEG)
draw_skeleton_data(data, index, RIGHT_LEG)
def depth_frame_ready(frame):
if video_display:
return
with screen_lock:
address = surface_to_array(screen)
frame.image.copy_bits(address)
del address
if skeletons is not None and draw_skeleton:
draw_skeletons(skeletons)
pygame.display.update()
def video_frame_ready(frame):
if not video_display:
return
with screen_lock:
address = surface_to_array(screen)
frame.image.copy_bits(address)
del address
if skeletons is not None and draw_skeleton:
draw_skeletons(skeletons)
pygame.display.update()
if __name__ == '__main__':
full_screen = False
draw_skeleton = True
video_display = False
screen_lock = thread.allocate()
screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 16)
pygame.display.set_caption('Python Kinect Demo')
skeletons = None
screen.fill(THECOLORS["black"])
kinect = nui.Runtime()
kinect.skeleton_engine.enabled = True
def post_frame(frame):
try:
pygame.event.post(pygame.event.Event(KINECTEVENT, skeletons=frame.SkeletonData))
except:
# event queue full
pass
kinect.skeleton_frame_ready += post_frame
kinect.depth_frame_ready += depth_frame_ready
kinect.video_frame_ready += video_frame_ready
kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)
kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution320x240, nui.ImageType.Depth)
print('Controls: ')
print(' d - Switch to depth view')
print(' v - Switch to video view')
print(' s - Toggle displaing of the skeleton')
print(' u - Increase elevation angle')
print(' j - Decrease elevation angle')
# main game loop
done = False
while not done:
e = pygame.event.wait()
dispInfo = pygame.display.Info()
if e.type == pygame.QUIT:
done = True
break
elif e.type == KINECTEVENT:
skeletons = e.skeletons
if draw_skeleton:
draw_skeletons(skeletons)
pygame.display.update()
elif e.type == KEYDOWN:
if e.key == K_ESCAPE:
done = True
break
elif e.key == K_d:
with screen_lock:
screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 16)
video_display = False
elif e.key == K_v:
with screen_lock:
screen = pygame.display.set_mode(VIDEO_WINSIZE, 0, 32)
video_display = True
elif e.key == K_s:
draw_skeleton = not draw_skeleton
elif e.key == K_u:
kinect.camera.elevation_angle = kinect.camera.elevation_angle + 2
elif e.key == K_j:
kinect.camera.elevation_angle = kinect.camera.elevation_angle - 2
elif e.key == K_x:
kinect.camera.elevation_angle = 2