Skip to content

Kinect v2 Python Setup Guide (64-bit Fix) #115

@RichLewi

Description

@RichLewi

Kinect v2 Python Setup Guide (64-bit Fix)

This guide outlines the steps required to get the Kinect for Windows v2 working with Python 3.8+ on a 64-bit Windows system using the pykinect2 library.

1. Prerequisites & Installation

  1. Kinect SDK 2.0: Download and install the Kinect for Windows SDK 2.0.
  2. USB Port: Ensure the Kinect is plugged into a USB 3.0 port.
  3. Python Environment: It is recommended to use a virtual environment (e.g., Conda).
pip install numpy opencv-python comtypes pykinect2 

2. Mandatory Library Patches

Because pykinect2 was designed for 32-bit Python 2.7/3.4, three manual code changes are required within your environment's site-packages folder.

Patch A: 64-bit Memory Alignment Fix

File: .../site-packages/pykinect2/PyKinectV2.py

Location: Line 2216

Action: Comment out the sizeof assertion. On 64-bit systems, this structure is 80 bytes, not 72, which causes a crash.

# Change this:
assert sizeof(tagSTATSTG) == 72, sizeof(tagSTATSTG)

# To this:
#assert sizeof(tagSTATSTG) == 72, sizeof(tagSTATSTG)

Patch B: Comtypes Version Bypass

File: .../site-packages/comtypes/_tlib_version_checker.py

Location: Inside def _check_version

Action: Add an immediate return to prevent the library from crashing due to version mismatches with modern comtypes.

def _check_version(actual, tlib_cached_mtime=None):
    return # Added to bypass version mismatch errors
    # ... rest of function

Patch C: Python 3.8+ Timing Fix

File: .../site-packages/pykinect2/PyKinectRuntime.py

Location: Lines 154, 312, 330, 350, 374

Action: time.clock() was removed in Python 3.8. Replace all occurrences with time.perf_counter().

# Change:
start_clock = time.clock()

# To:
start_clock = time.perf_counter()

3. Working Implementation Script

The following script initializes the Kinect, captures Color and Depth streams, and handles the BGRA-to-BGR conversion for OpenCV display.

import cv2
import numpy as np
from pykinect2 import PyKinectV2
from pykinect2 import PyKinectRuntime

# Initialize the Kinect runtime
kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Color | PyKinectV2.FrameSourceTypes_Depth)

while True:
    # --- Color Frame ---
    if kinect.has_new_color_frame():
        frame = kinect.get_last_color_frame()
        frame = frame.reshape((1080, 1920, 4))
        # Convert BGRA to BGR for OpenCV
        frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR) 
        frame = cv2.resize(frame, (960, 540))
        cv2.imshow('Kinect Color', frame)

    # --- Depth Frame ---
    if kinect.has_new_depth_frame():
        depth_frame = kinect.get_last_depth_frame()
        depth_frame = depth_frame.reshape((424, 512))
        # Normalize and colorize depth for visualization
        depth_8bit = (depth_frame / 4500.0 * 255).clip(0, 255).astype(np.uint8)
        depth_color = cv2.applyColorMap(depth_8bit, cv2.COLORMAP_JET)
        cv2.imshow('Kinect Depth', depth_color)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

kinect.close()
cv2.destroyAllWindows()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions