Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
sensors:kinect_library [2017/03/01 17:08] m16devan [Processing data] |
sensors:kinect_library [2019/04/25 14:08] (current) |
||
|---|---|---|---|
| Line 277: | Line 277: | ||
| ==== Processing data ==== | ==== Processing data ==== | ||
| - | Here is some examples how to proceed data in order to display it using pygame. | + | Here is some examples how to proceed data in order to display it using pygame. Some packages must be first imported for this example |
| + | |||
| + | <code python> | ||
| + | import ctypes | ||
| + | import _ctypes | ||
| + | import pymedia.video.vcodec as vcodec | ||
| + | import pygame | ||
| + | from pygame.locals import * | ||
| + | import numpy as np | ||
| + | </code> | ||
| === Color data === | === Color data === | ||
| Line 283: | Line 292: | ||
| <code python> | <code python> | ||
| pygame.init() | pygame.init() | ||
| - | screen = pygame.display.set_mode((self._infoObject.current_w >> 1, self._infoObject.current_h >> 1), | + | infoObject = pygame.display.Info() |
| + | screen = pygame.display.set_mode((infoObject.current_w >> 1, infoObject.current_h >> 1), | ||
| pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE, 32) | pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE, 32) | ||
| # back buffer surface for getting Kinect color frames, 32bit color, width and height equal to the Kinect color frame size | # back buffer surface for getting Kinect color frames, 32bit color, width and height equal to the Kinect color frame size | ||
| - | frame_surface = pygame.Surface((self._kinect.color_frame_desc.Width, self._kinect.color_frame_desc.Height), 0, 32) | + | frame_surface = pygame.Surface((kinect.color_frame_desc.Width, kinect.color_frame_desc.Height), 0, 32) |
| frame_surface.lock() | frame_surface.lock() | ||
| Line 296: | Line 306: | ||
| # --- copy back buffer surface pixels to the screen, resize it if needed and keep aspect ratio | # --- copy back buffer surface pixels to the screen, resize it if needed and keep aspect ratio | ||
| # --- (screen size may be different from Kinect's color frame size) | # --- (screen size may be different from Kinect's color frame size) | ||
| - | h_to_w = float(self._frame_surface.get_height()) / self._frame_surface.get_width() | + | h_to_w = float(frame_surface.get_height()) / frame_surface.get_width() |
| - | target_height = int(h_to_w * self._screen.get_width()) | + | target_height = int(h_to_w * screen.get_width()) |
| - | surface_to_draw = pygame.transform.scale(self._frame_surface, (self._screen.get_width(), target_height)); | + | surface_to_draw = pygame.transform.scale(frame_surface, (screen.get_width(), target_height)); |
| - | self._screen.blit(surface_to_draw, (0,0)) | + | screen.blit(surface_to_draw, (0,0)) |
| surface_to_draw = None | surface_to_draw = None | ||
| pygame.display.update() | pygame.display.update() | ||
| Line 326: | Line 336: | ||
| </code> | </code> | ||
| - | Similarly to C++ library, two external functions are used to display the skeleton | + | Similarly to C library, two external functions are used to display the skeleton |
| <code python> | <code python> | ||
| Line 384: | Line 394: | ||
| draw_body_bone(joints, jointColorPoints, color, PyKinectV2.JointType_AnkleLeft, PyKinectV2.JointType_FootLeft); | draw_body_bone(joints, jointColorPoints, color, PyKinectV2.JointType_AnkleLeft, PyKinectV2.JointType_FootLeft); | ||
| + | </code> | ||
| + | |||
| + | === Depth data === | ||
| + | This example shows how to display the depth frame using pygame. The depth frame has been captured in 'frameD'. | ||
| + | |||
| + | <code python> | ||
| + | pygame.init() | ||
| + | infoObject = pygame.display.Info() | ||
| + | screen = pygame.display.set_mode((infoObject.current_w >> 1, infoObject.current_h >> 1), | ||
| + | pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE, 32) | ||
| + | # back buffer surface for getting Kinect depth frames, 16 bit depth, width and height equal to the Kinect depth frame size | ||
| + | frame_surface = pygame.Surface((kinect.depth_frame_desc.Width, kinect.depth_frame_desc.Height), 0, 24) | ||
| + | |||
| + | frame_surface.lock() | ||
| + | f8=np.uint8(frameD.clip(1,4000)/16.) | ||
| + | frame8bit=np.dstack((f8,f8,f8)) | ||
| + | address = kinect.surface_as_array(target_surface.get_buffer()) | ||
| + | ctypes.memmove(address, frame8bit.ctypes.data, frame8bit.size) | ||
| + | del address | ||
| + | frame_surface.unlock() | ||
| + | |||
| + | # --- copy back buffer surface pixels to the screen, resize it if needed and keep aspect ratio | ||
| + | h_to_w = float(frame_surface.get_height()) / frame_surface.get_width() | ||
| + | target_height = int(h_to_w * screen.get_width()) | ||
| + | surface_to_draw = pygame.transform.scale(frame_surface, (screen.get_width(), target_height)); | ||
| + | screen.blit(surface_to_draw, (0,0)) | ||
| + | surface_to_draw = None | ||
| + | pygame.display.update() | ||
| + | |||
| + | # --- Go ahead and update the screen with what we've drawn. | ||
| + | pygame.display.flip() | ||
| + | </code> | ||
| + | |||
| + | ==== Closing the Kinect ==== | ||
| + | <code python> | ||
| + | kinect.close() | ||
| </code> | </code> | ||