-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trigger.py
505 lines (393 loc) · 18.6 KB
/
Trigger.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# coding=utf-8
# =============================================================================
# Copyright (c) 2001-2021 FLIR Systems, Inc. All Rights Reserved.
#
# This software is the confidential and proprietary information of FLIR
# Integrated Imaging Solutions, Inc. ("Confidential Information"). You
# shall not disclose such Confidential Information and shall use it only in
# accordance with the terms of the license agreement you entered into
# with FLIR Integrated Imaging Solutions, Inc. (FLIR).
#
# FLIR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
# SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE, OR NON-INFRINGEMENT. FLIR SHALL NOT BE LIABLE FOR ANY DAMAGES
# SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
# THIS SOFTWARE OR ITS DERIVATIVES.
# =============================================================================
#
# Trigger.py shows how to trigger the camera. It relies on information
# provided in the Enumeration, Acquisition, and NodeMapInfo examples.
#
# It can also be helpful to familiarize yourself with the ImageFormatControl
# and Exposure examples. As they are somewhat shorter and simpler, either
# provides a strong introduction to camera customization.
#
# This example shows the process of configuring, using, and cleaning up a
# camera for use with both a software and a hardware trigger.
import json
import os
import time
import PySpin
import sys
import asyncio
import CameraNexus as Nexus
NUM_IMAGES = 2 # number of images to grab
class TriggerType:
SOFTWARE = 1
HARDWARE = 2
CHOSEN_TRIGGER = TriggerType.SOFTWARE
def configure_trigger(cam):
"""
This function configures the camera to use a trigger. First, trigger mode is
set to off in order to select the trigger source. Once the trigger source
has been selected, trigger mode is then enabled, which has the camera
capture only a single image upon the execution of the chosen trigger.
:param cam: Camera to configure trigger for.
:type cam: CameraPtr
:return: True if successful, False otherwise.
:rtype: bool
"""
result = True
print('*** CONFIGURING TRIGGER ***\n')
print('Note that if the application / user software triggers faster than frame time, the trigger may be dropped / skipped by the camera.\n')
print('If several frames are needed per trigger, a more reliable alternative for such case, is to use the multi-frame mode.\n\n')
if CHOSEN_TRIGGER == TriggerType.SOFTWARE:
print('Software trigger chosen ...')
elif CHOSEN_TRIGGER == TriggerType.HARDWARE:
print('Hardware trigger chose ...')
try:
# Ensure trigger mode off
# The trigger must be disabled in order to configure whether the source
# is software or hardware.
nodemap = cam.GetNodeMap()
node_trigger_mode = PySpin.CEnumerationPtr(nodemap.GetNode('TriggerMode'))
if not PySpin.IsAvailable(node_trigger_mode) or not PySpin.IsReadable(node_trigger_mode):
print('Unable to disable trigger mode (node retrieval). Aborting...')
return False
node_trigger_mode_off = node_trigger_mode.GetEntryByName('Off')
if not PySpin.IsAvailable(node_trigger_mode_off) or not PySpin.IsReadable(node_trigger_mode_off):
print('Unable to disable trigger mode (enum entry retrieval). Aborting...')
return False
node_trigger_mode.SetIntValue(node_trigger_mode_off.GetValue())
print('Trigger mode disabled...')
# Set TriggerSelector to FrameStart
# For this example, the trigger selector should be set to frame start.
# This is the default for most cameras.
node_trigger_selector= PySpin.CEnumerationPtr(nodemap.GetNode('TriggerSelector'))
if not PySpin.IsAvailable(node_trigger_selector) or not PySpin.IsWritable(node_trigger_selector):
print('Unable to get trigger selector (node retrieval). Aborting...')
return False
node_trigger_selector_framestart = node_trigger_selector.GetEntryByName('FrameStart')
if not PySpin.IsAvailable(node_trigger_selector_framestart) or not PySpin.IsReadable(
node_trigger_selector_framestart):
print('Unable to set trigger selector (enum entry retrieval). Aborting...')
return False
node_trigger_selector.SetIntValue(node_trigger_selector_framestart.GetValue())
print('Trigger selector set to frame start...')
# Select trigger source
# The trigger source must be set to hardware or software while trigger
# mode is off.
node_trigger_source = PySpin.CEnumerationPtr(nodemap.GetNode('TriggerSource'))
if not PySpin.IsAvailable(node_trigger_source) or not PySpin.IsWritable(node_trigger_source):
print('Unable to get trigger source (node retrieval). Aborting...')
return False
if CHOSEN_TRIGGER == TriggerType.SOFTWARE:
node_trigger_source_software = node_trigger_source.GetEntryByName('Software')
if not PySpin.IsAvailable(node_trigger_source_software) or not PySpin.IsReadable(
node_trigger_source_software):
print('Unable to set trigger source (enum entry retrieval). Aborting...')
return False
node_trigger_source.SetIntValue(node_trigger_source_software.GetValue())
print('Trigger source set to software...')
elif CHOSEN_TRIGGER == TriggerType.HARDWARE:
node_trigger_source_hardware = node_trigger_source.GetEntryByName('Line0')
if not PySpin.IsAvailable(node_trigger_source_hardware) or not PySpin.IsReadable(
node_trigger_source_hardware):
print('Unable to set trigger source (enum entry retrieval). Aborting...')
return False
node_trigger_source.SetIntValue(node_trigger_source_hardware.GetValue())
print('Trigger source set to hardware...')
# Turn trigger mode on
# Once the appropriate trigger source has been set, turn trigger mode
# on in order to retrieve images using the trigger.
node_trigger_mode_on = node_trigger_mode.GetEntryByName('On')
if not PySpin.IsAvailable(node_trigger_mode_on) or not PySpin.IsReadable(node_trigger_mode_on):
print('Unable to enable trigger mode (enum entry retrieval). Aborting...')
return False
node_trigger_mode.SetIntValue(node_trigger_mode_on.GetValue())
print('Trigger mode turned back on...')
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
return result
def grab_next_image_by_trigger(nodemap, cam):
"""
This function acquires an image by executing the trigger node.
:param cam: Camera to acquire images from.
:param nodemap: Device nodemap.
:type cam: CameraPtr
:type nodemap: INodeMap
:return: True if successful, False otherwise.
:rtype: bool
"""
try:
result = True
# Use trigger to capture image
# The software trigger only feigns being executed by the Enter key;
# what might not be immediately apparent is that there is not a
# continuous stream of images being captured; in other examples that
# acquire images, the camera captures a continuous stream of images.
# When an image is retrieved, it is plucked from the stream.
if CHOSEN_TRIGGER == TriggerType.SOFTWARE:
# Execute software trigger
node_softwaretrigger_cmd = PySpin.CCommandPtr(nodemap.GetNode('TriggerSoftware'))
if not PySpin.IsAvailable(node_softwaretrigger_cmd) or not PySpin.IsWritable(node_softwaretrigger_cmd):
print('Unable to execute trigger. Aborting...')
return False
node_softwaretrigger_cmd.Execute()
# TODO: Blackfly and Flea3 GEV cameras need 2 second delay after software trigger
elif CHOSEN_TRIGGER == TriggerType.HARDWARE:
print('Use the hardware to trigger image acquisition.')
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
return result
# # From Spinnaker SDK : Examples : (copyright) FLIR
def acquire_images(cam, nodemap, nodemap_tldevice):
"""
This function acquires and saves 2 images from a device.
Please see Acquisition example for more in-depth comments on acquiring images.
:param cam: Camera to acquire images from.
:param nodemap: Device nodemap.
:param nodemap_tldevice: Transport layer device nodemap.
:type cam: CameraPtr
:type nodemap: INodeMap
:type nodemap_tldevice: INodeMap
:return: True if successful, False otherwise.
:rtype: bool
"""
# Configurs the text file for image numbering
Accuracy = open("Accuracy.json", "r")
load = json.load(Accuracy)
image_num_config = int(load["NumPhotos"])
Accuracy.close()
Accuracy = open("Accuracy.json", "w")
load["NumPhotos"] = int(load["NumPhotos"]) + NUM_IMAGES
json.dump(load, Accuracy)
print('*** IMAGE ACQUISITION ***\n')
try:
result = True
# Set acquisition mode to continuous
# In order to access the node entries, they have to be casted to a pointer type (CEnumerationPtr here)
node_acquisition_mode = PySpin.CEnumerationPtr(nodemap.GetNode('AcquisitionMode'))
if not PySpin.IsAvailable(node_acquisition_mode) or not PySpin.IsWritable(node_acquisition_mode):
return False
# Retrieve entry node from enumeration node
node_acquisition_mode_continuous = node_acquisition_mode.GetEntryByName('Continuous')
if not PySpin.IsAvailable(node_acquisition_mode_continuous) or not PySpin.IsReadable(
node_acquisition_mode_continuous):
print('Unable to set acquisition mode to continuous (entry retrieval). Aborting...')
return False
node_file_format = PySpin.IEnumerationPtr('PixelFormat')
if not PySpin.IsAvailable(node_file_format) or not PySpin.IsReadable(
node_file_format):
return False
# Retrieve integer value from entry node
acquisition_mode_continuous = node_acquisition_mode_continuous.GetValue()
# Set integer value from entry node as new value of enumeration node
node_acquisition_mode.SetIntValue(acquisition_mode_continuous)
# print('Acquisition mode set to continuous...')
# Begin acquiring images
cam.BeginAcquisition()
print('Acquiring images...')
device_serial_number = ''
node_device_serial_number = PySpin.CStringPtr(nodemap_tldevice.GetNode('DeviceSerialNumber'))
if PySpin.IsAvailable(node_device_serial_number) and PySpin.IsReadable(node_device_serial_number):
device_serial_number = node_device_serial_number.GetValue()
# print('Device serial number retrieved as %s...' % device_serial_number)
# Retrieve, convert, and save images
for i in range(NUM_IMAGES):
try:
# Retrieve the next image from the trigger
result &= grab_next_image_by_trigger(nodemap)
# Retrieve next received image
image_result = cam.GetNextImage(1000)
# Ensure image completion
if image_result.IsIncomplete():
print('Image incomplete with image status %d ...' % image_result.GetImageStatus())
else:
# Create a unique filename
if device_serial_number:
filename = 'sample-%s-%d-%d.png' % (device_serial_number, i +
image_num_config, Nexus.GetCameraTemperature(cam))
else: # if serial number is empty
filename = 'sample-%d.png' % i + str(image_num_config)
# Save image
# *** NOTES ***
# The standard practice of the examples is to use device
# serial numbers to keep images of one device from
# overwriting those of another.
image_result.Save(filename)
print('Image saved at %s\n' % filename)
# Release image
#
# *** NOTES ***
# Images retrieved directly from the camera (i.e. non-converted
# images) need to be released in order to keep from filling the
# buffer.
image_result.Release()
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
time.sleep(2)
# End acquisition
#
# *** NOTES ***
# Ending acquisition appropriately helps ensure that devices clean up
# properly and do not need to be power-cycled to maintain integrity.
cam.EndAcquisition()
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
return result
def reset_trigger(nodemap):
"""
This function returns the camera to a normal state by turning off trigger mode.
:param nodemap: Transport layer device nodemap.
:type nodemap: INodeMap
:returns: True if successful, False otherwise.
:rtype: bool
"""
try:
result = True
node_trigger_mode = PySpin.CEnumerationPtr(nodemap.GetNode('TriggerMode'))
if not PySpin.IsAvailable(node_trigger_mode) or not PySpin.IsReadable(node_trigger_mode):
print('Unable to disable trigger mode (node retrieval). Aborting...')
return False
node_trigger_mode_off = node_trigger_mode.GetEntryByName('Off')
if not PySpin.IsAvailable(node_trigger_mode_off) or not PySpin.IsReadable(node_trigger_mode_off):
print('Unable to disable trigger mode (enum entry retrieval). Aborting...')
return False
node_trigger_mode.SetIntValue(node_trigger_mode_off.GetValue())
print('Trigger mode disabled...')
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
result = False
return result
def print_device_info(nodemap):
"""
This function prints the device information of the camera from the transport
layer; please see NodeMapInfo example for more in-depth comments on printing
device information from the nodemap.
:param nodemap: Transport layer device nodemap.
:type nodemap: INodeMap
:returns: True if successful, False otherwise.
:rtype: bool
"""
print('*** DEVICE INFORMATION ***\n')
try:
result = True
node_device_information = PySpin.CCategoryPtr(nodemap.GetNode('DeviceInformation'))
if PySpin.IsAvailable(node_device_information) and PySpin.IsReadable(node_device_information):
features = node_device_information.GetFeatures()
for feature in features:
node_feature = PySpin.CValuePtr(feature)
print('%s: %s' % (node_feature.GetName(),
node_feature.ToString() if PySpin.IsReadable(node_feature) else 'Node not readable'))
else:
print('Device control information not available.')
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
return result
def run_single_camera(cam):
"""
This function acts as the body of the example; please see NodeMapInfo example
for more in-depth comments on setting up cameras.
:param cam: Camera to run on.
:type cam: CameraPtr
:return: True if successful, False otherwise.
:rtype: bool
"""
try:
result = True
err = False
# Retrieve TL device nodemap and print device information
nodemap_tldevice = cam.GetTLDeviceNodeMap()
result &= print_device_info(nodemap_tldevice)
# Initialize camera
cam.Init()
# Retrieve GenICam nodemap
nodemap = cam.GetNodeMap()
# Configure trigger
if configure_trigger(cam) is False:
return False
# Acquire images
result &= acquire_images(cam, nodemap, nodemap_tldevice)
# Reset trigger
result &= reset_trigger(nodemap)
# Deinitialize camera
cam.DeInit()
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
result = False
return result
def main():
"""
Example entry point; please see Enumeration example for more in-depth
comments on preparing and cleaning up the system.
:return: True if successful, False otherwise.
:rtype: bool
"""
# Since this application saves images in the current folder
# we must ensure that we have permission to write to this folder.
# If we do not have permission, fail right away.
try:
test_file = open('test.txt', 'w+')
except IOError:
print('Unable to write to current directory. Please check permissions.')
input('Press Enter to exit...')
return False
test_file.close()
os.remove(test_file.name)
result = True
# Retrieve singleton reference to system object
system = PySpin.System.GetInstance()
# Get current library version
version = system.GetLibraryVersion()
print('Library version: %d.%d.%d.%d' % (version.major, version.minor, version.type, version.build))
# Retrieve list of cameras from the system
cam_list = system.GetCameras()
num_cameras = cam_list.GetSize()
print('Number of cameras detected: %d' % num_cameras)
# Finish if there are no cameras
if num_cameras == 0:
# Clear camera list before releasing system
cam_list.Clear()
# Release system instance
system.ReleaseInstance()
print('Not enough cameras!')
input('Done! Press Enter to exit...')
return False
# Run example on each camera
for i, cam in enumerate(cam_list):
print('Running example for camera %d...' % i)
result &= run_single_camera(cam)
print('Camera %d example complete... \n' % i)
# Release reference to camera
# NOTE: Unlike the C++ examples, we cannot rely on pointer objects being automatically
# cleaned up when going out of scope.
# The usage of del is preferred to assigning the variable to None.
del cam
# Clear camera list before releasing system
cam_list.Clear()
# Release system instance
system.ReleaseInstance()
input('Done! Press Enter to exit...')
return result
if __name__ == '__main__':
if main():
sys.exit(0)
else:
sys.exit(1)