-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
399 lines (320 loc) · 14.6 KB
/
Copy pathcamera.py
File metadata and controls
399 lines (320 loc) · 14.6 KB
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
import os
import cv2
import imutils # type: ignore
import numpy as np
from PySide6.QtCore import QThread, Signal
from PySide6.QtGui import QImage
from tl_dotnet_wrapper import TL_SDK
# TODO: fix slow update rate of GUI -> maybe use camera.queue and/or don't display/emit 1 out of N frames
# NOTE: The frames received from the camera are 16 bit !!!
class ScientificCamera(QThread):
# Emit signal with processed frame/s,
frame_signal = Signal(QImage)
model_signal = Signal(str)
written_signal = Signal(bool)
debug = 2
stop = False
exposure_flag = False
gain_flag = False
bins_flag = False
def __init__(self, n_frames=1, exposure_ms=1, gain=25, bins=1):
super().__init__()
self.n_frames = n_frames
self.exposure_ms = exposure_ms
self.gain = gain
self.bins = bins
def run(self):
if self.debug == 0:
self.is_captured = False
self.cap = cv2.VideoCapture(0)
i = 0
frames = []
self.model_signal.emit("Thorcam")
while self.cap.isOpened():
_, frame = self.cap.read()
if self.stop:
break
if self.is_captured:
frames.append(frame)
i += 1
print(f"Frame #{i}")
if i == self.frames:
final_image = np.array(
np.mean(frames, axis=(0)), dtype=np.uint8
)
written = cv2.imwrite(
os.path.join(self.directory, "image_directory.png"),
final_image,
)
self.written_signal.emit(written)
i = 0
frames = []
self.is_captured = False
frame = self.cvimage_to_label(frame)
self.frame_signal.emit(frame)
self.cap.release()
cv2.destroyAllWindows()
elif self.debug == 1:
# continuous frame polling
try:
# initialize camera sdk
sdk = TL_SDK()
available_cameras = sdk.get_camera_list()
# NOTE: dispose of SDK if nothing works, otherwise it remains open!!!
# Return if no cameras detected
if len(available_cameras) < 1:
print("no cameras detected")
sdk.close()
return
# open camera
camera = sdk.open_camera(available_cameras[0])
# default parameters
camera.set_exposure_time_us(self.exposure_ms * 1000) # ms
camera.set_frames_per_trigger_zero_for_unlimited(0) # continuous mode
# TODO: set gain
# TODO: set taps
# camera.set_taps(4)
# set data rate
# camera.set_data_rate("30FPS")
# set frame rate control
camera.set_is_frame_rate_controlled(True)
camera.set_frame_rate_fps(15)
# TODO: set image queue in order to speed up process
# NOTE: too slow when immediately retrieving frame instead of queueing frames!
# camera.set_maximum_number_of_frames_to_queue(10)
# emit camera model
self.model_signal.emit(camera.get_model())
# arm camera and set to software trigger
camera.arm()
camera.issue_software_trigger()
self.is_captured = False
index = 0
imaging_index = 1
frames = []
while True:
# stop camera
if self.stop:
break
# update exposure before getting pending frame
if self.exposure_flag:
camera.set_exposure_time_us(self.exposure_ms * 1000)
self.exposure_flag = False
# actual polling for single frame
frame = None
while frame is None:
frame = camera.get_pending_frame_or_null()
# TODO: use pending_ARRAY and return each frame in array
if frame is not None:
# NOTE: using grayscale instead of RGB
# save current frame
frame = camera.frame_to_array(frame)
# capture N frames
if self.is_captured:
frames.append(frame)
index += 1
print(f"Frame #{imaging_index}")
# take average of N frames
if index == self.n_frames:
final_image = np.array(
np.mean(frames, axis=(0)), dtype=np.uint16
)
# NOTE: returns true if the image was successfully written
written = cv2.imwrite(
os.path.join(
self.directory,
f"image_{imaging_index}.png",
),
final_image,
)
# send signal for written and bind it to ptychography module
self.written_signal.emit(written)
index = 0
imaging_index += 1
frames = []
self.is_captured = False
# resize and convert frame to QImage and emit it
# TODO: decouple conversion into MainWindow, just emit the numpy array
# NOTE: since it's grayscale, no need to create 3 dimensional array for RGB
# DON'T RESIZE
# frame = imutils.resize(frame, width=240, height=240)
image = QImage(
frame,
frame.shape[1],
frame.shape[0],
QImage.Format_Grayscale16,
)
self.frame_signal.emit(image)
else:
# print("Unable to acquire image")
continue
# don't close the camera and sdk each time, insted grab image whenever requested
# make sure to disarm each time an image is grabbed
camera.disarm()
camera.close()
sdk.close()
except Exception as error:
print(f"Error: {error}")
sdk.close()
return
else:
# single frame
try:
# initialize camera sdk
sdk = TL_SDK()
available_cameras = sdk.get_camera_list()
# NOTE: dispose of SDK if nothing works, otherwise it remains open!!!
# Return if no cameras detected
if len(available_cameras) < 1:
print("no camera detected")
sdk.close()
return
# open camera
camera = sdk.open_camera(available_cameras[0])
# emit camera model
self.model_signal.emit(camera.get_model())
# set frames per trigger to single capture
camera.set_frames_per_trigger_zero_for_unlimited(1)
# default parameters
# set exposure, gain, black level
camera.set_black_level(54)
camera.set_exposure_time_us(self.exposure_ms * 1000)
camera.set_gain(self.gain)
# set binning to (0, 0), w: 3296, h: 2472, binx = biny
print(
f"width: {camera.get_sensor_width_pixels()}, height: {camera.get_sensor_height_pixels()}"
)
camera.set_roi_binning(
0,
0,
camera.get_sensor_width_pixels(),
camera.get_sensor_height_pixels(),
self.bins,
self.bins,
)
self.is_captured = False
index = 0
imaging_index = 1
frames = []
while True:
# stop camera
if self.stop:
break
# update exposure
if self.exposure_flag:
camera.set_exposure_time_us(self.exposure_ms * 1000)
self.exposure_flag = False
# update gain
if self.gain_flag:
camera.set_gain(self.gain)
self.gain_flag = False
# NOTE: to set the ROI, camera must be disarmed
# update bins (w: 3296, h: 2472)
if self.bins_flag:
camera.set_roi_binning(
0,
0,
camera.get_sensor_width_pixels(),
camera.get_sensor_height_pixels(),
self.bins,
self.bins,
)
self.bins_flag = False
# capture N frames
if self.is_captured:
# call disarm before arming to clear current queue
camera.disarm()
# TODO: check if you need to set exposure and gain at each trigger
print(
f"Before, exposure: {camera.get_exposure_time_us()*1000}, gain: {camera.get_gain()}"
)
# TODO: read manual and check if set_frames_per_trigger is to be called each time to capture
# camera.set_exposure_time_us(self.exposure_ms * 1000)
# camera.set_gain(self.gain)
# camera.set_frames_per_trigger_zero_for_unlimited(1)
# camera.set_maximum_number_of_frames_to_queue(10)
camera.arm()
camera.issue_software_trigger()
# TODO
print(
f"After, exposure: {camera.get_exposure_time_us()*1000}, gain: {camera.get_gain()}"
)
# actual polling for single frame
frame = None
while frame is None:
frame = camera.get_pending_frame_or_null()
# convert frame to numpy.array
# NOTE: using grayscale instead of RGB
frame = camera.frame_to_array(frame)
frames.append(frame)
index += 1
print(f"Frame #{imaging_index}")
# take average of N frames
# NOTE: np.uint16 !!!
if index == self.n_frames:
if self.n_frames > 1:
final_image = np.array(
np.mean(frames, axis=(0)), dtype=np.uint16
)
# NOTE: returns true if the image was successfully written
written = cv2.imwrite(
os.path.join(
self.directory,
f"image_{imaging_index}.png",
),
final_image,
)
else:
written = cv2.imwrite(
os.path.join(
self.directory,
f"image_{imaging_index}.png",
),
frame,
)
# send signal for written and bind it to ptychography module
self.written_signal.emit(written)
index = 0
imaging_index += 1
frames = []
self.is_captured = False
# NOTE: remember to disarm camera each time
camera.disarm()
# resize and convert frame to QImage and emit it
# TODO: decouple conversion into MainWindow, just emit the numpy array
# NOTE: since it's grayscale, no need to create 3 dimensional array for RGB
# don't resize
# frame = imutils.resize(frame, width=600, height=600)
image = QImage(
frame,
frame.shape[1],
frame.shape[0],
QImage.Format_Grayscale16,
)
self.frame_signal.emit(image)
# don't close the camera and sdk each time, instead grab image whenever requested
# make sure to disarm each time an image is grabbed
camera.disarm()
camera.close()
sdk.close()
except Exception as error:
print(f"Error: {error}")
sdk.close()
return
def cvimage_to_label(self, image):
image = imutils.resize(image, width=240, height=240)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = QImage(image, image.shape[1], image.shape[0], QImage.Format_RGB888)
return image
def update_exposure(self, exposure_ms):
self.exposure_flag = True
self.exposure_ms = exposure_ms
def update_gain(self, gain):
self.gain_flag = True
self.gain = gain
def update_bins(self, bins):
self.bins_flag = True
self.bins = bins
def capture(self, directory, n_frames):
self.is_captured = True
self.n_frames = n_frames
self.directory = directory