├── doc └── img1.png ├── code ├── kinect2.blend ├── geometry_test.blend ├── kinect_axis_angle.blend ├── kinect_interface.blend ├── kinect_interface_IK.blend ├── kinect_axis_angle_no_rigging.blend ├── server │ ├── server.txt │ ├── clientKinect.py │ ├── serverKinect_fake.py │ ├── serverKinect.py │ └── serverKinect_screen.py └── blender.txt └── README.md /doc/img1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngmsoftware/blender-kinect/HEAD/doc/img1.png -------------------------------------------------------------------------------- /code/kinect2.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngmsoftware/blender-kinect/HEAD/code/kinect2.blend -------------------------------------------------------------------------------- /code/geometry_test.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngmsoftware/blender-kinect/HEAD/code/geometry_test.blend -------------------------------------------------------------------------------- /code/kinect_axis_angle.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngmsoftware/blender-kinect/HEAD/code/kinect_axis_angle.blend -------------------------------------------------------------------------------- /code/kinect_interface.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngmsoftware/blender-kinect/HEAD/code/kinect_interface.blend -------------------------------------------------------------------------------- /code/kinect_interface_IK.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngmsoftware/blender-kinect/HEAD/code/kinect_interface_IK.blend -------------------------------------------------------------------------------- /code/kinect_axis_angle_no_rigging.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ngmsoftware/blender-kinect/HEAD/code/kinect_axis_angle_no_rigging.blend -------------------------------------------------------------------------------- /code/server/server.txt: -------------------------------------------------------------------------------- 1 | clientKinect.py : dummy client to test the server's connectivity 2 | serverKinect_screen.py : Server with image preview 3 | serverKinect.py : Server without image preview 4 | serverKinect_fake.py : Dummy server (sends random stuff organized as a Kinect skeleton) 5 | 6 | -------------------------------------------------------------------------------- /code/blender.txt: -------------------------------------------------------------------------------- 1 | kinect_axis_angle.blend : Bones controlled by kinect (just bones, no object) 2 | kinect_axis_angle_no_rigging.blend : Bones controlled by kinect with basic object rigged to it 3 | kinect_interface.blend : Bones controlled by kinect with methods to position objects using hands (open and close fingers) 4 | 5 | kinect_interface_IK.blend and kinect2.blend : attempt to use inverse kinematics to filter skeleton movement (not even started, I don't remember) 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blender Kinect 2 | 3 | Blender interface with Kinect. 4 | 5 | Youtube demo: 6 | https://www.youtube.com/watch?v=oKTQPcK8uc4 7 | 8 | ## Software dependencies 9 | 10 | - PyKinect2 11 | - pygame 12 | 13 | ## Hardware dependencies 14 | 15 | - Kinect sensor 16 | 17 | ## Screen-shoots 18 | 19 | ![Screenshoot 1](/doc/img1.png?raw=true "Blender file with custom panel") 20 | 21 | ## Instructions 22 | 23 | - Install PyKinect2 24 | - install pygame 25 | - Run the server found on (./server/serverKinect.py) 26 | - open kinect_axis_angle_no_rigging.blend and run the panel.py 27 | - click 'start' in the new panel 28 | 29 | -------------------------------------------------------------------------------- /code/server/clientKinect.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import struct, time 3 | 4 | # server 5 | HOST = "localhost" 6 | PORT = 6660 7 | 8 | 9 | def recvall(socket): 10 | str = "" 11 | maxRecvSize = 256 12 | finished = False 13 | while not finished: 14 | t = socket.recv(maxRecvSize).decode() 15 | str = "%s%s"%(str,t) 16 | 17 | finished = str[-3:] == 'EOD' 18 | 19 | return str 20 | 21 | 22 | # connect to server 23 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 24 | s.connect((HOST, PORT)) 25 | 26 | for i in range(1,10): 27 | t = recvall(s) 28 | print("received: %s"%t) 29 | 30 | bytes = s.sendall("ACK".encode()) 31 | print(bytes) 32 | 33 | s.close() 34 | -------------------------------------------------------------------------------- /code/server/serverKinect_fake.py: -------------------------------------------------------------------------------- 1 | import random 2 | import pygame 3 | import sys 4 | 5 | import socket 6 | import sys 7 | import os 8 | 9 | PORT = 6660 10 | 11 | class KinectBodyServer(object): 12 | def __init__(self): 13 | 14 | self._connection = None 15 | self._clientAddress = None 16 | self._socket = None 17 | 18 | # Create a UDS socket 19 | self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 20 | 21 | # Bind the socket to the port 22 | print("server on localhost") 23 | self._socket.bind(("",PORT)) 24 | 25 | # Listen for incoming connections 26 | self._socket.listen(1) 27 | 28 | def waitForConnection(self): 29 | # Wait for a connection 30 | print("waiting for a connection") 31 | self._connection, self._clientAddress = self._socket.accept() 32 | 33 | def sendValues(self): 34 | amp = 0.01 35 | 36 | n1 = amp*float(random.randint(0,100))/40000.0 37 | n2 = amp*float(random.randint(0,100))/40000.0 38 | n3 = amp*float(random.randint(0,100))/40000.0 39 | n4 = amp*float(random.randint(0,100))/40000.0 40 | n5 = amp*float(random.randint(0,100))/40000.0 41 | 42 | 43 | # Torso 44 | j1x = 0.0 + n1 45 | j2x = 0.0 + n1 + n2 46 | j3x = 0.0 + n1 + n2 + n3 47 | j4x = 0.0 - n1 48 | j5x = 0.0 - n1 + n2 49 | j6x = 0.15 - n1 + n2 + n3 50 | j7x = -0.15 + n1 51 | j8x = 0.1 + n1 - n2 52 | j9x = -0.1 + n1 - n2 + n3 53 | 54 | # Right Arm 55 | j10x = 0.25 + n1 56 | j11x = 0.3 - n1 - n2 57 | j12x = 0.3 - n1 - n2 + n3 58 | j13x = 0.31 + n1 59 | j14x = 0.29 + n1 + n2 60 | 61 | # Left Arm 62 | j15x = -0.25 + n1 + n2 - n3 63 | j16x = -0.3 + n1 64 | j17x = -0.3 + n1 + n2 65 | j18x = -0.31 + n1 + n2 - n3 66 | j19x = -0.29 + n1 67 | 68 | # Right Leg 69 | j20x = 0.2 + n1 + n2 70 | j21x = 0.2 + n1 + n2 - n3 71 | j22x = 0.25 - n1 72 | 73 | # Left Leg 74 | j23x = -0.2 - n1 + n2 75 | j24x = -0.2 - n1 + n2 - n3 76 | j25x = -0.25 + n1 77 | 78 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,"%(j1x,j2x,j3x,j4x,j5x,j6x,j7x,j8x,j9x,j10x,j11x,j12x,j13x,j14x,j15x,j16x,j17x,j18x,j19x,j20x,j21x,j22x,j23x,j24x,j25x)).encode()) 79 | 80 | # Torso 81 | j1y = 0.7 + n1 82 | j2y = 0.6 + n1 + n2 83 | j3y = 0.3 - n1 + n2 - n3 84 | j4y = 0.0 + n1 85 | j5y = 0.55 + n1 - n2 86 | j6y = 0.55 + n1 - n2 + n3 87 | j7y = 0.55 + n1 88 | j8y = -0.1 + n1 - n2 89 | j9y = -0.1 + n1 - n2 + n3 90 | 91 | # Right Arm 92 | j10y = 0.3 + n1 93 | j11y = 0.0 - n1 + n2 94 | j12y = -0.1 + n1 + n2 + n3 95 | j13y = -0.15 - n1 96 | j14y = -0.15 + n1 + n2 97 | 98 | # Left Arm 99 | j15y = 0.3 + n1 - n2 + n3 100 | j16y = 0.0 + n1 101 | j17y = -0.1 + n1 + n2 102 | j18y = -0.15 + n1 + n2 + n3 103 | j19y = -0.15 + n1 104 | 105 | # Right Leg 106 | j20y = -0.3 + n1 - n2 107 | j21y = -0.6 + n1 + n2 + n3 108 | j22y = -0.65 + n1 109 | 110 | # Left Leg 111 | j23y = -0.3 + n1 + n2 112 | j24y = -0.6 + n1 + n2 + n3 113 | j25y = -0.65 + n1 114 | 115 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,"%(j1y,j2y,j3y,j4y,j5y,j6y,j7y,j8y,j9y,j10y,j11y,j12y,j13y,j14y,j15y,j16y,j17y,j18y,j19y,j20y,j21y,j22y,j23y,j24y,j25y)).encode()) 116 | 117 | # Torso 118 | j1z = 0.1 + n4 + n5 119 | j2z = 0.075 + n4 + n2 + n3 120 | j3z = 0.025 + n4 121 | j4z = 0.0 + n4 + n2 122 | j5z = 0.0 + n4 + n2 + n3 123 | j6z = 0.0 + n4 124 | j7z = 0.0 + n4 + n5 125 | j8z = 0.0 + n4 + n2 + n3 126 | j9z = 0.0 + n4 127 | 128 | # Right Arm 129 | j10z = 0.0 + n4 - n2 130 | j11z = 0.0 + n4 - n2 + n3 131 | j12z = 0.0 + n4 132 | j13z = 0.0 + n4 - n2 133 | j14z = 0.0 + n4 + n2 + n3 134 | 135 | # Left Arm 136 | j15z = 0.0 + n4 137 | j16z = 0.0 + n4 + n5 138 | j17z = 0.0 + n4 - n5 + n3 139 | j18z = 0.0 + n4 140 | j19z = 0.0 + n4 + n2 141 | 142 | # Right Leg 143 | j20z = 0.0 + n4 + n5 + n3 144 | j21z = 0.0 + n4 145 | j22z = 0.0 + n4 - n5 146 | 147 | # Left Leg 148 | j23z = 0.0 + n4 + n5 + n3 149 | j24z = 0.0 + n4 150 | j25z = 0.0 + n4 + n5 151 | 152 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4fEOD"%(j1z,j2z,j3z,j4z,j5z,j6z,j7z,j8z,j9z,j10z,j11z,j12z,j13z,j14z,j15z,j16z,j17z,j18z,j19z,j20z,j21z,j22z,j23z,j24z,j25z)).encode()) 153 | 154 | 155 | def closeConnection(self): 156 | self._connection.close(); 157 | 158 | 159 | 160 | 161 | class BodyGameRuntime(object): 162 | def __init__(self): 163 | pygame.init() 164 | 165 | 166 | # Loop until the user clicks the close button. 167 | self._done = False 168 | 169 | 170 | # Torso 171 | # PyKinectV2.JointType_Head -> PyKinectV2.JointType_Neck 172 | # PyKinectV2.JointType_Neck -> PyKinectV2.JointType_SpineShoulder); 173 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_SpineMid); 174 | # PyKinectV2.JointType_SpineMid -> PyKinectV2.JointType_SpineBase); 175 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_ShoulderRight); 176 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_ShoulderLeft); 177 | # PyKinectV2.JointType_SpineBase -> PyKinectV2.JointType_HipRight); 178 | # PyKinectV2.JointType_SpineBase -> PyKinectV2.JointType_HipLeft); 179 | 180 | # Right Arm 181 | # PyKinectV2.JointType_ShoulderRight -> PyKinectV2.JointType_ElbowRight); 182 | # PyKinectV2.JointType_ElbowRight -> PyKinectV2.JointType_WristRight); 183 | # PyKinectV2.JointType_WristRight -> PyKinectV2.JointType_HandRight); 184 | # PyKinectV2.JointType_HandRight -> PyKinectV2.JointType_HandTipRight); 185 | # PyKinectV2.JointType_WristRight -> PyKinectV2.JointType_ThumbRight); 186 | 187 | # Left Arm 188 | # PyKinectV2.JointType_ShoulderLeft -> PyKinectV2.JointType_ElbowLeft); 189 | # PyKinectV2.JointType_ElbowLeft -> PyKinectV2.JointType_WristLeft); 190 | # PyKinectV2.JointType_WristLeft -> PyKinectV2.JointType_HandLeft); 191 | # PyKinectV2.JointType_HandLeft -> PyKinectV2.JointType_HandTipLeft); 192 | # PyKinectV2.JointType_WristLeft -> PyKinectV2.JointType_ThumbLeft); 193 | 194 | # Right Leg 195 | # PyKinectV2.JointType_HipRight -> PyKinectV2.JointType_KneeRight); 196 | # PyKinectV2.JointType_KneeRight -> PyKinectV2.JointType_AnkleRight); 197 | # PyKinectV2.JointType_AnkleRight -> PyKinectV2.JointType_FootRight); 198 | 199 | # Left Leg 200 | # PyKinectV2.JointType_HipLeft -> PyKinectV2.JointType_KneeLeft); 201 | # PyKinectV2.JointType_KneeLeft -> PyKinectV2.JointType_AnkleLeft); 202 | # PyKinectV2.JointType_AnkleLeft -> PyKinectV2.JointType_FootLeft); 203 | 204 | 205 | self._server = KinectBodyServer() 206 | 207 | self._server.waitForConnection() 208 | 209 | 210 | 211 | def run(self): 212 | 213 | 214 | # -------- Main Program Loop ----------- 215 | while not self._done: 216 | # --- Main event loop 217 | for event in pygame.event.get(): # User did something (including Ctrl+C) 218 | if event.type == pygame.QUIT: # If user clicked close 219 | self._done = True # Flag that we are done so we exit this loop 220 | 221 | 222 | 223 | self._server.sendValues() 224 | 225 | s = self._server._connection.recv(3) 226 | 227 | 228 | # Close our Kinect sensor, close the window and quit. 229 | try: 230 | self._server.closeConnection() 231 | finally: 232 | pass 233 | 234 | pygame.quit() 235 | 236 | 237 | 238 | __main__ = "Kinect server (fake)" 239 | game = BodyGameRuntime(); 240 | game.run(); 241 | 242 | -------------------------------------------------------------------------------- /code/server/serverKinect.py: -------------------------------------------------------------------------------- 1 | from pykinect2 import PyKinectV2 2 | from pykinect2.PyKinectV2 import * 3 | from pykinect2 import PyKinectRuntime 4 | 5 | #import ctypes 6 | #import _ctypes 7 | import pygame 8 | import sys 9 | 10 | import socket 11 | import sys 12 | import os 13 | 14 | PORT = 6660 15 | 16 | class KinectBodyServer(object): 17 | def __init__(self): 18 | 19 | self._connection = None 20 | self._clientAddress = None 21 | self._socket = None 22 | 23 | # Create a UDS socket 24 | self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 25 | 26 | # Bind the socket to the port 27 | print("server on localhost") 28 | self._socket.bind(("",PORT)) 29 | 30 | # Listen for incoming connections 31 | self._socket.listen(1) 32 | 33 | def waitForConnection(self): 34 | # Wait for a connection 35 | print("waiting for a connection") 36 | self._connection, self._clientAddress = self._socket.accept() 37 | 38 | def sendValues(self, joints): 39 | # Torso 40 | j1x = joints[PyKinectV2.JointType_Head].Position.x 41 | j2x = joints[PyKinectV2.JointType_Neck].Position.x 42 | j3x = joints[PyKinectV2.JointType_SpineMid].Position.x 43 | j4x = joints[PyKinectV2.JointType_SpineBase].Position.x 44 | j5x = joints[PyKinectV2.JointType_SpineShoulder].Position.x 45 | j6x = joints[PyKinectV2.JointType_ShoulderRight].Position.x 46 | j7x = joints[PyKinectV2.JointType_ShoulderLeft].Position.x 47 | j8x = joints[PyKinectV2.JointType_HipRight].Position.x 48 | j9x = joints[PyKinectV2.JointType_HipLeft].Position.x 49 | 50 | # Right Arm 51 | j10x = joints[PyKinectV2.JointType_ElbowRight].Position.x 52 | j11x = joints[PyKinectV2.JointType_WristRight].Position.x 53 | j12x = joints[PyKinectV2.JointType_HandRight].Position.x 54 | j13x = joints[PyKinectV2.JointType_HandTipRight].Position.x 55 | j14x = joints[PyKinectV2.JointType_ThumbRight].Position.x 56 | 57 | # Left Arm 58 | j15x = joints[PyKinectV2.JointType_ElbowLeft].Position.x 59 | j16x = joints[PyKinectV2.JointType_WristLeft].Position.x 60 | j17x = joints[PyKinectV2.JointType_HandLeft].Position.x 61 | j18x = joints[PyKinectV2.JointType_HandTipLeft].Position.x 62 | j19x = joints[PyKinectV2.JointType_ThumbLeft].Position.x 63 | 64 | # Right Leg 65 | j20x = joints[PyKinectV2.JointType_KneeRight].Position.x 66 | j21x = joints[PyKinectV2.JointType_AnkleRight].Position.x 67 | j22x = joints[PyKinectV2.JointType_FootRight].Position.x 68 | 69 | # Left Leg 70 | j23x = joints[PyKinectV2.JointType_KneeLeft].Position.x 71 | j24x = joints[PyKinectV2.JointType_AnkleLeft].Position.x 72 | j25x = joints[PyKinectV2.JointType_FootLeft].Position.x 73 | 74 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,"%(j1x,j2x,j3x,j4x,j5x,j6x,j7x,j8x,j9x,j10x,j11x,j12x,j13x,j14x,j15x,j16x,j17x,j18x,j19x,j20x,j21x,j22x,j23x,j24x,j25x)).encode()) 75 | 76 | # Torso 77 | j1y = joints[PyKinectV2.JointType_Head].Position.y 78 | j2y = joints[PyKinectV2.JointType_Neck].Position.y 79 | j3y = joints[PyKinectV2.JointType_SpineMid].Position.y 80 | j4y = joints[PyKinectV2.JointType_SpineBase].Position.y 81 | j5y = joints[PyKinectV2.JointType_SpineShoulder].Position.y 82 | j6y = joints[PyKinectV2.JointType_ShoulderRight].Position.y 83 | j7y = joints[PyKinectV2.JointType_ShoulderLeft].Position.y 84 | j8y = joints[PyKinectV2.JointType_HipRight].Position.y 85 | j9y = joints[PyKinectV2.JointType_HipLeft].Position.y 86 | 87 | # Right Arm 88 | j10y = joints[PyKinectV2.JointType_ElbowRight].Position.y 89 | j11y = joints[PyKinectV2.JointType_WristRight].Position.y 90 | j12y = joints[PyKinectV2.JointType_HandRight].Position.y 91 | j13y = joints[PyKinectV2.JointType_HandTipRight].Position.y 92 | j14y = joints[PyKinectV2.JointType_ThumbRight].Position.y 93 | 94 | # Left Arm 95 | j15y = joints[PyKinectV2.JointType_ElbowLeft].Position.y 96 | j16y = joints[PyKinectV2.JointType_WristLeft].Position.y 97 | j17y = joints[PyKinectV2.JointType_HandLeft].Position.y 98 | j18y = joints[PyKinectV2.JointType_HandTipLeft].Position.y 99 | j19y = joints[PyKinectV2.JointType_ThumbLeft].Position.y 100 | 101 | # Right Leg 102 | j20y = joints[PyKinectV2.JointType_KneeRight].Position.y 103 | j21y = joints[PyKinectV2.JointType_AnkleRight].Position.y 104 | j22y = joints[PyKinectV2.JointType_FootRight].Position.y 105 | 106 | # Left Leg 107 | j23y = joints[PyKinectV2.JointType_KneeLeft].Position.y 108 | j24y = joints[PyKinectV2.JointType_AnkleLeft].Position.y 109 | j25y = joints[PyKinectV2.JointType_FootLeft].Position.y 110 | 111 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,"%(j1y,j2y,j3y,j4y,j5y,j6y,j7y,j8y,j9y,j10y,j11y,j12y,j13y,j14y,j15y,j16y,j17y,j18y,j19y,j20y,j21y,j22y,j23y,j24y,j25y)).encode()) 112 | 113 | # Torso 114 | j1z = joints[PyKinectV2.JointType_Head].Position.z 115 | j2z = joints[PyKinectV2.JointType_Neck].Position.z 116 | j3z = joints[PyKinectV2.JointType_SpineMid].Position.z 117 | j4z = joints[PyKinectV2.JointType_SpineBase].Position.z 118 | j5z = joints[PyKinectV2.JointType_SpineShoulder].Position.z 119 | j6z = joints[PyKinectV2.JointType_ShoulderRight].Position.z 120 | j7z = joints[PyKinectV2.JointType_ShoulderLeft].Position.z 121 | j8z = joints[PyKinectV2.JointType_HipRight].Position.z 122 | j9z = joints[PyKinectV2.JointType_HipLeft].Position.z 123 | 124 | # Right Arm 125 | j10z = joints[PyKinectV2.JointType_ElbowRight].Position.z 126 | j11z = joints[PyKinectV2.JointType_WristRight].Position.z 127 | j12z = joints[PyKinectV2.JointType_HandRight].Position.z 128 | j13z = joints[PyKinectV2.JointType_HandTipRight].Position.z 129 | j14z = joints[PyKinectV2.JointType_ThumbRight].Position.z 130 | 131 | # Left Arm 132 | j15z = joints[PyKinectV2.JointType_ElbowLeft].Position.z 133 | j16z = joints[PyKinectV2.JointType_WristLeft].Position.z 134 | j17z = joints[PyKinectV2.JointType_HandLeft].Position.z 135 | j18z = joints[PyKinectV2.JointType_HandTipLeft].Position.z 136 | j19z = joints[PyKinectV2.JointType_ThumbLeft].Position.z 137 | 138 | # Right Leg 139 | j20z = joints[PyKinectV2.JointType_KneeRight].Position.z 140 | j21z = joints[PyKinectV2.JointType_AnkleRight].Position.z 141 | j22z = joints[PyKinectV2.JointType_FootRight].Position.z 142 | 143 | # Left Leg 144 | j23z = joints[PyKinectV2.JointType_KneeLeft].Position.z 145 | j24z = joints[PyKinectV2.JointType_AnkleLeft].Position.z 146 | j25z = joints[PyKinectV2.JointType_FootLeft].Position.z 147 | 148 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4fEOD"%(j1z,j2z,j3z,j4z,j5z,j6z,j7z,j8z,j9z,j10z,j11z,j12z,j13z,j14z,j15z,j16z,j17z,j18z,j19z,j20z,j21z,j22z,j23z,j24z,j25z)).encode()) 149 | 150 | 151 | def closeConnection(self): 152 | self._connection.close(); 153 | 154 | 155 | 156 | 157 | class BodyGameRuntime(object): 158 | def __init__(self): 159 | pygame.init() 160 | 161 | # Kinect runtime object, we want only color and body frames 162 | self._kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Body) 163 | 164 | print(self._kinect) 165 | 166 | # here we will store skeleton data 167 | self._bodies = None 168 | 169 | # Loop until the user clicks the close button. 170 | self._done = False 171 | 172 | 173 | # Torso 174 | # PyKinectV2.JointType_Head -> PyKinectV2.JointType_Neck 175 | # PyKinectV2.JointType_Neck -> PyKinectV2.JointType_SpineShoulder); 176 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_SpineMid); 177 | # PyKinectV2.JointType_SpineMid -> PyKinectV2.JointType_SpineBase); 178 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_ShoulderRight); 179 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_ShoulderLeft); 180 | # PyKinectV2.JointType_SpineBase -> PyKinectV2.JointType_HipRight); 181 | # PyKinectV2.JointType_SpineBase -> PyKinectV2.JointType_HipLeft); 182 | 183 | # Right Arm 184 | # PyKinectV2.JointType_ShoulderRight -> PyKinectV2.JointType_ElbowRight); 185 | # PyKinectV2.JointType_ElbowRight -> PyKinectV2.JointType_WristRight); 186 | # PyKinectV2.JointType_WristRight -> PyKinectV2.JointType_HandRight); 187 | # PyKinectV2.JointType_HandRight -> PyKinectV2.JointType_HandTipRight); 188 | # PyKinectV2.JointType_WristRight -> PyKinectV2.JointType_ThumbRight); 189 | 190 | # Left Arm 191 | # PyKinectV2.JointType_ShoulderLeft -> PyKinectV2.JointType_ElbowLeft); 192 | # PyKinectV2.JointType_ElbowLeft -> PyKinectV2.JointType_WristLeft); 193 | # PyKinectV2.JointType_WristLeft -> PyKinectV2.JointType_HandLeft); 194 | # PyKinectV2.JointType_HandLeft -> PyKinectV2.JointType_HandTipLeft); 195 | # PyKinectV2.JointType_WristLeft -> PyKinectV2.JointType_ThumbLeft); 196 | 197 | # Right Leg 198 | # PyKinectV2.JointType_HipRight -> PyKinectV2.JointType_KneeRight); 199 | # PyKinectV2.JointType_KneeRight -> PyKinectV2.JointType_AnkleRight); 200 | # PyKinectV2.JointType_AnkleRight -> PyKinectV2.JointType_FootRight); 201 | 202 | # Left Leg 203 | # PyKinectV2.JointType_HipLeft -> PyKinectV2.JointType_KneeLeft); 204 | # PyKinectV2.JointType_KneeLeft -> PyKinectV2.JointType_AnkleLeft); 205 | # PyKinectV2.JointType_AnkleLeft -> PyKinectV2.JointType_FootLeft); 206 | 207 | 208 | self._server = KinectBodyServer() 209 | 210 | self._server.waitForConnection() 211 | 212 | 213 | 214 | 215 | def run(self): 216 | 217 | 218 | # -------- Main Program Loop ----------- 219 | while not self._done: 220 | # --- Main event loop 221 | for event in pygame.event.get(): # User did something (including Ctrl+C) 222 | if event.type == pygame.QUIT: # If user clicked close 223 | self._done = True # Flag that we are done so we exit this loop 224 | 225 | 226 | 227 | # --- Cool! We have a body frame, so can get skeletons 228 | if self._kinect.has_new_body_frame(): 229 | self._bodies = self._kinect.get_last_body_frame() 230 | 231 | 232 | # --- draw skeletons to _frame_surface 233 | body = None 234 | if self._bodies is not None: 235 | for i in range(0,len(self._bodies.bodies)): 236 | if self._bodies.bodies[i].is_tracked: 237 | body = self._bodies.bodies[i] 238 | 239 | 240 | if body is not None: 241 | joints = body.joints 242 | print(joints[PyKinectV2.JointType_Head].Position.x) 243 | self._server.sendValues(joints) 244 | # convert joint coordinates to color space 245 | #joint_points = self._kinect.body_joints_to_color_space(joints) 246 | #self.draw_body(joints, joint_points) 247 | 248 | s = self._server._connection.recv(3) 249 | else: 250 | print("no body") 251 | self._server._connection.sendall("EOD".encode()) 252 | 253 | s = self._server._connection.recv(3) 254 | 255 | 256 | 257 | # Close our Kinect sensor, close the window and quit. 258 | try: 259 | self._server.closeConnection() 260 | finally: 261 | pass 262 | 263 | pygame.quit() 264 | 265 | 266 | 267 | __main__ = "Kinect server" 268 | game = BodyGameRuntime(); 269 | game.run(); 270 | 271 | -------------------------------------------------------------------------------- /code/server/serverKinect_screen.py: -------------------------------------------------------------------------------- 1 | from pykinect2 import PyKinectV2 2 | from pykinect2.PyKinectV2 import * 3 | from pykinect2 import PyKinectRuntime 4 | 5 | import ctypes 6 | import _ctypes 7 | import pygame 8 | import sys 9 | 10 | import socket 11 | import sys 12 | import os 13 | 14 | PORT = 6660 15 | 16 | class KinectBodyServer(object): 17 | def __init__(self): 18 | 19 | 20 | 21 | 22 | self._connection = None 23 | self._clientAddress = None 24 | self._socket = None 25 | 26 | # Create a UDS socket 27 | self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 28 | 29 | # Bind the socket to the port 30 | print("server on localhost") 31 | self._socket.bind(("",PORT)) 32 | 33 | # Listen for incoming connections 34 | self._socket.listen(1) 35 | 36 | def waitForConnection(self): 37 | # Wait for a connection 38 | print("waiting for a connection") 39 | self._connection, self._clientAddress = self._socket.accept() 40 | 41 | def sendValues(self, joints): 42 | # Torso 43 | j1x = joints[PyKinectV2.JointType_Head].Position.x 44 | j2x = joints[PyKinectV2.JointType_Neck].Position.x 45 | j3x = joints[PyKinectV2.JointType_SpineMid].Position.x 46 | j4x = joints[PyKinectV2.JointType_SpineBase].Position.x 47 | j5x = joints[PyKinectV2.JointType_SpineShoulder].Position.x 48 | j6x = joints[PyKinectV2.JointType_ShoulderRight].Position.x 49 | j7x = joints[PyKinectV2.JointType_ShoulderLeft].Position.x 50 | j8x = joints[PyKinectV2.JointType_HipRight].Position.x 51 | j9x = joints[PyKinectV2.JointType_HipLeft].Position.x 52 | 53 | # Right Arm 54 | j10x = joints[PyKinectV2.JointType_ElbowRight].Position.x 55 | j11x = joints[PyKinectV2.JointType_WristRight].Position.x 56 | j12x = joints[PyKinectV2.JointType_HandRight].Position.x 57 | j13x = joints[PyKinectV2.JointType_HandTipRight].Position.x 58 | j14x = joints[PyKinectV2.JointType_ThumbRight].Position.x 59 | 60 | # Left Arm 61 | j15x = joints[PyKinectV2.JointType_ElbowLeft].Position.x 62 | j16x = joints[PyKinectV2.JointType_WristLeft].Position.x 63 | j17x = joints[PyKinectV2.JointType_HandLeft].Position.x 64 | j18x = joints[PyKinectV2.JointType_HandTipLeft].Position.x 65 | j19x = joints[PyKinectV2.JointType_ThumbLeft].Position.x 66 | 67 | # Right Leg 68 | j20x = joints[PyKinectV2.JointType_KneeRight].Position.x 69 | j21x = joints[PyKinectV2.JointType_AnkleRight].Position.x 70 | j22x = joints[PyKinectV2.JointType_FootRight].Position.x 71 | 72 | # Left Leg 73 | j23x = joints[PyKinectV2.JointType_KneeLeft].Position.x 74 | j24x = joints[PyKinectV2.JointType_AnkleLeft].Position.x 75 | j25x = joints[PyKinectV2.JointType_FootLeft].Position.x 76 | 77 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,"%(j1x,j2x,j3x,j4x,j5x,j6x,j7x,j8x,j9x,j10x,j11x,j12x,j13x,j14x,j15x,j16x,j17x,j18x,j19x,j20x,j21x,j22x,j23x,j24x,j25x)).encode()) 78 | 79 | # Torso 80 | j1y = joints[PyKinectV2.JointType_Head].Position.y 81 | j2y = joints[PyKinectV2.JointType_Neck].Position.y 82 | j3y = joints[PyKinectV2.JointType_SpineMid].Position.y 83 | j4y = joints[PyKinectV2.JointType_SpineBase].Position.y 84 | j5y = joints[PyKinectV2.JointType_SpineShoulder].Position.y 85 | j6y = joints[PyKinectV2.JointType_ShoulderRight].Position.y 86 | j7y = joints[PyKinectV2.JointType_ShoulderLeft].Position.y 87 | j8y = joints[PyKinectV2.JointType_HipRight].Position.y 88 | j9y = joints[PyKinectV2.JointType_HipLeft].Position.y 89 | 90 | # Right Arm 91 | j10y = joints[PyKinectV2.JointType_ElbowRight].Position.y 92 | j11y = joints[PyKinectV2.JointType_WristRight].Position.y 93 | j12y = joints[PyKinectV2.JointType_HandRight].Position.y 94 | j13y = joints[PyKinectV2.JointType_HandTipRight].Position.y 95 | j14y = joints[PyKinectV2.JointType_ThumbRight].Position.y 96 | 97 | # Left Arm 98 | j15y = joints[PyKinectV2.JointType_ElbowLeft].Position.y 99 | j16y = joints[PyKinectV2.JointType_WristLeft].Position.y 100 | j17y = joints[PyKinectV2.JointType_HandLeft].Position.y 101 | j18y = joints[PyKinectV2.JointType_HandTipLeft].Position.y 102 | j19y = joints[PyKinectV2.JointType_ThumbLeft].Position.y 103 | 104 | # Right Leg 105 | j20y = joints[PyKinectV2.JointType_KneeRight].Position.y 106 | j21y = joints[PyKinectV2.JointType_AnkleRight].Position.y 107 | j22y = joints[PyKinectV2.JointType_FootRight].Position.y 108 | 109 | # Left Leg 110 | j23y = joints[PyKinectV2.JointType_KneeLeft].Position.y 111 | j24y = joints[PyKinectV2.JointType_AnkleLeft].Position.y 112 | j25y = joints[PyKinectV2.JointType_FootLeft].Position.y 113 | 114 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,"%(j1y,j2y,j3y,j4y,j5y,j6y,j7y,j8y,j9y,j10y,j11y,j12y,j13y,j14y,j15y,j16y,j17y,j18y,j19y,j20y,j21y,j22y,j23y,j24y,j25y)).encode()) 115 | 116 | # Torso 117 | j1z = joints[PyKinectV2.JointType_Head].Position.z 118 | j2z = joints[PyKinectV2.JointType_Neck].Position.z 119 | j3z = joints[PyKinectV2.JointType_SpineMid].Position.z 120 | j4z = joints[PyKinectV2.JointType_SpineBase].Position.z 121 | j5z = joints[PyKinectV2.JointType_SpineShoulder].Position.z 122 | j6z = joints[PyKinectV2.JointType_ShoulderRight].Position.z 123 | j7z = joints[PyKinectV2.JointType_ShoulderLeft].Position.z 124 | j8z = joints[PyKinectV2.JointType_HipRight].Position.z 125 | j9z = joints[PyKinectV2.JointType_HipLeft].Position.z 126 | 127 | # Right Arm 128 | j10z = joints[PyKinectV2.JointType_ElbowRight].Position.z 129 | j11z = joints[PyKinectV2.JointType_WristRight].Position.z 130 | j12z = joints[PyKinectV2.JointType_HandRight].Position.z 131 | j13z = joints[PyKinectV2.JointType_HandTipRight].Position.z 132 | j14z = joints[PyKinectV2.JointType_ThumbRight].Position.z 133 | 134 | # Left Arm 135 | j15z = joints[PyKinectV2.JointType_ElbowLeft].Position.z 136 | j16z = joints[PyKinectV2.JointType_WristLeft].Position.z 137 | j17z = joints[PyKinectV2.JointType_HandLeft].Position.z 138 | j18z = joints[PyKinectV2.JointType_HandTipLeft].Position.z 139 | j19z = joints[PyKinectV2.JointType_ThumbLeft].Position.z 140 | 141 | # Right Leg 142 | j20z = joints[PyKinectV2.JointType_KneeRight].Position.z 143 | j21z = joints[PyKinectV2.JointType_AnkleRight].Position.z 144 | j22z = joints[PyKinectV2.JointType_FootRight].Position.z 145 | 146 | # Left Leg 147 | j23z = joints[PyKinectV2.JointType_KneeLeft].Position.z 148 | j24z = joints[PyKinectV2.JointType_AnkleLeft].Position.z 149 | j25z = joints[PyKinectV2.JointType_FootLeft].Position.z 150 | 151 | self._connection.sendall(("%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4f,%3.4fEOD"%(j1z,j2z,j3z,j4z,j5z,j6z,j7z,j8z,j9z,j10z,j11z,j12z,j13z,j14z,j15z,j16z,j17z,j18z,j19z,j20z,j21z,j22z,j23z,j24z,j25z)).encode()) 152 | 153 | 154 | def closeConnection(self): 155 | self._connection.close(); 156 | 157 | 158 | 159 | 160 | class BodyGameRuntime(object): 161 | def __init__(self): 162 | pygame.init() 163 | 164 | # Used to manage how fast the screen updates 165 | self._clock = pygame.time.Clock() 166 | 167 | # Set the width and height of the screen [width, height] 168 | self._infoObject = pygame.display.Info() 169 | self._screen = pygame.display.set_mode((self._infoObject.current_w >> 1, self._infoObject.current_h >> 1), 170 | pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE, 32) 171 | 172 | pygame.display.set_caption("Kinect for Blender v2 server") 173 | 174 | 175 | 176 | # Kinect runtime object, we want only color and body frames 177 | self._kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Body) 178 | 179 | # back buffer surface for getting Kinect color frames, 32bit color, width and height equal to the Kinect color frame size 180 | self._frame_surface = pygame.Surface((self._kinect.color_frame_desc.Width, self._kinect.color_frame_desc.Height), 0, 32) 181 | 182 | print(self._kinect) 183 | 184 | # here we will store skeleton data 185 | self._bodies = None 186 | 187 | # Loop until the user clicks the close button. 188 | self._done = False 189 | 190 | # Used to manage how fast the screen updates 191 | self._clock = pygame.time.Clock() 192 | 193 | # Torso 194 | # PyKinectV2.JointType_Head -> PyKinectV2.JointType_Neck 195 | # PyKinectV2.JointType_Neck -> PyKinectV2.JointType_SpineShoulder); 196 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_SpineMid); 197 | # PyKinectV2.JointType_SpineMid -> PyKinectV2.JointType_SpineBase); 198 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_ShoulderRight); 199 | # PyKinectV2.JointType_SpineShoulder -> PyKinectV2.JointType_ShoulderLeft); 200 | # PyKinectV2.JointType_SpineBase -> PyKinectV2.JointType_HipRight); 201 | # PyKinectV2.JointType_SpineBase -> PyKinectV2.JointType_HipLeft); 202 | 203 | # Right Arm 204 | # PyKinectV2.JointType_ShoulderRight -> PyKinectV2.JointType_ElbowRight); 205 | # PyKinectV2.JointType_ElbowRight -> PyKinectV2.JointType_WristRight); 206 | # PyKinectV2.JointType_WristRight -> PyKinectV2.JointType_HandRight); 207 | # PyKinectV2.JointType_HandRight -> PyKinectV2.JointType_HandTipRight); 208 | # PyKinectV2.JointType_WristRight -> PyKinectV2.JointType_ThumbRight); 209 | 210 | # Left Arm 211 | # PyKinectV2.JointType_ShoulderLeft -> PyKinectV2.JointType_ElbowLeft); 212 | # PyKinectV2.JointType_ElbowLeft -> PyKinectV2.JointType_WristLeft); 213 | # PyKinectV2.JointType_WristLeft -> PyKinectV2.JointType_HandLeft); 214 | # PyKinectV2.JointType_HandLeft -> PyKinectV2.JointType_HandTipLeft); 215 | # PyKinectV2.JointType_WristLeft -> PyKinectV2.JointType_ThumbLeft); 216 | 217 | # Right Leg 218 | # PyKinectV2.JointType_HipRight -> PyKinectV2.JointType_KneeRight); 219 | # PyKinectV2.JointType_KneeRight -> PyKinectV2.JointType_AnkleRight); 220 | # PyKinectV2.JointType_AnkleRight -> PyKinectV2.JointType_FootRight); 221 | 222 | # Left Leg 223 | # PyKinectV2.JointType_HipLeft -> PyKinectV2.JointType_KneeLeft); 224 | # PyKinectV2.JointType_KneeLeft -> PyKinectV2.JointType_AnkleLeft); 225 | # PyKinectV2.JointType_AnkleLeft -> PyKinectV2.JointType_FootLeft); 226 | 227 | 228 | self._server = KinectBodyServer() 229 | 230 | self._server.waitForConnection() 231 | 232 | 233 | def draw_color_frame(self, frame, target_surface): 234 | target_surface.lock() 235 | address = self._kinect.surface_as_array(target_surface.get_buffer()) 236 | ctypes.memmove(address, frame.ctypes.data, frame.size) 237 | del address 238 | target_surface.unlock() 239 | 240 | 241 | def run(self): 242 | 243 | 244 | # -------- Main Program Loop ----------- 245 | while not self._done: 246 | # --- Main event loop 247 | for event in pygame.event.get(): # User did something (including Ctrl+C) 248 | if event.type == pygame.QUIT: # If user clicked close 249 | self._done = True # Flag that we are done so we exit this loop 250 | 251 | elif event.type == pygame.VIDEORESIZE: # window resized 252 | self._screen = pygame.display.set_mode(event.dict['size'], 253 | pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE, 32) 254 | 255 | # --- Getting frames and drawing 256 | # --- Woohoo! We've got a color frame! Let's fill out back buffer surface with frame's data 257 | if self._kinect.has_new_color_frame(): 258 | frame = self._kinect.get_last_color_frame() 259 | self.draw_color_frame(frame, self._frame_surface) 260 | frame = None 261 | 262 | 263 | # --- Cool! We have a body frame, so can get skeletons 264 | if self._kinect.has_new_body_frame(): 265 | self._bodies = self._kinect.get_last_body_frame() 266 | 267 | 268 | # --- draw skeletons to _frame_surface 269 | body = None 270 | if self._bodies is not None: 271 | for i in range(0,len(self._bodies.bodies)): 272 | if self._bodies.bodies[i].is_tracked: 273 | body = self._bodies.bodies[i] 274 | 275 | 276 | if body is not None: 277 | joints = body.joints 278 | print(joints[PyKinectV2.JointType_Head].Position.x) 279 | self._server.sendValues(joints) 280 | # convert joint coordinates to color space 281 | #joint_points = self._kinect.body_joints_to_color_space(joints) 282 | #self.draw_body(joints, joint_points) 283 | 284 | s = self._server._connection.recv(3) 285 | else: 286 | print("no body") 287 | self._server._connection.sendall("EOD".encode()) 288 | 289 | s = self._server._connection.recv(3) 290 | 291 | # --- copy back buffer surface pixels to the screen, resize it if needed and keep aspect ratio 292 | # --- (screen size may be different from Kinect's color frame size) 293 | h_to_w = float(self._frame_surface.get_height()) / self._frame_surface.get_width() 294 | target_height = int(h_to_w * self._screen.get_width()) 295 | surface_to_draw = pygame.transform.scale(self._frame_surface, (self._screen.get_width(), target_height)); 296 | self._screen.blit(surface_to_draw, (0,0)) 297 | surface_to_draw = None 298 | pygame.display.update() 299 | 300 | # --- Go ahead and update the screen with what we've drawn. 301 | pygame.display.flip() 302 | 303 | # --- Limit to 60 frames per second 304 | self._clock.tick(60) 305 | 306 | 307 | 308 | # Close our Kinect sensor, close the window and quit. 309 | try: 310 | self._server.closeConnection() 311 | finally: 312 | pass 313 | 314 | # Close our Kinect sensor, close the window and quit. 315 | self._kinect.close() 316 | pygame.quit() 317 | 318 | 319 | 320 | __main__ = "Kinect server" 321 | game = BodyGameRuntime(); 322 | game.run(); 323 | 324 | --------------------------------------------------------------------------------