This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskycam.py
executable file
·352 lines (244 loc) · 10.3 KB
/
skycam.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
#!/usr/bin/env python3
import os
import time
import zwoasi as asi
from threading import Thread
from queue import Queue
from PIL import Image
from glob import glob
class SkyCam:
""" SkyCam is an abstraction layer for zwoasi Python bindings
"""
@staticmethod
def initialize(_library=None):
""" Initialize ZWOASI SDK library
The official SDK library can be obtained from this link:
https://astronomy-imaging-camera.com/tets1/
Args:
_library (str): ovveride default location for the library
"""
if _library is None:
_library = os.path.dirname(os.path.realpath(__file__))\
+ '/asi.so'
asi.init(_library)
@staticmethod
def cameras():
""" List of conneted cameras
Returns:
list: List of camera names as sttings
"""
return asi.list_cameras()
def __init__(self, _camera_id, _bandwidth=80):
""" Initializes a SkyCam camera object
This funtion automatically sets camera parameters
to default settings. To change them use configure()
Args:
_camera_id (int): Camera ID in the cameras() list or it's name
"""
self.camera = asi.Camera(_camera_id)
self.camera_info = self.camera.get_camera_property()
self.camera.set_control_value(asi.ASI_BANDWIDTHOVERLOAD, _bandwidth)
self.camera.stop_video_capture()
self.camera.stop_exposure()
self.configure()
self.frame_buffer = Queue()
self.frame_counter = 0
self.recorder = self.Recorder(self)
self.camera.set_control_value(asi.ASI_GAIN, 150)
self.camera.set_control_value(asi.ASI_EXPOSURE, 1000000)
self.camera.set_control_value(asi.ASI_WB_B, 99)
self.camera.set_control_value(asi.ASI_WB_R, 75)
self.camera.set_control_value(asi.ASI_GAMMA, 60)
self.camera.set_control_value(asi.ASI_BRIGHTNESS, 50)
self.camera.set_control_value(asi.ASI_FLIP, 0)
self.camera.start_video_capture()
self.camera.set_image_type(asi.ASI_IMG_RAW8)
def configure(self, _gain=None, _exposure=None, _wb_b=None,\
_wb_r=None, _gamma=None, _brightness=None, _flip=None,\
_bin=None, _roi=None, _drange=None,\
_color=None, _mode=None):
""" Used to change camera parameters
Args:
_gain (int): Camera gain
_exposure (int): Camera exposure in microseconds
_wb_b (int): Camera whitebalance
_wb_r (int): Camera whitebalance
_gamma (int): Camera gamma
_brightness (int): Camera brightness
_flip (int): Picture flip, valuse can be 0 or 1
_bin (int): Picture binning, values can be 1 or 2
_roi (tuple): Region of interest, formatted as a
tuple (x, y, width, height)
_drange (int): Dynamic range, value can be 8 or 16 bits
_color (bool): Camera oolor mode
_mode (str): Capturing mode, value can be 'video' or 'piture'
If set to 'picture', apturing is a lot slower.
"""
self.camera.stop_exposure()
if _mode == 'video':
self.camera.start_video_capture()
elif _mode == 'picture':
self.camera.stop_video_capture()
if _mode is not None:
self.mode = _mode
if _exposure is not None:
self.camera.set_control_value(asi.ASI_EXPOSURE, _exposure)
if _gain is not None:
self.camera.set_control_value(asi.ASI_GAIN, _gain)
if _wb_b is not None:
self.camera.set_control_value(asi.ASI_WB_B, _wb_b)
if _wb_r is not None:
self.camera.set_control_value(asi.ASI_WB_R, _wb_r)
if _gamma is not None:
self.camera.set_control_value(asi.ASI_GAMMA, _gamma)
if _brightness is not None:
self.camera.set_control_value(asi.ASI_BRIGHTNESS, _brightness)
if _flip is not None:
self.camera.set_control_value(asi.ASI_FLIP, _flip)
if _bin is None:
_bin = 1
if _roi is None:
_roi = (
0, 0,
int(self.camera_info['MaxWidth'] / _bin),
int(self.camera_info['MaxHeight'] / _bin)
)
self.camera.set_roi(start_x=_roi[0], start_y=_roi[1],\
width=_roi[2], height=_roi[3], bins=_bin)
if _color is True:
self.camera.set_image_type(asi.ASI_IMG_RGB24)
else:
if _drange is 8:
self.camera.set_image_type(asi.ASI_IMG_RAW8)
elif _drange is 16:
self.camera.set_image_type(asi.ASI_IMG_RAW16)
def capture(self, _directory=None, _file=None, _format='.jpg'):
""" Frame capturing function
If both _directory and _file are not declared, it will return
the picture as an array. Otherwise, undeclared parameters will
fall back to default values.
Args:
_directory (str): Path for saving captured photos
_file (str): File name, strftime formatting is enabled
Formatting instrutions: http://strftime.org/
_format (str): Indiates piture format, default is JPEG
Returns:
numpy array: If both _directory and _file are not declared,
it will only return the picture as an array.
"""
if _file is None and _directory is not None:
_file = self.camera_info['Name'].replace(' ', '-') \
+ '-%Y-%m-%d-%H-%M-%S-%Z-' +\
str(self.frame_counter) + _format
self.frame_counter += 1
if _directory is None and _file is not None:
if not os.path.isdir('/tmp/skycam/'):
os.makedirs('/tmp/skycam', 755)
_directory = '/tmp/skycam/'
if _file is not None and _directory is not None:
_file = time.strftime(_file)
if self.mode == 'picture':
self.camera.capture(filename=\
(_directory + '/' + _file))
elif self.mode == 'video':
self.camera.capture_video_frame(filename=\
(_directory + '/' + _file))
if self.mode == 'picture':
return self.camera.capture()
elif self.mode == 'video':
return self.camera.capture_video_frame()
class Recorder:
""" Recorder is used to record continuous
frames automatically
Note that Recorder class gets isntanced as
SkyCam.recorder object.
"""
def __init__(self, _owner):
""" Sets all variables to default state
"""
self.owner = _owner
self.buffer = Queue()
self.recording = False
self.delay = 0
self.directory = None
self.file = None
self.format = None
self.keep = True
self.save = False
def configure(self, _delay=None, _keep=None, _save=None,\
_directory=None, _file=None, _format=None):
""" Configure SkyCam recorder
Args:
_delay (int): Delay between frames in milliseconds
_keep (bool): Indicates whether to keep frames
in RAM buffer
_save (bool): Indicates whether to keep frames
to storage device
_directory (str): Path for saving captured photos
_file (str): File name, strftime formatting is enabled
Formatting instrutions: http://strftime.org/
_format (str): Indiates piture format, default is JPEG
"""
if _delay is not None:
self.delay = _delay
if _directory is not None:
self.directory = _directory
if _file is not None:
self.file = _file
if _format is not None:
self.format = _format
if _keep is not None:
self.keep = _keep
if _save is not None:
self.save = _save
def record(self):
""" Recorder background thread
Do not call this method directly. Use start() instead.
"""
while self.recording:
if self.save:
_frame = self.owner.capture(_directory=self.directory,\
_file=self.file, _format=self.format)
else:
_frame = self.owner.capture()
if self.keep:
self.buffer.put((_frame, time.time()))
time.sleep(self.delay / 1000)
def start(self):
""" Starts background thread for recording
"""
self.recorder = Thread(target=self.record, args=())
self.recording = True
self.recorder.start()
def stop(self):
""" Stops background thread for recording
"""
self.recording = False
self.recorder.join()
def buffer_is_empty(self):
""" Ckeck if buffer is empty
"""
return self.buffer.empty()
def buffer_next(self):
""" Returns oldest frame in the buffer
Calling this method requires _keep to be True.
Returned frame gets removed from the buffer.
"""
return self.buffer.get_nowait()
def buffer_all(self):
""" Returns all frames stored in the buffer
Calling this method requires _keep to be True.
"""
return list(self.buffer.queue)
def buffer_clear(self):
""" Clears the buffer
"""
self.buffer.clear()
def buffer_load(self):
""" Loads all frames stored in _directory
into the buffer.
Calling this method requires _directory to be
defined to an existing direcotry.
"""
_files = glob(self.directory)
print(_files)