1+ # -*- coding: utf-8 -*-
2+ # @Time : 2018/1/7 11:33
3+ # @Author : play4fun
4+ # @File : freenect_test1.py
5+ # @Software: PyCharm
6+
7+ """
8+ freenect_test1.py:
9+ 不行。段错误 (核心已转储)
10+ """
11+
12+ import pygame
13+ import numpy as np
14+ import sys
15+ from freenect import sync_get_depth as get_depth
16+
17+
18+ def make_gamma ():
19+ """
20+ Create a gamma table
21+ """
22+ num_pix = 2048 # there's 2048 different possible depth values
23+ npf = float (num_pix )
24+ _gamma = np .empty ((num_pix , 3 ), dtype = np .uint16 )
25+ for i in range (num_pix ):
26+ v = i / npf
27+ v = pow (v , 3 ) * 6
28+ pval = int (v * 6 * 256 )
29+ lb = pval & 0xff
30+ pval >>= 8
31+ if pval == 0 :
32+ a = np .array ([255 , 255 - lb , 255 - lb ], dtype = np .uint8 )
33+ elif pval == 1 :
34+ a = np .array ([255 , lb , 0 ], dtype = np .uint8 )
35+ elif pval == 2 :
36+ a = np .array ([255 - lb , lb , 0 ], dtype = np .uint8 )
37+ elif pval == 3 :
38+ a = np .array ([255 - lb , 255 , 0 ], dtype = np .uint8 )
39+ elif pval == 4 :
40+ a = np .array ([0 , 255 - lb , 255 ], dtype = np .uint8 )
41+ elif pval == 5 :
42+ a = np .array ([0 , 0 , 255 - lb ], dtype = np .uint8 )
43+ else :
44+ a = np .array ([0 , 0 , 0 ], dtype = np .uint8 )
45+
46+ _gamma [i ] = a
47+ return _gamma
48+
49+
50+ gamma = make_gamma ()
51+
52+
53+ if __name__ == "__main__" :
54+ fpsClock = pygame .time .Clock ()
55+ FPS = 30 # kinect only outputs 30 fps
56+ disp_size = (640 , 480 )
57+ pygame .init ()
58+ screen = pygame .display .set_mode (disp_size )
59+ font = pygame .font .Font ('slkscr.ttf' , 32 ) #TODO provide your own font
60+ while True :
61+ events = pygame .event .get ()
62+ for e in events :
63+ if e .type == pygame .QUIT :
64+ sys .exit ()
65+ fps_text = "FPS: {0:.2f}" .format (fpsClock .get_fps ())
66+ # draw the pixels
67+
68+ depth = np .rot90 (get_depth ()[0 ]) # get the depth readinngs from the camera
69+ pixels = gamma [depth ] # the colour pixels are the depth readings overlayed onto the gamma table
70+ temp_surface = pygame .Surface (disp_size )
71+ pygame .surfarray .blit_array (temp_surface , pixels )
72+ pygame .transform .scale (temp_surface , disp_size , screen )
73+ screen .blit (font .render (fps_text , 1 , (255 , 255 , 255 )), (30 , 30 ))
74+ pygame .display .flip ()
75+ fpsClock .tick (FPS )
0 commit comments