-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlc-demo.py
executable file
·417 lines (309 loc) · 13.1 KB
/
lc-demo.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
import os
import datetime as dt
from PyQt5.uic import loadUiType
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas)
import numpy as np
import cv2
# Load our UI file created in designer
Ui_MainWindow, QMainWindow = loadUiType('lightcurver.ui')
class Main(QMainWindow, Ui_MainWindow):
def __init__(self, video_device=0):
super(Main, self).__init__()
# self.showMaximized()
self.setupUi(self)
self.reset_data()
# Plot basics
self.fig1 = Figure()
self.ax1 = self.fig1.add_subplot(111)
self.canvas = FigureCanvas(self.fig1)
self.lcLayout.addWidget(self.canvas)
# UI callbacks
self.start_button.clicked.connect(self.start_lightcurve)
self.clear_button.clicked.connect(self.clear_lightcurve)
self.quit_button.clicked.connect(self.quit_application)
self.actionQuit.triggered.connect(self.quit_application)
self.lc_interval.valueChanged.connect(self.update_interval)
self._lc_value = self.lc_interval.value()
self._lc_active = False
self._normal_factor = [1., 1., 1., ]
# Setup webcam
self.capture = QtCapture(
video_device, self.radius_slider, self.actionColors)
self.capture.setParent(self)
self.capture.setWindowFlags(QtCore.Qt.Tool)
self._image_saved = False
self._image_path = ''
self.webcamLayout.addWidget(self.capture)
self.setWindowTitle('LightCurve Demo')
self.canvas.draw()
self.show()
self.start_application()
def reset_data(self):
self._lc_value = self.lc_interval.value()
self._lc_tick_num = 0
self._lc_tick = 40
self._lc_max_tick_num = int((self._lc_value * 1000.) / self._lc_tick)
self._lc_range = np.arange(
0, (self._lc_max_tick_num * self._lc_tick), self._lc_tick) / 1000.
self._lc_data = np.zeros((3, self._lc_max_tick_num))
##########################################################################
# Properties
##########################################################################
@property
def getting_lc(self):
return self._lc_active
##########################################################################
# Timers
##########################################################################
def start_application(self):
if not self.capture:
self.capture = QtCapture(0)
self.stop_button.clicked.connect(self.capture.stop)
self.capture.setParent(self)
self.capture.setWindowFlags(QtCore.Qt.Tool)
self.capture.show()
self.start_webcam()
def start_webcam(self):
self.webcam_timer = QtCore.QTimer()
self.webcam_timer.timeout.connect(self.webcam_callback)
self.webcam_timer.start(40)
def stop_webcam(self):
self.webcam_timer.stop()
def start_lightcurve(self):
self._lc_active = True
self.lc_timer = QtCore.QTimer()
self.lc_timer.timeout.connect(self.lightcurve_callback)
self.reset_data()
self.lc_timer.start(self._lc_tick)
# UI disable
self.start_button.setDisabled(True)
self.lc_interval.setDisabled(True)
self.radius_slider.setDisabled(True)
self.radius_label.setDisabled(True)
self.seconds_label.setDisabled(True)
# If loop mode
if self.actionLoop_Mode.isChecked():
# Change start button to stop button
self.clear_button.setEnabled(True)
self.clear_button.setText('&Stop')
self.show()
def stop_lightcurve(self):
self.lc_timer.stop()
if self.actionLoop_Mode.isChecked():
self.clear_lightcurve()
self.start_lightcurve()
else:
self.clear_button.setEnabled(True)
# If we have a pic, show it
if self.actionSave_Pics and os.path.exists(self._image_path):
self.stop_webcam()
img_data = cv2.imread(self._image_path)
img_data = cv2.cvtColor(img_data, cv2.COLOR_BGR2RGB)
self.capture._set_image(img_data)
text, ok = QtWidgets.QInputDialog.getText(
self, 'Save Image?', 'Email Address')
if ok:
if text:
os.rename(
self._image_path, '/home/panoptes/Pictures/{}.png'.format(text))
self.fig1.savefig(
'/home/panoptes/Pictures/{}_plot.png'.format(text))
else:
self.fig1.savefig(
'{}_plot.png'.format(self._image_path))
else:
# Remove image
os.unlink(self._image_path)
self.clear_lightcurve()
def clear_lightcurve(self):
self._lc_active = False
self._image_saved = False
self._image_path = ''
self._lc_sec = 0.
# Reset timer
self.lc_interval.setValue(self._lc_value)
# Clear plot lines
try:
del self.gray_line
del self.r_line
del self.g_line
del self.b_line
except Exception:
pass
if not self.actionLoop_Mode.isChecked():
# UI enable
self.start_button.setEnabled(True)
self.radius_slider.setEnabled(True)
self.clear_button.setDisabled(True)
self.lc_interval.setEnabled(True)
self.radius_label.setEnabled(True)
self.seconds_label.setEnabled(True)
self.start_webcam()
##########################################################################
# Action Methods
##########################################################################
def webcam_callback(self):
if self.actionSave_Pics.isChecked() and \
not self._image_saved \
and hasattr(self, '_lc_sec') \
and self._lc_sec > self._lc_value / 2:
self._image_path = "/home/panoptes/Pictures/{}.png".format(
dt.datetime.now().isoformat())
self._image_saved = True
# Capture next webcam frame
self.img_data = self.capture.get_frame(save_frame=self._image_path)
self.plot_values(self.img_data)
def lightcurve_callback(self):
# Which second we are on
self._lc_sec = np.floor((self._lc_tick_num * self._lc_tick) / 1000.)
# Show countdown in spinbox
self.lc_interval.setValue(self._lc_value - self._lc_sec)
self._lc_tick_num = self._lc_tick_num + 1
if self._lc_tick_num >= self._lc_max_tick_num:
self.stop_lightcurve()
else:
# Update plot values
self.plot_values(self.img_data)
def plot_values(self, masked_data):
if self.actionColors.isChecked():
self._plot_color(masked_data)
else:
self._plot_gray(masked_data)
##########################################################################
# UI Methods
##########################################################################
def update_webcam(self):
self.stop()
self.start()
def update_interval(self):
if not self.getting_lc:
# Store
lc_interval = self.lc_interval.value()
self._lc_value = lc_interval
# Update plot
self.ax1.set_xlim(0, lc_interval)
def quit_application(self):
self._delete_later()
self.capture = None
QtCore.QCoreApplication.instance().quit()
##########################################################################
# Private Methods
##########################################################################
def _plot_gray(self, masked_data):
if not self.getting_lc:
self.ax1.clear()
self._plot_init()
self.ax1.axhline(100., color='k', ls='dashed')
# Store the normalization
self._normal_factor[0] = masked_data.sum()
else:
if self.lc_timer.isActive():
light_value = masked_data.sum() / self._normal_factor[0] * 100.
self._lc_data[0, self._lc_tick_num] = light_value
if not hasattr(self, 'gray_line'):
self.gray_line, = self.ax1.plot(
self._lc_range, self._lc_data[0], 'o', color='gray')
self.gray_marker, = self.ax1.plot(
self._lc_tick_num, color='gray')
else:
self.gray_line.set_data(self._lc_range, self._lc_data[0])
self.gray_marker.set_data(self._lc_range, self._lc_data[0])
self.fig1.canvas.draw()
def _plot_color(self, masked_data):
r_sum = masked_data[:, :, 0].sum()
g_sum = masked_data[:, :, 1].sum()
b_sum = masked_data[:, :, 2].sum()
if not self.getting_lc:
self.ax1.clear()
self._plot_init()
self.ax1.axhline(100., color='r', ls='dashed')
self.ax1.axhline(100., color='g', ls='dashed')
self.ax1.axhline(100., color='b', ls='dashed')
self._normal_factor = [r_sum, g_sum, b_sum]
else:
if self.lc_timer.isActive():
r_value = min((r_sum / self._normal_factor[0]) * 100., 100)
g_value = min((g_sum / self._normal_factor[1]) * 100., 100)
b_value = min((b_sum / self._normal_factor[2]) * 100., 100)
self._lc_data[0, self._lc_tick_num] = r_value
self._lc_data[1, self._lc_tick_num] = g_value
self._lc_data[2, self._lc_tick_num] = b_value
if not hasattr(self, 'r_line'):
self.r_line, = self.ax1.plot(
self._lc_range, self._lc_data[0], color='r')
self.g_line, = self.ax1.plot(
self._lc_range, self._lc_data[1], color='g')
self.b_line, = self.ax1.plot(
self._lc_range, self._lc_data[2], color='b')
else:
self.r_line.set_data(self._lc_range, self._lc_data[0])
self.g_line.set_data(self._lc_range, self._lc_data[1])
self.b_line.set_data(self._lc_range, self._lc_data[2])
self.fig1.canvas.draw()
def _plot_init(self):
# and disable figure-wide autoscale
# self.ax1.set_autoscale_on(False)
self.ax1.set_xlim(0, self._lc_value)
self.ax1.set_ylim(50., 105.)
self.ax1.set_xlabel("Time [s]")
self.ax1.set_ylabel("Light [\%]")
self.fig1.tight_layout()
def _delete_later(self):
self.capture.cap.release()
class QtCapture(QtWidgets.QWidget):
def __init__(self, vid_num, radius_slider, actionColors):
super(QtWidgets.QWidget, self).__init__()
self.cap = cv2.VideoCapture()
# self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
# self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
if self.cap.open(vid_num) is False:
raise Exception("Can't attach to video")
self.video_frame = QtWidgets.QLabel()
lay = QtWidgets.QVBoxLayout()
lay.addWidget(self.video_frame)
self.setLayout(lay)
ret, frame = self.cap.read()
self.height, self.width, self.depth = frame.shape
self._radius_slider = radius_slider
self.actionColors = actionColors
@property
def radius(self):
return self._radius_slider.value()
def get_frame(self, save_frame=''):
ret, frame = self.cap.read()
if save_frame is not '':
cv2.imwrite(save_frame, frame)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Create circular mask
circle_img = np.zeros((self.height, self.width), np.uint8)
cv2.circle(circle_img, (int(self.width / 2),
int(self.height / 2)), int(self.radius), 1, -1)
masked_data = cv2.bitwise_and(frame, frame, mask=circle_img)
# Show image in window
self._set_image(masked_data)
# If grayscale, convert frame before returning
if not self.actionColors.isChecked():
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
masked_data = cv2.bitwise_and(frame, frame, mask=circle_img)
return masked_data
def _set_image(self, img_data):
img = QtGui.QImage(img_data, img_data.shape[1], img_data.shape[
0], QtGui.QImage.Format_RGB888)
pix = QtGui.QPixmap.fromImage(img)
self.video_frame.setPixmap(pix)
if __name__ == '__main__':
import sys
import argparse
parser = argparse.ArgumentParser(description='Light-curve demo')
parser.add_argument('--device', type=int, default=0,
help='Video device to use, e.g. 0 for /dev/video0, 1 for /dev/video1')
args = parser.parse_args()
app = QtWidgets.QApplication(sys.argv)
main = Main(video_device=args.device)
main.show()
sys.exit(app.exec_())