1
+ '''
2
+ Short test file, may get longer
3
+ '''
4
+ from primesense import openni2
5
+ from primesense import _openni2 as c_api
6
+ #numpy, for matrix manipulation fo the images
7
+ import numpy as np
8
+ #matplotlib, for temporary display to check the images
9
+ import matplotlib .pyplot as plt
10
+
11
+ #takes frame data, and the type it is and displays the image
12
+ #frame_data = frame.get_buffer_as_blah(); thisType = numpy.someType
13
+ def print_frame (frame_data , thisType ):
14
+ #need to know what format to get the buffer in:
15
+ # if color pixel type is RGB888, then it must be uint8,
16
+ #otherwise it will split the pixels incorrectly
17
+ img = np .frombuffer (frame_data , dtype = thisType )
18
+ whatisit = img .size
19
+ #QVGA is what my camera defaulted to, so: 1 x 480 x 640
20
+ #also order was weird (1, 480, 640) not (640, 480, 1)
21
+ if whatisit == (640 * 480 * 1 ):#QVGA
22
+ img .shape = (1 , 480 , 640 )
23
+ #This order? Really? ^
24
+ #shape it accordingly
25
+ img = np .concatenate ((img , img , img ), axis = 0 )
26
+ img = np .swapaxes (img , 0 , 2 )
27
+ img = np .swapaxes (img , 0 , 1 )
28
+ elif whatisit == (640 * 480 * 3 ):
29
+ img .shape = (480 , 640 , 3 )
30
+ #these are, what is it, normalizsed?
31
+ else :
32
+ print "Frames are of size: " ,img .size
33
+ #images still appear to be reflected, but I don't need them to be correct in that way
34
+ print img .shape
35
+ #need both of follwoing: plt.imShow adds image to plot
36
+ plt .imshow (img )
37
+ #plt.show shows all the currently added figures
38
+ #plt.show()
39
+ plt .pause (0.1 )
40
+ plt .draw ()
41
+ plt .close ()
42
+
43
+ def print_frames (frame_data , frame_data2 , thisType , thisType2 ):
44
+ img = np .frombuffer (frame_data , dtype = thisType )
45
+ whatisit = img .size
46
+ print "Image size 1 " ,whatisit
47
+ if whatisit == (640 * 480 * 1 ): #QVGA, default
48
+ img .shape = (1 , 480 , 640 )
49
+ #This order? Really? ^
50
+ #shape it accordingly
51
+ img = np .concatenate ((img , img , img ), axis = 0 )
52
+ img = np .swapaxes (img , 0 , 2 )
53
+ img = np .swapaxes (img , 0 , 1 )
54
+ elif whatisit == (640 * 480 * 3 ):
55
+ img .shape = (480 , 640 , 3 )
56
+ #these are, what is it, normalizsed?
57
+ else :
58
+ print "Frames are of size: " ,img .size
59
+ print img .shape
60
+ plt .imshow (img )
61
+ plt .show ()
62
+
63
+ img1 = np .frombuffer (frame_data2 , dtype = thisType2 )
64
+ whatisit = img1 .size
65
+ print "Image size 2 " ,whatisit
66
+ if whatisit == (640 * 480 * 1 ): #QVGA, default
67
+ img1 .shape = (1 , 480 , 640 )
68
+ #This order? Really? ^
69
+ #shape it accordingly
70
+ img1 = np .concatenate ((img1 , img1 , img1 ), axis = 0 )
71
+ img1 = np .swapaxes (img1 , 0 , 2 )
72
+ img1 = np .swapaxes (img1 , 0 , 1 )
73
+ elif whatisit == (640 * 480 * 3 ):
74
+ img1 .shape = (480 , 640 , 3 )
75
+ img1 = np .swapaxes (img1 , 0 , 2 )
76
+ #these are, what is it, normalizsed?
77
+ else :
78
+ print "Frames are of size: " ,img1 .size
79
+ print img1 .shape
80
+ plt .imshow (img1 )
81
+
82
+ plt .show ()
83
+
84
+ openni2 .initialize () # can also accept the path of the OpenNI redistribution
85
+
86
+ dev = openni2 .Device .open_any ()
87
+ print dev .get_sensor_info (openni2 .SENSOR_DEPTH )
88
+
89
+ print "testing depth "
90
+ depth_stream = dev .create_depth_stream ()
91
+ '''depth_stream.set_video_mode(c_api.OniVideoMode(pixelFormat = c_api.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_1_MM, resolutionX = 640, resolutionY = 480, fps = 30))
92
+ depth_stream.start()
93
+ frame = depth_stream.read_frame()
94
+ frame_data = frame.get_buffer_as_uint16()
95
+ print_frame(frame_data, np.uint16)
96
+ depth_stream.stop()'''
97
+
98
+ '''print "Testing depth 2 "
99
+ depth_stream.set_video_mode(c_api.OniVideoMode(pixelFormat = c_api.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_100_UM, resolutionX = 640, resolutionY = 480, fps = 30))
100
+ depth_stream.start()
101
+ frame = depth_stream.read_frame()
102
+ frame_data = frame.get_buffer_as_uint16()
103
+ print_frame(frame_data, np.uint16)
104
+ depth_stream.stop()'''
105
+
106
+ print "Testing Color "
107
+ color_stream = dev .create_color_stream ()
108
+ '''color_stream.set_video_mode(c_api.OniVideoMode(pixelFormat = c_api.OniPixelFormat.ONI_PIXEL_FORMAT_RGB888, resolutionX = 640, resolutionY = 480, fps = 30))
109
+ color_stream.start()
110
+ frame = color_stream.read_frame()
111
+ frame_data1 = frame.get_buffer_as_uint8()
112
+ print_frame(frame_data1, np.uint8)
113
+ color_stream.stop()'''
114
+
115
+ print "what?"
116
+ depth_stream .set_video_mode (c_api .OniVideoMode (pixelFormat = c_api .OniPixelFormat .ONI_PIXEL_FORMAT_DEPTH_100_UM , resolutionX = 640 , resolutionY = 480 , fps = 30 ))
117
+ color_stream .set_video_mode (c_api .OniVideoMode (pixelFormat = c_api .OniPixelFormat .ONI_PIXEL_FORMAT_RGB888 , resolutionX = 640 , resolutionY = 480 , fps = 30 ))
118
+ print "Starting Streams"
119
+ depth_stream .start ()
120
+ color_stream .start ()
121
+ print "Read frames"
122
+ frame = depth_stream .read_frame ()
123
+ frame1 = color_stream .read_frame ()
124
+ print "Getting Buffer"
125
+ frame_data = frame .get_buffer_as_uint16 ()
126
+ frame_data1 = frame1 .get_buffer_as_uint8 ()
127
+ print "Printing"
128
+ print_frames (frame_data , frame_data1 , np .uint16 , np .uint8 )
129
+ depth_stream .stop ()
130
+ color_stream .stop ()
131
+
132
+ openni2 .unload ()
0 commit comments