-
Notifications
You must be signed in to change notification settings - Fork 0
/
Acquisition.py
328 lines (253 loc) · 11.9 KB
/
Acquisition.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
# coding=utf-8
# =============================================================================
# Copyright (c) 2001-2023 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.
# =============================================================================
#
# Acquisition.py shows how to acquire images. It relies on
# information provided in the Enumeration example. Also, check out
# the ExceptionHandling and NodeMapInfo examples if you haven't already.
# ExceptionHandling shows the handling of standard and Spinnaker exceptions
# while NodeMapInfo explores retrieving information from various node types.
#
# This example touches on the preparation and cleanup of a camera just before
# and just after the acquisition of images. Image retrieval and conversion,
# grabbing image data, and saving images are all covered as well.
#
# Once comfortable with Acquisition, we suggest checking out
# AcquisitionMultipleCamera, NodeMapCallback, or SaveToAvi.
# AcquisitionMultipleCamera demonstrates simultaneously acquiring images from
# a number of cameras, NodeMapCallback serves as a good introduction to
# programming with callbacks and events, and SaveToAvi exhibits video creation.
#
# Please leave us feedback at: https://www.surveymonkey.com/r/TDYMVAPI
# More source code examples at: https://github.com/Teledyne-MV/Spinnaker-Examples
# Need help? Check out our forum at: https://teledynevisionsolutions.zendesk.com/hc/en-us/community/topics
import os
import PySpin
import sys
import Constantes
class StreamMode:
STREAM_MODE_TELEDYNE_GIGE_VISION = 0
STREAM_MODE_PGRLWF = 1
STREAM_MODE_SOCKET = 2
CHOSEN_STREAMMODE = StreamMode.STREAM_MODE_TELEDYNE_GIGE_VISION
NUM_IMAGES = 1
def set_stream_mode(cam):
streamMode = "TeledyneGigeVision" if CHOSEN_STREAMMODE == StreamMode.STREAM_MODE_TELEDYNE_GIGE_VISION else "LWF" if CHOSEN_STREAMMODE == StreamMode.STREAM_MODE_PGRLWF else "Socket"
result = True
nodemap_tlstream = cam.GetTLStreamNodeMap()
node_stream_mode = PySpin.CEnumerationPtr(nodemap_tlstream.GetNode('StreamMode'))
if not PySpin.IsReadable(node_stream_mode) or not PySpin.IsWritable(node_stream_mode):
return True
node_stream_mode_custom = PySpin.CEnumEntryPtr(node_stream_mode.GetEntryByName(streamMode))
if not PySpin.IsReadable(node_stream_mode_custom):
print('Stream mode ' + streamMode + ' not available. Aborting...')
return False
stream_mode_custom = node_stream_mode_custom.GetValue()
node_stream_mode.SetIntValue(stream_mode_custom)
print('Stream Mode set to %s...' % node_stream_mode.GetCurrentEntry().GetSymbolic())
return result
def print_pixel_formats(nodemap):
print('*** PIXEL FORMATS ***\n')
try:
node_pixel_formats = PySpin.CCategoryPtr(nodemap.GetNode('PixelFormat'))
if PySpin.IsReadable(node_pixel_formats):
entries = node_pixel_formats.GetEntries()
for entry in entries:
node_entry = PySpin.CEnumEntryPtr(entry)
if PySpin.IsReadable(node_entry):
print('Pixel Format: %s' % node_entry.GetSymbolic())
else:
print('Pixel format information not readable.')
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
def set_pixel_format(cam, format_name):
try:
nodemap = cam.GetNodeMap()
node_pixel_format = PySpin.CEnumerationPtr(nodemap.GetNode('PixelFormat'))
if not PySpin.IsReadable(node_pixel_format) or not PySpin.IsWritable(node_pixel_format):
print('Unable to access pixel format node. Aborting...')
return False
node_pixel_format_entry = PySpin.CEnumEntryPtr(node_pixel_format.GetEntryByName(format_name))
if not PySpin.IsReadable(node_pixel_format_entry):
print('Pixel format %s not available. Aborting...' % format_name)
return False
pixel_format_value = node_pixel_format_entry.GetValue()
node_pixel_format.SetIntValue(pixel_format_value)
print('Pixel Format set to %s...' % format_name)
return True
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
def set_exposure(cam, exposure_time_us):
try:
nodemap = cam.GetNodeMap()
node_exposure_time = PySpin.CFloatPtr(nodemap.GetNode('ExposureTime'))
if PySpin.IsReadable(node_exposure_time) and PySpin.IsWritable(node_exposure_time):
node_exposure_time.SetValue(exposure_time_us)
print(f'Exposure time set to {exposure_time_us} us...')
return True
else:
print('Unable to set exposure time.')
return False
except PySpin.SpinnakerException as ex:
print(f'Error: {ex}')
return False
def set_brightness(cam, brightness_value):
try:
nodemap = cam.GetNodeMap()
node_brightness = PySpin.CIntegerPtr(nodemap.GetNode('Brightness'))
if PySpin.IsReadable(node_brightness) and PySpin.IsWritable(node_brightness):
node_brightness.SetValue(brightness_value)
print(f'Brightness set to {brightness_value}...')
return True
else:
print('Unable to set brightness.')
return False
except PySpin.SpinnakerException as ex:
print(f'Error: {ex}')
return False
def acquire_images(cam, nodemap, nodemap_tldevice):
print('*** IMAGE ACQUISITION ***\n')
try:
result = True
node_acquisition_mode = PySpin.CEnumerationPtr(nodemap.GetNode('AcquisitionMode'))
if not PySpin.IsReadable(node_acquisition_mode) or not PySpin.IsWritable(node_acquisition_mode):
print('Unable to set acquisition mode to continuous (enum retrieval). Aborting...')
return False
node_acquisition_mode_continuous = PySpin.CEnumEntryPtr(node_acquisition_mode.GetEntryByName('Continuous'))
if not PySpin.IsReadable(node_acquisition_mode_continuous):
print('Unable to set acquisition mode to continuous (entry retrieval). Aborting...')
return False
acquisition_mode_continuous = node_acquisition_mode_continuous.GetValue()
node_acquisition_mode.SetIntValue(acquisition_mode_continuous)
print('Acquisition Mode set to Continuous...')
cam.BeginAcquisition()
device_serial_number = ''
node_device_serial_number = PySpin.CStringPtr(nodemap_tldevice.GetNode('DeviceSerialNumber'))
if PySpin.IsReadable(node_device_serial_number):
device_serial_number = node_device_serial_number.GetValue()
print('Device serial number retrieved as %s...' % device_serial_number)
processor = PySpin.ImageProcessor()
processor.SetColorProcessing(PySpin.SPINNAKER_COLOR_PROCESSING_ALGORITHM_HQ_LINEAR)
for i in range(NUM_IMAGES):
try:
image_result = cam.GetNextImage()
if image_result.IsIncomplete():
print('Image incomplete with image status %d ...' % image_result.GetImageStatus())
else:
width = image_result.GetWidth()
height = image_result.GetHeight()
print('Grabbed image with width = %d, height = %d' % (width, height))
image_converted = processor.Convert(image_result, PySpin.PixelFormat_RGB8)
# Define the path and filename
if device_serial_number:
filename = os.path.join(Constantes.image_path, 'Acquisition-%s-%d.jpg' % (device_serial_number, i))
else:
filename = os.path.join(Constantes.image_path, 'Acquisition-%d.jpg' % i)
# Ensure the save directory exists
if not os.path.exists(Constantes.image_path):
os.makedirs(Constantes.image_path)
image_converted.Save(filename)
print('Image saved at %s' % filename)
image_result.Release()
print('')
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
cam.EndAcquisition()
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
return result
def print_device_info(nodemap):
print('*** DEVICE INFORMATION ***\n')
try:
result = True
node_device_information = PySpin.CCategoryPtr(nodemap.GetNode('DeviceInformation'))
if 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 readable.')
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
return result
def run_single_camera(cam):
try:
result = True
print("Inicializando cámara...")
nodemap_tldevice = cam.GetTLDeviceNodeMap()
result &= print_device_info(nodemap_tldevice)
print("Configurando cámara...")
cam.Init()
nodemap = cam.GetNodeMap()
result &= set_stream_mode(cam)
print_pixel_formats(nodemap)
result &= set_pixel_format(cam, 'RGB8')
result &= set_exposure(cam, 20000)
result &= set_brightness(cam, 128) # Adjust brightness if needed
print("Adquiriendo imágenes...")
result &= acquire_images(cam, nodemap, nodemap_tldevice)
print("Desinicializando cámara...")
cam.DeInit()
except PySpin.SpinnakerException as ex:
print('Error en la cámara: %s' % ex)
result = False
return 0
def main():
try:
test_file = open('test.txt', 'w+')
except IOError:
print('No se puede escribir en el directorio actual. Verifica permisos.')
input('Presiona Enter para salir...')
return False
test_file.close()
os.remove(test_file.name)
result = True
try:
system = PySpin.System.GetInstance()
version = system.GetLibraryVersion()
print('Versión de la biblioteca: %d.%d.%d.%d' % (version.major, version.minor, version.type, version.build))
cam_list = system.GetCameras()
num_cameras = cam_list.GetSize()
print('Número de cámaras detectadas: %d' % num_cameras)
if num_cameras == 0:
cam_list.Clear()
system.ReleaseInstance()
print('¡No se encontraron cámaras!')
input('¡Listo! Presiona Enter para salir...')
return False
for i, cam in enumerate(cam_list):
print('Ejecutando ejemplo para la cámara %d...' % i)
result &= run_single_camera(cam)
print('Ejemplo de la cámara %d completo... \n' % i)
del cam
cam_list.Clear()
system.ReleaseInstance()
except PySpin.SpinnakerException as ex:
print('Error en el sistema: %s' % ex)
result = False
return result
if __name__ == '__main__':
if main():
sys.exit(0)
else:
sys.exit(1)