-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasler_camera_array.py
292 lines (197 loc) · 9.46 KB
/
basler_camera_array.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
import numpy as np
from .basler_camera import BaslerCamera
import pypylon
class BaslerCameraArray:
"""
A class for basler camera array. Useful for capturing from multiple cameras simultaneously.
If the task requires high synchronization, using hardware synchronization is preferred.
"""
_TIME_OUT = 2000
def __init__(self, devices_info):
"""The constructor of the array.
:param devices_info: a list of camera device info stored as dictionaries that has IP address
or serial number.
Examples:
``devices_info = [{'serial_number': '21939024'}, {'ip': '192.168.0.31'}]``
"""
self._camera_array = None
self._device_info_objects = []
self._converter = None
for device_info in devices_info:
info = pypylon.pylon.CDeviceInfo()
if 'ip' in device_info:
info.SetIpAddress(device_info['ip'])
if 'serial_number' in device_info:
info.SetSerialNumber(device_info['serial_number'])
self._device_info_objects.append(info)
def _get_camera_array(self):
if self._camera_array is None:
raise NameError('Not initialized!')
else:
return self._camera_array
def _get_camera_by_id(self, cam_id: int):
"""get individual camera instance
:param cam_id: camera ID, determined by the order of appearance in devices_info at initialization
"""
camera_array = self._get_camera_array()
if (cam_id < 0) or (cam_id > camera_array.GetSize() - 1):
raise ValueError('Wrong Camera ID')
camera = camera_array[cam_id]
return camera
def connect(self):
tlf = pypylon.pylon.TlFactory.GetInstance()
num_devices = len(self._device_info_objects)
self._camera_array = pypylon.pylon.InstantCameraArray(num_devices)
for idx, device_info in enumerate(self._device_info_objects):
self._camera_array[idx].Attach(tlf.CreateDevice(device_info))
self._camera_array.Open()
def disconnect(self):
camera_array = self._get_camera_array()
camera_array.Close()
self._camera_array = None
# ----------------------- getter -----------------------------------
def get_aoi(self, cam_id:int):
"""return the area of interest (AOI) of a certain camera
:param cam_id: camera ID
:return: a tuple of ``(offset_x, offset_y, width, height)``
"""
cam = self._get_camera_by_id(cam_id)
aoi = BaslerCamera.get_aoi_helper(cam)
return aoi
def get_exposure_time(self, cam_id:int):
"""return the exposure time of a certain camera in millisecond (ms).
:param cam_id: camera ID
"""
cam = self._get_camera_by_id(cam_id)
exposure_time = BaslerCamera.get_exposure_time(cam)
return exposure_time
def get_resulting_framerate(self, cam_id:int):
"""return the resulting framerate of a certain camera.
:param cam_id: camera ID
"""
cam = self._get_camera_by_id(cam_id)
framerate = BaslerCamera.get_resulting_framerate(cam)
return framerate
# ----------------------- setter -----------------------------------
def set_converter(self, convert=True):
"""when saving TIFF files, always use a 16-bit format with the most significant bit (MSB)
aligned.
:param convert: a boolean
"""
if convert:
self._converter = pypylon.pylon.ImageFormatConverter()
self._converter.OutputPixelFormat = pypylon.pylon.PixelType_Mono16
self._converter.OutputBitAlignment = pypylon.pylon.OutputBitAlignment_MsbAligned
else:
self._converter = None
def set_pixel_format(self, cam_id: int, pixel_format_string: str):
"""set acquisition pixel format for the camera.
:param cam_id: camera id
:param pixel_format_string: a pixel format string, like ``Mono8``, ``Mono12``, ``Mono12Packed``.
Allowed values vary from camera to camera. Refer to camera documentation in the Pylon software for details.
"""
cam = self._get_camera_by_id(cam_id)
cam.PixelFormat.SetValue(pixel_format_string)
def set_acquisition_framerate(self, cam_id: int, framerate: float = None):
"""set the acquisition frame rates for a certain camera.
:param cam_id: camera ID.
:param framerate: frame rate.
:type cam_id: int
:type framerate: float
See the :func:`~basler.BaslerCamera.set_acquisition_framerate` of the BaslerCamera class for details.
"""
cam = self._get_camera_by_id(cam_id)
BaslerCamera.set_acquisition_framerate_helper(cam, framerate)
def set_exposure_time(self, cam_id: int, exposure_time: float):
"""set exposure time for a certain camera.
:param cam_id: camera ID.
:param exposure_time: exposure time.
:type cam_id: int
:type exposure_time: float
See the :func:`~basler.BaslerCamera.set_exposure_time` of BaslerCamera class for details.
"""
cam = self._get_camera_by_id(cam_id)
BaslerCamera.set_exposure_time_helper(cam, exposure_time)
def set_aoi(self, cam_id: int, aoi: tuple):
"""set the area of interest (AOI) of the camera.
:param cam_id: camera ID
:param aoi: a tuple: ``(offset_x, offset_y, width, height)``
"""
cam = self._get_camera_by_id(cam_id)
BaslerCamera.set_aoi_helper(cam, aoi)
# ----------------------- helper -----------------------------------
def post_processing(self, grab_result):
if self._converter is not None:
target_image = self._converter.Convert(grab_result)
else:
target_image = pypylon.pylon.PylonImage()
target_image.AttachGrabResultBuffer(grab_result)
return target_image
# --------------------------- grabbing -----------------------------
def grab_one(self):
"""grab one frame from each camera as a list of numpy arrays"""
camera_array = self._get_camera_array()
size = camera_array.GetSize()
result = []
for i in range(size):
grab_result = camera_array[i].GrabOne(self._TIME_OUT)
image_array = self.post_processing(grab_result).GetArray()
grab_result.Release()
result.append(image_array)
return result
def grab_many(self, n: int):
"""grab n frames from each camera, and return a list of numpy arrays of shape ``(n, height_i, width_i)``
where ``height_i`` and ``width_i`` are the height and width of the i-th camera
:param n: the number of frames
"""
camera_array = self._get_camera_array()
size = camera_array.GetSize()
result = []
frames_captured = np.zeros(size, dtype=int)
for i in range(size): # pre allocate array memory
cam = camera_array[i]
width = cam.Width.GetValue()
height = cam.Height.GetValue()
r = np.zeros([n, height, width])
result.append(r)
camera_array.StartGrabbing()
while True:
if (not camera_array.IsGrabbing()) or np.all(frames_captured >= n):
break
grab_result = camera_array.RetrieveResult(self._TIME_OUT, pypylon.pylon.TimeoutHandling_ThrowException)
camera_no = grab_result.GetCameraContext()
image_array = self.post_processing(grab_result).GetArray()
result[camera_no][frames_captured[camera_no], :, :] = image_array
frames_captured[camera_no] += 1
camera_array.StopGrabbing()
return result
def grab_n_save(self, n: int, save_patterns: list, n_start: list = None):
r"""grab n frames and save them sequentially as TIFF files
:param n: the number of frames to grab for each camera
:param save_patterns: a list of strings where each contains one single '%d' as the number.
:param n_start: a list of integers indicating the start number of the filename.
Default is ``[1, 1, ...]``, i.e. 1 for each camera.
Example:
``grab_n_save(200, ['/home/zhe/data/cam0-%d.tiff', '/home/zhe/data/cam1-%d.tiff'], [3, 5])``
Images are saved as ``cam0-3.tiff``, ``cam0-4.tiff``, ... for camera 0,
and ``cam1-5.tiff``, ``cam1-6.tiff``, ... for camera 1
"""
camera_array = self._get_camera_array()
size = camera_array.GetSize()
frames_captured = np.zeros(size, dtype=int)
if n_start is None:
n_start = np.ones(size, dtype=int)
camera_array.StartGrabbing()
while True:
if (not camera_array.IsGrabbing()) or np.all(frames_captured >= n):
break
grab_result = camera_array.RetrieveResult(self._TIME_OUT, pypylon.pylon.TimeoutHandling_ThrowException)
camera_no = grab_result.GetCameraContext()
image = self.post_processing(grab_result)
image = pypylon.pylon.PylonImage(image)
filename = save_patterns[camera_no] % (n_start[camera_no] + frames_captured[camera_no])
image.Save(pypylon.pylon.ImageFileFormat_Tiff, filename)
image.Release()
grab_result.Release()
frames_captured[camera_no] += 1
camera_array.StopGrabbing()