├── .DS_Store
├── LICENSE.txt
├── PyKinect2.pyproj
├── PyKinect2.sln
├── README.md
├── examples
├── PyKinectBodyGame.py
├── __init__.py
├── pointer.png
└── right_hand_filtered.png
├── pykinect2
├── PyKinectRuntime.py
├── PyKinectV2.py
└── __init__.py
└── setup.py
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/abhirajD/PyKinect/2aed4ff72111f1ec770f8488c820cf84484274f7/.DS_Store
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) Microsoft
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/PyKinect2.pyproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | 2.0
6 | 862156b9-96a7-4f85-bb66-b7ec70418cf7
7 | .
8 | pykinect2/PyKinectBodyGame.py
9 |
10 |
11 | .
12 | .
13 | PyKinect2
14 | PyKinect2
15 | False
16 |
17 |
18 | true
19 | false
20 |
21 |
22 | true
23 | false
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | 10.0
35 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets
36 |
37 |
38 |
39 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/PyKinect2.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.22609.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "PyKinect2", "PyKinect2.pyproj", "{862156B9-96A7-4F85-BB66-B7EC70418CF7}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {862156B9-96A7-4F85-BB66-B7EC70418CF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {862156B9-96A7-4F85-BB66-B7EC70418CF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | EndGlobal
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PyKinect2
2 |
3 | Enables writing Kinect applications, games, and experiences using Python. Inspired by the original [PyKinect project on CodePlex](http://pytools.codeplex.com/wikipage?title=PyKinect).
4 |
5 | Only color, depth, body and body index frames are supported in this version.
6 | PyKinectBodyGame is a sample game. It demonstrates how to use Kinect color and body frames.
7 |
8 |
9 | ## Prerequisites
10 |
11 | The easiest way to get most of the pre-requisites is to use Anaconda which includes NumPy. You'll then need to pip install comtypes. The PyKinectBodyGame sample requires PyGame which needs to be manually installed.
12 |
13 | 1. Download [Anaconda](https://store.continuum.io/cshop/anaconda/) get the 32-bit version. This includes NumPy.
14 | 2. pip install comtypes
15 | 3. Install the [Kinect for Windows SDK v2](http://aka.ms/k4wv2sdk)
16 |
17 | Full List of Dependencies
18 | * [Python 2.7.x or 3.4 and higher](https://www.python.org/)
19 | * [NumPy](http://www.numpy.org/)
20 | * [comtypes](https://github.com/enthought/comtypes/)
21 | * [Kinect for Windows SDK v2](http://aka.ms/k4wv2sdk)
22 | * [Kinect v2 sensor and adapter](http://aka.ms/k4wv2purchase) Note: you can use a Kinect for Xbox One as long as you also have the Kinect Adapter for Windows
23 | * [PyGame](http://www.pygame.org) - for running PyKinectBodyGame sample
24 |
25 |
26 | 
27 |
28 |
29 | ## Installation
30 |
31 | The package can be installed through pip using the usual means:
32 | ```
33 | pip install pykinect2
34 | ````
35 | If you are using a virtual environment, be sure to activate it first.
36 |
37 | For more information, please see https://pip.pypa.io/en/latest/user_guide.html#installing-packages
38 |
39 |
40 | ## Installation (Manual)
41 |
42 | To install the package manually, clone this repository to a local folder and include it in the appropriate python environment. If installing in a virtual environment, be sure to install all required dependencies (above).
43 |
44 | For example:
45 | ```
46 | cd c:\projects\myproject\env\
47 | /Scripts/activate.bat
48 |
49 | easy_install -a c:\projects\downloads\PyKinect2
50 | ```
51 | After installation is complete, you can launch the interactive python shell and `import pykinect2` to ensure everything has been installed properly.
52 |
53 | Core helper classes for working with the Kinect sensor are located in PyKinectRuntime.py. For usage examples, please see /examples/PyKinectBodyGame.py.
54 |
--------------------------------------------------------------------------------
/examples/PyKinectBodyGame.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 | if sys.hexversion >= 0x03000000:
11 | import _thread as thread
12 | else:
13 | import thread
14 |
15 | # colors for drawing different bodies
16 | SKELETON_COLORS = [pygame.color.THECOLORS["red"],
17 | pygame.color.THECOLORS["blue"],
18 | pygame.color.THECOLORS["green"],
19 | pygame.color.THECOLORS["orange"],
20 | pygame.color.THECOLORS["purple"],
21 | pygame.color.THECOLORS["yellow"],
22 | pygame.color.THECOLORS["violet"]]
23 |
24 |
25 | class BodyGameRuntime(object):
26 | def __init__(self):
27 | pygame.init()
28 |
29 | # Used to manage how fast the screen updates
30 | self._clock = pygame.time.Clock()
31 |
32 | # Set the width and height of the screen [width, height]
33 | self._infoObject = pygame.display.Info()
34 | self._screen = pygame.display.set_mode((self._infoObject.current_w >> 1, self._infoObject.current_h >> 1),
35 | pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE, 32)
36 |
37 | pygame.display.set_caption("Kinect for Windows v2 Body Game")
38 |
39 | # Loop until the user clicks the close button.
40 | self._done = False
41 |
42 | # Used to manage how fast the screen updates
43 | self._clock = pygame.time.Clock()
44 |
45 | # Kinect runtime object, we want only color and body frames
46 | self._kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Body)
47 |
48 | # back buffer surface for getting Kinect color frames, 32bit color, width and height equal to the Kinect color frame size
49 | self._frame_surface = pygame.Surface((self._kinect.color_frame_desc.Width, self._kinect.color_frame_desc.Height), 0, 32)
50 |
51 | # here we will store skeleton data
52 | self._bodies = None
53 |
54 |
55 | def draw_body_bone(self, joints, jointPoints, color, joint0, joint1):
56 | joint0State = joints[joint0].TrackingState;
57 | joint1State = joints[joint1].TrackingState;
58 |
59 | # both joints are not tracked
60 | if (joint0State == PyKinectV2.TrackingState_NotTracked) or (joint1State == PyKinectV2.TrackingState_NotTracked):
61 | return
62 |
63 | # both joints are not *really* tracked
64 | if (joint0State == PyKinectV2.TrackingState_Inferred) and (joint1State == PyKinectV2.TrackingState_Inferred):
65 | return
66 |
67 | # ok, at least one is good
68 | start = (jointPoints[joint0].x, jointPoints[joint0].y)
69 | end = (jointPoints[joint1].x, jointPoints[joint1].y)
70 |
71 | try:
72 | pygame.draw.line(self._frame_surface, color, start, end, 8)
73 | except: # need to catch it due to possible invalid positions (with inf)
74 | pass
75 |
76 | def draw_body(self, joints, jointPoints, color):
77 | # Torso
78 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_Head, PyKinectV2.JointType_Neck);
79 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_Neck, PyKinectV2.JointType_SpineShoulder);
80 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_SpineShoulder, PyKinectV2.JointType_SpineMid);
81 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_SpineMid, PyKinectV2.JointType_SpineBase);
82 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_SpineShoulder, PyKinectV2.JointType_ShoulderRight);
83 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_SpineShoulder, PyKinectV2.JointType_ShoulderLeft);
84 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_SpineBase, PyKinectV2.JointType_HipRight);
85 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_SpineBase, PyKinectV2.JointType_HipLeft);
86 |
87 | # Right Arm
88 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_ShoulderRight, PyKinectV2.JointType_ElbowRight);
89 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_ElbowRight, PyKinectV2.JointType_WristRight);
90 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_WristRight, PyKinectV2.JointType_HandRight);
91 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_HandRight, PyKinectV2.JointType_HandTipRight);
92 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_WristRight, PyKinectV2.JointType_ThumbRight);
93 |
94 | # Left Arm
95 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_ShoulderLeft, PyKinectV2.JointType_ElbowLeft);
96 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_ElbowLeft, PyKinectV2.JointType_WristLeft);
97 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_WristLeft, PyKinectV2.JointType_HandLeft);
98 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_HandLeft, PyKinectV2.JointType_HandTipLeft);
99 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_WristLeft, PyKinectV2.JointType_ThumbLeft);
100 |
101 | # Right Leg
102 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_HipRight, PyKinectV2.JointType_KneeRight);
103 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_KneeRight, PyKinectV2.JointType_AnkleRight);
104 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_AnkleRight, PyKinectV2.JointType_FootRight);
105 |
106 | # Left Leg
107 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_HipLeft, PyKinectV2.JointType_KneeLeft);
108 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_KneeLeft, PyKinectV2.JointType_AnkleLeft);
109 | self.draw_body_bone(joints, jointPoints, color, PyKinectV2.JointType_AnkleLeft, PyKinectV2.JointType_FootLeft);
110 |
111 |
112 | def draw_color_frame(self, frame, target_surface):
113 | target_surface.lock()
114 | address = self._kinect.surface_as_array(target_surface.get_buffer())
115 | ctypes.memmove(address, frame.ctypes.data, frame.size)
116 | del address
117 | target_surface.unlock()
118 |
119 | def run(self):
120 | # -------- Main Program Loop -----------
121 | while not self._done:
122 | # --- Main event loop
123 | for event in pygame.event.get(): # User did something
124 | if event.type == pygame.QUIT: # If user clicked close
125 | self._done = True # Flag that we are done so we exit this loop
126 |
127 | elif event.type == pygame.VIDEORESIZE: # window resized
128 | self._screen = pygame.display.set_mode(event.dict['size'],
129 | pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE, 32)
130 |
131 | # --- Game logic should go here
132 |
133 | # --- Getting frames and drawing
134 | # --- Woohoo! We've got a color frame! Let's fill out back buffer surface with frame's data
135 | if self._kinect.has_new_color_frame():
136 | frame = self._kinect.get_last_color_frame()
137 | self.draw_color_frame(frame, self._frame_surface)
138 | frame = None
139 |
140 | # --- Cool! We have a body frame, so can get skeletons
141 | if self._kinect.has_new_body_frame():
142 | self._bodies = self._kinect.get_last_body_frame()
143 |
144 | # --- draw skeletons to _frame_surface
145 | if self._bodies is not None:
146 | for i in range(0, self._kinect.max_body_count):
147 | body = self._bodies.bodies[i]
148 | if not body.is_tracked:
149 | continue
150 |
151 | joints = body.joints
152 | # convert joint coordinates to color space
153 | joint_points = self._kinect.body_joints_to_color_space(joints)
154 | self.draw_body(joints, joint_points, SKELETON_COLORS[i])
155 |
156 | # --- copy back buffer surface pixels to the screen, resize it if needed and keep aspect ratio
157 | # --- (screen size may be different from Kinect's color frame size)
158 | h_to_w = float(self._frame_surface.get_height()) / self._frame_surface.get_width()
159 | target_height = int(h_to_w * self._screen.get_width())
160 | surface_to_draw = pygame.transform.scale(self._frame_surface, (self._screen.get_width(), target_height));
161 | self._screen.blit(surface_to_draw, (0,0))
162 | surface_to_draw = None
163 | pygame.display.update()
164 |
165 | # --- Go ahead and update the screen with what we've drawn.
166 | pygame.display.flip()
167 |
168 | # --- Limit to 60 frames per second
169 | self._clock.tick(60)
170 |
171 | # Close our Kinect sensor, close the window and quit.
172 | self._kinect.close()
173 | pygame.quit()
174 |
175 |
176 | __main__ = "Kinect v2 Body Game"
177 | game = BodyGameRuntime();
178 | game.run();
179 |
180 |
--------------------------------------------------------------------------------
/examples/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/abhirajD/PyKinect/2aed4ff72111f1ec770f8488c820cf84484274f7/examples/__init__.py
--------------------------------------------------------------------------------
/examples/pointer.png:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
739 |
740 | You signed in with another tab or window. Reload to refresh your session.
741 | You signed out in another tab or window. Reload to refresh your session.
742 |
739 |
740 | You signed in with another tab or window. Reload to refresh your session.
741 | You signed out in another tab or window. Reload to refresh your session.
742 |