def test_runtime_creation(self):
        # create the runtime, check the instance
        with Runtime() as nui:
            self.assertEqual(nui.instance_index, 0)

        # we should be able to create a 2nd runtime after the first is disposed
        with Runtime() as nui2:
            self.assertEqual(nui2.instance_index, 0)

        # accessing a disposed runtime should throw
        self.assertRaises(KinectError, lambda: nui2.instance_index)
    def test_video_stream(self):
        with Runtime() as nui:
            nui.video_stream.open(ImageStreamType.Depth, 2,
                                  ImageResolution.Resolution640x480,
                                  ImageType.Color)

            # we can only open a single stream at a time
            self.assertRaises(KinectError, nui.video_stream.open,
                              ImageStreamType.Video, 2,
                              ImageResolution.Resolution1280x1024,
                              ImageType.Color)

        valid_resolutions = {
            ImageType.Color: (ImageResolution.Resolution1280x1024,
                              ImageResolution.Resolution640x480),
            ImageType.ColorYuv: (ImageResolution.Resolution640x480, ),
            ImageType.Color: (ImageResolution.Resolution640x480, ),
            ImageType.DepthAndPlayerIndex: (ImageResolution.Resolution320x240,
                                            ImageResolution.Resolution80x60),
            ImageType.Depth: (ImageResolution.Resolution320x240,
                              ImageResolution.Resolution640x480,
                              ImageResolution.Resolution80x60),
        }

        for image_type, resolution_list in valid_resolutions.items():
            for resolution in resolution_list:
                with Runtime() as nui:
                    nui.video_stream.open(ImageStreamType.Video, 2, resolution,
                                          image_type)

        with Runtime() as nui:
            invalid_resolutions = {
                ImageType.Color: (ImageResolution.Resolution320x240,
                                  ImageResolution.Resolution80x60),
                ImageType.DepthAndPlayerIndex:
                (ImageResolution.Resolution1280x1024,
                 ImageResolution.Resolution640x480),
                ImageType.ColorYuv: (ImageResolution.Resolution1280x1024,
                                     ImageResolution.Resolution320x240,
                                     ImageResolution.Resolution80x60),
                ImageType.Color: (ImageResolution.Resolution320x240,
                                  ImageResolution.Resolution80x60),
                ImageType.Depth: (ImageResolution.Resolution1280x1024, ),
            }

            for image_type, resolution_list in invalid_resolutions.items():
                for resolution in resolution_list:
                    self.assertRaises(KinectError, nui.video_stream.open,
                                      ImageStreamType.Video, 2, resolution,
                                      image_type)
 def test_skeleton_engine(self):
     with Runtime() as nui:
         self.assertEqual(nui.skeleton_engine.enabled, False)
         nui.skeleton_engine.enabled = True
         self.assertEqual(nui.skeleton_engine.enabled, True)
         frame = nui.skeleton_engine.get_next_frame()
Exemple #4
0
def main():
    # First of all, check if there is a sensor plugged in
    if nui._NuiGetSensorCount() < 1:
        sys.stderr.write("Error: no Kinect sensors available\n")
        return 1

    # Initialize Everything
    pygame.init()
    pygame.font.init()
    global fontBig, fontSub
    fontBig = pygame.font.SysFont("", 22, True)
    fontSub = pygame.font.SysFont("", 20)

    # initialization of images and their surfaces
    for index in range(4):
        img_device_srf[index] = pygame.Surface(PREVIEW_RESOLUTION, 0, 8)
        img_skel_srf[index] = pygame.Surface(PREVIEW_RESOLUTION)
        img_skel_srf[index].set_colorkey(THECOLORS["black"])
        img_device[index] = gui.Image(img_device_srf[index])
        img_device_skeleton[index] = gui.Image(img_skel_srf[index])

    for img in img_device_srf:
        img.set_palette(tuple([(i, i, i) for i in range(256)]))

    screen = pygame.display.set_mode(SCREEN_SIZE)
    pygame.display.set_caption('Kinect tracker manager')

    # create GUI object
    this_gui = gui.App()

    # layout using document
    lo = gui.Container(width=SCREEN_SIZE[0], height=SCREEN_SIZE[1])

    # draw the GUI
    draw_gui_kinect("KINECT A", 0, lo, 5, 5)
    draw_gui_kinect("KINECT B", 1, lo, 435, 5)
    draw_gui_kinect("KINECT C", 2, lo, 5, 260)
    draw_gui_kinect("KINECT D", 3, lo, 435, 260)

    # add layout to the GUI
    this_gui.init(lo)

    global kinects
    global kinect_cam
    for i in range(len(kinects)):
        # init all Kinects
        try:
            kinects[i] = Runtime(
                RuntimeOptions.uses_depth_and_player_index
                | RuntimeOptions.uses_skeletal_tracking, i)

            kinects[i].depth_stream.open(
                nui.ImageStreamType.depth, 2,
                nui.ImageResolution.resolution_640x480,
                nui.ImageType.depth_and_player_index)
            kinects[i].depth_frame_ready += lambda f, i=i: update_depth(f, i)

            kinects[i].skeleton_engine.enabled = True
            kinects[i].skeleton_frame_ready += lambda f, i=i: update_skel(f, i)

            kinect_cam[i] = nui.Camera(kinects[i])

            # set the right tilt angle
            widgets[i]['device_slider'].value = -kinect_cam[i].elevation_angle

            toggle_kinect_manager_gui(i)
        except:
            kinects[i] = None

    # Main Loop
    while True:

        # Handle Input Events
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return

            # pass event to gui
            this_gui.event(event)

        # clear background
        screen.fill((250, 250, 250))

        # Draw GUI
        try:
            this_gui.paint(screen)
        except pygame.error as e:
            pass

        pygame.display.flip()