-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPtGrey.py
288 lines (245 loc) · 13.5 KB
/
PtGrey.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
def runPySpinCam(cam_id, _mode=0):
global height, width, image_converted, cap_fps
system = PtGrey.System.GetInstance()
cam_list = system.GetCameras()
num_cameras = cam_list.GetSize()
print("Number of cameras detected: {:d}".format(num_cameras))
if num_cameras == 0:
cam_list.Clear()
system.ReleaseInstance()
raise IOError("Not enough cameras!")
cam = cam_list.GetByIndex(cam_id)
try:
nodemap_tldevice = cam.GetTLDeviceNodeMap()
try:
node_device_information = PtGrey.CCategoryPtr(nodemap_tldevice.GetNode("DeviceInformation"))
if PtGrey.IsAvailable(node_device_information) and PtGrey.IsReadable(node_device_information):
features = node_device_information.GetFeatures()
for feature in features:
node_feature = PtGrey.CValuePtr(feature)
print("%s: %s" % (node_feature.GetName(),
node_feature.ToString() if PtGrey.IsReadable(node_feature) else
"Node not readable"))
else:
print("Device control information not available.")
except PtGrey.SpinnakerException as ex:
raise IOError("Error in getting device info: %s" % ex)
cam.Init()
nodemap = cam.GetNodeMap()
if rgb_mode == 1:
pix_format_txt = "RGB8Packed"
elif rgb_mode == 2:
pix_format_txt = "BayerRG8"
else:
pix_format_txt = "Mono8"
pixel_format_mode = PtGrey.CEnumerationPtr(nodemap.GetNode("PixelFormat"))
if not PtGrey.IsAvailable(pixel_format_mode) or not PtGrey.IsWritable(pixel_format_mode):
raise IOError("Unable to set pixel format mode to RGB (enum retrieval). Aborting...")
node_pixel_format_mode_rgb8 = pixel_format_mode.GetEntryByName(pix_format_txt)
if not PtGrey.IsAvailable(node_pixel_format_mode_rgb8) or not PtGrey.IsReadable(
node_pixel_format_mode_rgb8):
raise IOError("Unable to set pixel format mode to RGB (entry retrieval). Aborting...")
pixel_format_mode.SetIntValue(node_pixel_format_mode_rgb8.GetValue())
print("pixel format mode set to {:s}...".format(pix_format_txt))
video_mode_txt = 'Mode{:d}'.format(video_mode)
video_mode_node = PtGrey.CEnumerationPtr(nodemap.GetNode("VideoMode"))
if not PtGrey.IsAvailable(video_mode_node) or not PtGrey.IsWritable(video_mode_node):
raise IOError("Unable to set video mode to {} (enum retrieval). Aborting...".format(video_mode_txt))
node_video_mode_node = video_mode_node.GetEntryByName(video_mode_txt)
if not PtGrey.IsAvailable(node_video_mode_node) or not PtGrey.IsReadable(node_video_mode_node):
raise IOError("Unable to set video mode to {} (entry retrieval). Aborting...".format(video_mode_txt))
video_mode_node.SetIntValue(node_video_mode_node.GetValue())
print("video mode set to {:s}...".format(video_mode_txt))
node_acquisition_mode = PtGrey.CEnumerationPtr(nodemap.GetNode("AcquisitionMode"))
if not PtGrey.IsAvailable(node_acquisition_mode) or not PtGrey.IsWritable(node_acquisition_mode):
raise IOError(
"Unable to set acquisition mode to continuous (enum retrieval). Aborting...")
node_acquisition_mode_continuous = node_acquisition_mode.GetEntryByName("Continuous")
if not PtGrey.IsAvailable(node_acquisition_mode_continuous) or not PtGrey.IsReadable(
node_acquisition_mode_continuous):
raise IOError("Unable to set acquisition mode to continuous (entry retrieval). Aborting...")
acquisition_mode_continuous = node_acquisition_mode_continuous.GetValue()
node_acquisition_mode.SetIntValue(acquisition_mode_continuous)
print("acquisition mode set to continuous...")
cam.BeginAcquisition()
# get first image
while True:
try:
# print('Getting the first image')
image_result = cam.GetNextImage()
if image_result.IsIncomplete():
print("Image incomplete with image status %d ..." % image_result.GetImageStatus())
continue
width = image_result.GetWidth()
height = image_result.GetHeight()
image_converted = image_result
# if rgb_mode == 2:
# image_converted = image_result.Convert(PtGrey.PixelFormat_RGB8Packed, PtGrey.HQ_LINEAR)
# else:
# image_converted = image_result
# image_result.Release()
break
except PtGrey.SpinnakerException as ex:
raise IOError("Error in acquiring image: %s" % ex)
while True:
if stop_pt_grey_cam:
break
try:
cap_start_t = time.time()
image_result = cam.GetNextImage()
if image_result.IsIncomplete():
print("Image incomplete with image status %d ..." % image_result.GetImageStatus())
continue
width = image_result.GetWidth()
height = image_result.GetHeight()
cap_end_t = time.time()
cap_fps = 1.0 / float(cap_end_t - cap_start_t)
with ptgrey_mutex:
# if rgb_mode == 2:
# image_converted = image_result.Convert(PtGrey.PixelFormat_RGB8Packed, PtGrey.HQ_LINEAR)
# else:
# image_converted = image_result
image_converted = image_result
# cap_end_t2 = time.time()
# cap_fps2 = 1.0 / float(cap_end_t2 - cap_start_t)
if _mode == 1:
image_np_gray = np.array(image_converted.GetData(), dtype=np.uint8).reshape(
(height, width)).copy()
image_np = cv2.cvtColor(image_np_gray, cv2.COLOR_GRAY2RGB)
cv2.imshow(win_title, image_np)
k = cv2.waitKey(1)
if k == ord('q') or k == 27:
break
# image_result.Release()
except PtGrey.SpinnakerException as ex:
raise IOError("Error in acquiring image: %s" % ex)
except PtGrey.SpinnakerException as ex:
raise IOError("Error: %s" % ex)
cam.EndAcquisition()
cam.DeInit()
del cam
cam_list.Clear()
system.ReleaseInstance()
def runPySpinCam(cam_id, _mode=0):
global height, width, image_converted, cap_fps
system = PtGrey.System.GetInstance()
cam_list = system.GetCameras()
num_cameras = cam_list.GetSize()
print("Number of cameras detected: {:d}".format(num_cameras))
if num_cameras == 0:
cam_list.Clear()
system.ReleaseInstance()
raise IOError("Not enough cameras!")
cam = cam_list.GetByIndex(cam_id)
try:
nodemap_tldevice = cam.GetTLDeviceNodeMap()
try:
node_device_information = PtGrey.CCategoryPtr(nodemap_tldevice.GetNode("DeviceInformation"))
if PtGrey.IsAvailable(node_device_information) and PtGrey.IsReadable(node_device_information):
features = node_device_information.GetFeatures()
for feature in features:
node_feature = PtGrey.CValuePtr(feature)
print("%s: %s" % (node_feature.GetName(),
node_feature.ToString() if PtGrey.IsReadable(node_feature) else
"Node not readable"))
else:
print("Device control information not available.")
except PtGrey.SpinnakerException as ex:
raise IOError("Error in getting device info: %s" % ex)
cam.Init()
nodemap = cam.GetNodeMap()
if rgb_mode == 1:
pix_format_txt = "RGB8Packed"
elif rgb_mode == 2:
pix_format_txt = "BayerRG8"
else:
pix_format_txt = "Mono8"
pixel_format_mode = PtGrey.CEnumerationPtr(nodemap.GetNode("PixelFormat"))
if not PtGrey.IsAvailable(pixel_format_mode) or not PtGrey.IsWritable(pixel_format_mode):
raise IOError("Unable to set pixel format mode to RGB (enum retrieval). Aborting...")
node_pixel_format_mode_rgb8 = pixel_format_mode.GetEntryByName(pix_format_txt)
if not PtGrey.IsAvailable(node_pixel_format_mode_rgb8) or not PtGrey.IsReadable(
node_pixel_format_mode_rgb8):
raise IOError("Unable to set pixel format mode to RGB (entry retrieval). Aborting...")
pixel_format_mode.SetIntValue(node_pixel_format_mode_rgb8.GetValue())
print("pixel format mode set to {:s}...".format(pix_format_txt))
video_mode_txt = 'Mode{:d}'.format(video_mode)
video_mode_node = PtGrey.CEnumerationPtr(nodemap.GetNode("VideoMode"))
if not PtGrey.IsAvailable(video_mode_node) or not PtGrey.IsWritable(video_mode_node):
raise IOError("Unable to set video mode to {} (enum retrieval). Aborting...".format(video_mode_txt))
node_video_mode_node = video_mode_node.GetEntryByName(video_mode_txt)
if not PtGrey.IsAvailable(node_video_mode_node) or not PtGrey.IsReadable(node_video_mode_node):
raise IOError(
"Unable to set video mode to {} (entry retrieval). Aborting...".format(video_mode_txt))
video_mode_node.SetIntValue(node_video_mode_node.GetValue())
print("video mode set to {:s}...".format(video_mode_txt))
node_acquisition_mode = PtGrey.CEnumerationPtr(nodemap.GetNode("AcquisitionMode"))
if not PtGrey.IsAvailable(node_acquisition_mode) or not PtGrey.IsWritable(node_acquisition_mode):
raise IOError(
"Unable to set acquisition mode to continuous (enum retrieval). Aborting...")
node_acquisition_mode_continuous = node_acquisition_mode.GetEntryByName("Continuous")
if not PtGrey.IsAvailable(node_acquisition_mode_continuous) or not PtGrey.IsReadable(
node_acquisition_mode_continuous):
raise IOError("Unable to set acquisition mode to continuous (entry retrieval). Aborting...")
acquisition_mode_continuous = node_acquisition_mode_continuous.GetValue()
node_acquisition_mode.SetIntValue(acquisition_mode_continuous)
print("acquisition mode set to continuous...")
cam.BeginAcquisition()
# get first image
while True:
try:
# print('Getting the first image')
image_result = cam.GetNextImage()
if image_result.IsIncomplete():
print("Image incomplete with image status %d ..." % image_result.GetImageStatus())
continue
width = image_result.GetWidth()
height = image_result.GetHeight()
image_converted = image_result
# if rgb_mode == 2:
# image_converted = image_result.Convert(PtGrey.PixelFormat_RGB8Packed, PtGrey.HQ_LINEAR)
# else:
# image_converted = image_result
# image_result.Release()
break
except PtGrey.SpinnakerException as ex:
raise IOError("Error in acquiring image: %s" % ex)
while True:
if stop_pt_grey_cam:
break
try:
cap_start_t = time.time()
image_result = cam.GetNextImage()
if image_result.IsIncomplete():
print("Image incomplete with image status %d ..." % image_result.GetImageStatus())
continue
width = image_result.GetWidth()
height = image_result.GetHeight()
cap_end_t = time.time()
cap_fps = 1.0 / float(cap_end_t - cap_start_t)
with ptgrey_mutex:
# if rgb_mode == 2:
# image_converted = image_result.Convert(PtGrey.PixelFormat_RGB8Packed, PtGrey.HQ_LINEAR)
# else:
# image_converted = image_result
image_converted = image_result
# cap_end_t2 = time.time()
# cap_fps2 = 1.0 / float(cap_end_t2 - cap_start_t)
if _mode == 1:
image_np_gray = np.array(image_converted.GetData(), dtype=np.uint8).reshape(
(height, width)).copy()
image_np = cv2.cvtColor(image_np_gray, cv2.COLOR_GRAY2RGB)
cv2.imshow(win_title, image_np)
k = cv2.waitKey(1)
if k == ord('q') or k == 27:
break
# image_result.Release()
except PtGrey.SpinnakerException as ex:
raise IOError("Error in acquiring image: %s" % ex)
except PtGrey.SpinnakerException as ex:
raise IOError("Error: %s" % ex)
cam.EndAcquisition()
cam.DeInit()
del cam
cam_list.Clear()
system.ReleaseInstance()