pythonkinectpykinect

Kinect Video Channel Setup Python


I have this program that I wrote to show the kinect video channel on a pygame screen.

import thread
import pygame
import easygui
from pykinect import nui

DEPTH_WINSIZE = (640, 480)

screen_lock = thread.allocate()
screen = None

tmp_s = pygame.Surface(DEPTH_WINSIZE, 0, 16)

s=None
def video_frame_ready(frame):
    with screen_lock:
        frame.image.copy_bits(tmp_s._pixels_address)
        arr2d = (pygame.surfarray.pixels2d(tmp_s) >> 7) & 255
        pygame.surfarray.blit_array(screen, arr2d)

        pygame.display.update()

def main():
    """Initialize and run the game"""
    pygame.init()
    global s
    s=0
    global arr
    arr = []

    # Initialize PyGame
    global screen
    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 8)
    screen.set_palette(tuple([(i, i, i) for i in range (256)]))
    pygame.display.set_caption("PyKinect Depth Map Example")

    angle = 0
    with nui.Runtime() as kinect:
        kinect.video_frame_ready += video_frame_ready
        kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)        
        # Main game loop
        while (True):
            event = pygame.event.wait()

            if (event == pygame.QUIT):
                break
if (__name__ == "__main__"):
    main()

At the frame.image.copy_bits(tmp_s._pixels_address) line, it gives me an error:

Unhandled exception in thread started by <bound method Runtime._event_thread of <pykinect.nui.Runtime object at 0x03FDB0D0>>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\pykinect-1.0-py2.7.egg\pykinect\nui\__init__.py", line 196, in _event_thread
    self.video_frame_ready.fire(depth_frame)     
  File "C:\Python27\lib\site-packages\pykinect-1.0-py2.7.egg\pykinect\nui\__init__.py", line 426, in fire
    handler(*args)
  File "C:/Users/navid/Desktop/Kinect_Project/Kinect_Color_Video.py", line 16, in video_frame_ready
    frame.image.copy_bits(tmp_s._pixels_address)
  File "C:\Python27\lib\site-packages\pykinect-1.0-py2.7.egg\pykinect\nui\structs.py", line 133, in copy_bits
    ctypes.memmove(dest, rect.bits, desc.height * rect.pitch)
WindowsError: exception: access violation writing 0x0A80C000

Is there a way to fix this problem? Any help is much appreciated.


Solution

  • Try this:

    import pygame
    import easygui
    from pykinect import nui
    
    VIDEO_WINSIZE = (640, 480)
    
    screen = None
    
    s=None
    def video_frame_ready(frame):
        frame.image.copy_bits(screen._pixels_address)
        pygame.display.update()
    
    def main():
        """Initialize and run the game"""
        pygame.init()
        global s
        s=0
        global arr
        arr = []
    
        # Initialize PyGame
        global screen
        screen = pygame.display.set_mode(VIDEO_WINSIZE, 0, 32)
    
        pygame.display.set_caption("PyKinect Video Example")
    
        angle = 0
        with nui.Runtime() as kinect:
            kinect.video_frame_ready += video_frame_ready
            kinect.video_stream.open(nui.ImageStreamType.Video, 2, nui.ImageResolution.Resolution640x480, nui.ImageType.Color)        
            # Main game loop
            while (True):
                event = pygame.event.wait()
    
                if (event == pygame.QUIT):
                    break
    if (__name__ == "__main__"):
        main()