-
Notifications
You must be signed in to change notification settings - Fork 7
/
skycamera.py
290 lines (212 loc) · 6.49 KB
/
skycamera.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2016 Joerg Hermann Mueller
#
# This file is part of pynephoscope.
#
# pynephoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pynephoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pynephoscope. If not, see <http://www.gnu.org/licenses/>.
from astropy.time import Time
import os
import cv2
import numpy as np
import sys
from configuration import Configuration
from night import NightChecker
from control import SkyCameraControl
from skycamerafile import SkyCameraFile
class DynamicDifferenceThreshold:
def __init__(self):
self.last = []
self.last_index = None
self.count = 0
def check(self, difference):
k = Configuration.dnfa_window_size
# first lets save the new value
if len(self.last) < k:
self.last.append(difference)
self.last_index = len(self.last) - 1
else:
self.last_index = (self.last_index + 1) % k
self.last[self.last_index] = difference
# checking counts
self.count += 1
if self.count < Configuration.dnfa_min_frames:
return False
if self.count >= Configuration.dnfa_max_frames:
self.count = 0
return True
# now let's calculate the dynamic threshold
mi = min(self.last)
me = np.median(self.last)
threshold = Configuration.dnfa_min_med_diff_factor * max(me - mi, Configuration.dnfa_min_diff_value) + me
if difference > threshold:
self.count = 0
return True
return False
class SkyCamera:
@staticmethod
def log(message):
if Configuration.logging:
time = Time.now()
log = open(Configuration.log_file, "a")
log.write("%s: %s\n" % (time.iso, message))
log.close()
@staticmethod
def getBitMask():
return np.uint8(cv2.imread(Configuration.mask_file, 0) / 255)
@staticmethod
def getMask():
return np.float32(SkyCamera.getBitMask())
def __init__(self):
self.device = 0
self.channel = 0
self.capture = cv2.VideoCapture()
self.control = SkyCameraControl()
self.night_checker = NightChecker()
self.night = None
self.last_image = None
self.differenceChecker = DynamicDifferenceThreshold()
def checkDaytime(self):
self.night_checker.now()
night = self.night_checker.isNight()
if self.night != night:
self.night = night
if Configuration.control_settings:
self.control.switchConfiguration(self.night, Configuration.verbose_commands)
def open(self):
self.capture.open(self.device)
def close(self):
self.capture.release()
def readNight(self):
sum_image = None
count = 0
while True:
if self.capture.grab():
if cv2.__version__[0] == '3':
_, image = self.capture.retrieve(flag = self.channel)
else:
_, image = self.capture.retrieve(channel = self.channel)
image1 = np.int32(image)
if self.last_image is not None:
difference = image1 - self.last_image
else:
difference = np.array([0])
difference = float(np.sum(np.abs(difference))) / float(difference.size)
if sum_image is None:
sum_image = self.last_image
else:
sum_image += self.last_image
count += 1
self.last_image = image1
if self.differenceChecker.check(difference):
SkyCamera.log('Difference: %f %d 1' % (difference, count))
time = Time.now()
self.checkDaytime()
return np.uint8(sum_image / count), time
else:
SkyCamera.log('Difference: %f %d 0' % (difference, count))
else:
return None, None
def read(self):
sum_image = None
image = None
count = Configuration.day_averaging_frames
if self.night:
count = Configuration.night_averaging_frames
for i in range(count):
if self.capture.grab():
if cv2.__version__[0] == '3':
_, image = self.capture.retrieve(flag = self.channel)
else:
_, image = self.capture.retrieve(channel = self.channel)
if sum_image is None:
sum_image = np.int32(image)
else:
sum_image += np.int32(image)
time = Time.now()
self.checkDaytime()
return np.uint8(sum_image / count), time
def oneShot(self):
self.open()
image, time = self.read()
self.close()
return image, time
@staticmethod
def saveToFile(image, time, path, sub_directory = True):
SkyCameraFile.stampImage(image, time)
if sub_directory:
path = os.path.join(path, str(int(time.mjd)))
if not os.path.exists(path):
os.mkdir(path)
filename = SkyCameraFile.getFileName(time)
path = os.path.join(path, filename)
cv2.imwrite(path, image)
def captureToFile(self, path, sub_directory = True):
if self.capture.isOpened():
if Configuration.dnfa_enabled:
if self.night:
image, time = self.readNight()
else:
image, time = self.read()
else:
image, time = self.read()
else:
image, time = self.oneShot()
if image is None:
return None, None
SkyCamera.saveToFile(image, time, path, sub_directory)
return image, time
if __name__ == '__main__':
directory = Configuration.default_storage_path
count = Configuration.frame_count
if len(sys.argv) > 1:
directory = sys.argv[1]
if len(sys.argv) > 2:
count = int(sys.argv[2])
window = 'All Sky Camera'
camera = SkyCamera()
camera.open()
loop = True
if Configuration.show_recorded_frames:
cv2.namedWindow(window)
i = 0
start = Time.now()
while loop:
image, time = camera.captureToFile(directory, Configuration.store_in_subdirectory)
if image is None:
print("Error opening Device")
sys.exit(1)
print("stored image %d at time %s" % (i, time.iso))
i += 1
if Configuration.show_recorded_frames:
cv2.imshow(window, image)
time_between_frames = Configuration.day_time_between_frames
if camera.night:
time_between_frames = Configuration.night_time_between_frames
end = Time.now()
deltatime = int((time_between_frames - (end - start).sec) * 1000)
if deltatime < 1:
deltatime = 1
key = cv2.waitKey(deltatime) & 0xFF
start = Time.now()
if key == 27:
loop = False
if count > 0:
count -= 1
if count == 0:
loop = False
camera.close()
if Configuration.show_recorded_frames:
cv2.destroyAllWindows()