-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_copy.py
580 lines (494 loc) · 19.3 KB
/
main_copy.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# This Python file uses the following encoding: utf-8
import sys
from pathlib import Path
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem
from PyQt5.QtWidgets import QFileDialog, QMessageBox
from PyQt5.QtCore import QFile, Qt, QTimer
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtGui import QIcon, QImage, QPixmap
from PyQt5.QtGui import QIntValidator, QDoubleValidator
from PyQt5 import uic
import cv2
import json
import subprocess
from collections import deque
import time
import numpy as np
import os
class MainWindow(QWidget):
def __init__(self, image_buffer_size=20):
super(QWidget, self).__init__()
self.load_ui()
# Add image buffers as deques
self.buffer_side = deque(maxlen=image_buffer_size)
self.buffer_top = deque(maxlen=image_buffer_size)
def load_ui(self):
self.curpath = Path(__file__).parent
path = self.curpath / "ui" / "control_panel.ui"
ui_file = QFile(path.as_posix())
ui_file.open(QFile.ReadOnly)
uic.loadUi(ui_file, self)
ui_file.close()
# Load parameters for the camera address
cam_config_file = self.curpath / "config" / "cameras.json"
with open(cam_config_file.as_posix()) as f:
params = json.load(f)
# pass
# TODO: change address to address!
self.button_side_cam.clicked.connect(lambda: self.fun_window_cam(which="side",
cam_params=params["side"]))
self.button_top_cam.clicked.connect(lambda: self.fun_window_cam(which="top",
cam_params=params["top"]))
#address="http://raspberrypi.local:8081"))
self.button_measurement.clicked.connect(lambda: self.fun_window_measure())
def fun_window_cam(self, which, cam_params={}):
assert which in ("side", "top")
# Start a side cam window
window_name = "window_{}_cam".format(which)
if not hasattr(self, window_name):
setattr(self, window_name,
VideoWindow(title="{} Camera".format(which.capitalize()),
parent=self,
which=which,
cam_params=cam_params))
# If window not visible then show it
win_obj = getattr(self, window_name)
if not win_obj.isVisible():
win_obj.show()
def switch_cam_state(self, which):
# Provide which for either side or top ?
assert which in ("side", "top")
button = getattr(self, "button_{}_cam".format(which))
window = getattr(self, "window_{}_cam".format(which))
if window.camera_activated:
img_path = self.curpath / "icons" / "{}_cam_on.png".format(which)
else:
img_path = self.curpath / "icons" / "{}_cam_off.png".format(which)
button.setIcon(QIcon(img_path.as_posix()))
def fun_window_measure(self):
# Open an window instance of measurement series
# and add to the self.windows_measurement list
if not hasattr(self, "windows_measurement"):
self.windows_measurement = []
new_window = MeasurementWindow(title="",
parent=self)
# TODO: How to remove the window instance?
self.windows_measurement.append(new_window)
new_window.show()
print(self.windows_measurement)
class VideoWindow(QWidget):
def __init__(self, title="",
parent=None,
which="",
cam_params={}):
super(QWidget, self).__init__()
self.load_ui()
if title != "":
self.setWindowTitle(title)
# Quick tweak with parent window?
if parent is not None:
self.parent = parent
# TODO: must be better way to solve the button assignment?!
# which camera to use?
self.which = which
self.camera_activated = False
self.cam_params = cam_params
# Get address of camera, use int is possible
address = cam_params["address"]
try:
address = int(address)
except ValueError:
pass
self.address = address
# Set FPS; Possibly to a lower number if Video is Slaggy
try:
fps = int(cam_params["fps"])
except ValueError:
fps = 30
self.fps = fps
# Turn on and off video preview
self.radio_preview.toggled.connect(self.toggle_preview)
self.camera = cv2.VideoCapture(self.address)
self.camera_initialized = self.camera.isOpened()
print(self.address, self.camera)
self.timer = QTimer()
self.timer.timeout.connect(self.next_frame)
self.img_buffer = getattr(self.parent,
"buffer_{which}".format(which=which))
# Turn on and off
def toggle_preview(self):
# TODO: add state check for error in video
if self.radio_preview.isChecked():
# If camera not initialized, try to use the cam_params["init_cmd"]
if not self.camera_initialized:
ret = init_cam(self.cam_params["init_cmd"])
if ret is False:
self.radio_preview.setChecked(False)
if self.camera_initialized:
print("Preview !")
self.camera_activated = True
self.timer.start(int(1000 / self.fps))
else:
print("No preview")
self.camera_activated = False
# Stop the timer
self.timer.stop()
# self.video_frame.setPixmap(QPixmap())
self.video_frame.setText("No Preview Available")
self.video_frame.adjustSize()
# self.adjustSize()
self.parent.switch_cam_state(self.which)
def next_frame(self):
# Net frame?
# TODO: use the threaded version instead
ret, frame = self.camera.read()
#print(ret, frame.shape)
# If there is no image captured, return False
# So that the Label is "no preview"
if frame is None:
# If ret is False then the image is None
empty_img = np.zeros((640, 480, 3), np.uint8)
self.img_buffer.append(empty_img)
return False
self.img_buffer.append(frame)
# ret, frame = self.image_hub.recv_image()
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Now add the image data to the buffer_side
#print(frame.shape)
# OpenCV used BGR format!
img = QImage(frame, frame.shape[1],
frame.shape[0],
QImage.Format_BGR888)
pix = QPixmap.fromImage(img)
# TODO: Check if resizing really works
self.video_frame.setPixmap(pix)
self.video_frame.adjustSize()
return True
def load_ui(self):
curpath = Path(__file__).parent
path = curpath / "ui" / "video_window.ui"
ui_file = QFile(path.as_posix())
ui_file.open(QFile.ReadOnly)
uic.loadUi(ui_file, self)
ui_file.close()
class MeasurementWindow(QWidget):
def __init__(self, title="", parent=None):
super(QWidget, self).__init__()
self.load_ui()
# Set the field to contain only digits
self.field_time_interval.setValidator(QDoubleValidator())
self.field_counts.setValidator(QIntValidator())
# Simply use array to store?
# self.images_side = []
# self.images_top = []
# self.results = ObjectArray(rows)
# Which column is activated?
self.current_column = 0
if parent is not None:
self.parent = parent
# Setup signal
self.button_measure.clicked.connect(self.start_measure)
self.button_save.clicked.connect(self.save)
self.button_update.clicked.connect(self.resize_table_rows)
# To provide
self._init_results()
self._init_table()
def _get_rows(self):
return int(self.field_counts.text())
def _init_results(self):
rows = self._get_rows()
cols = 10 # Default rows
self.results = ObjectArray(rows, cols)
def _init_table(self):
self.resize_table_rows()
self.table.setCurrentCell(0, 0)
def _enable_controls(self, choice):
# Enable or diable controls
for name in ("button_measure",
"button_save",
"field_time_interval",
"field_counts",
"table"):
getattr(self, name).setEnabled(choice)
def load_ui(self):
curpath = Path(__file__).parent
path = curpath / "ui" / "experiment_window.ui"
ui_file = QFile(path.as_posix())
ui_file.open(QFile.ReadOnly)
uic.loadUi(ui_file, self)
ui_file.close()
def start_measure(self):
# Start the measurement
# Should not return error since Validators used
# TODO: add a thread lock to block further data acq
# Do nothing!
print("Current column", self.table.currentColumn())
t_interval = int(self.field_time_interval.text())
total_counts = int(self.field_counts.text())
# Local image buffers
# local_results[0] = = []
# local_results[1] = []
print("Will do with: ", t_interval, total_counts)
print("Capturing now ........")
cnt = 0
current_column = self.table.currentColumn()
self._enable_controls(False)
def handler():
nonlocal cnt
cnt += 1
# Update text
self.text_status.setText("Capturing:\n{0}/{1}".format(cnt, total_counts))
# try add the last img in the queue,
# other with add the empty img
# Left image, right image, test fields
local_results = [None, None, None]
try:
local_results[0] = self.parent.buffer_side[-1]
except IndexError:
local_results[0] = gen_empty_img()
try:
local_results[1] = self.parent.buffer_top[-1]
except IndexError:
local_results[1] = gen_empty_img()
# TODO: need more elegant code to do it outside
self.table.setItem(cnt - 1,
current_column,
QTableWidgetItem("---"))
self.table.setCurrentCell(cnt - 1,
current_column)
self.results.set_item(cnt - 1,
current_column,
local_results)
# print(time.time())
if cnt >= total_counts:
# self.button_measure.setEnabled(True)
# Clear the status
timer.stop()
timer.deleteLater()
cnt = 0
QTimer.singleShot(1000,
lambda: self.text_status.setText(""))
# self.images_side.append(local_results[0] = )
# self.images_top.append(local_results[1])
self._enable_controls(True)
self.table.setCurrentCell(0, current_column + 1)
return
# self.timer.singleShot(t_interval, handler)
timer = QTimer()
timer.timeout.connect(handler)
timer.start(t_interval)
return True
def save(self):
(filename,
filetype) = QFileDialog.getSaveFileName(self,
caption=
"Choose the name to save")
fn = Path(filename)
print(filename, fn)
save_root = fn.parent
save_name = fn.name
self.save_partial(which="side", root=save_root, name=save_name)
self.save_partial(which="top", root=save_root, name=save_name)
def save_partial(self, which="side",
root=None,
name=""):
# Save images and csv files
# TODO: remove this testing code
if root is None:
root = self.parent.curpath / "test_ui"
if not root.is_dir():
os.makedirs(root)
img_path = name + "_{which}_{i}.bmp"
# Disable in case save process is long
# It is a blocking process!
self._enable_controls(False)
cnt = 0
def handler():
nonlocal cnt
print(which, cnt)
# images_list = getattr(self, "images_{0}".format(which))
if which == "side":
index = 0
else:
index = 1
# total_c = len(images_list)
# total_r = len(images_list[0])
total_r, total_c = self.results.get_max_nonempty()
# print(which)
# Use ndarray instead
total_imgs = total_r * total_c
if cnt >= total_imgs:
timer.stop()
timer.deleteLater()
cnt = 0
QTimer.singleShot(1000,
lambda: self.text_status.setText(""))
self._enable_controls(True)
return
r = cnt // total_r
c = cnt % total_r
print(total_c, total_r, c, r)
# img = images_list[c][r]
img = self.results.get_item(r, c)[index]
if img is None:
img = gen_empty_img()
cnt += 1
rt = save_image(img,
root / img_path.format(which=which,
i=cnt))
self.text_status.setText("Saving images {0}\n{1}/{2}".
format(which,
cnt,
total_imgs))
timer = QTimer()
timer.timeout.connect(handler)
timer.start(10)
# handler("side")
# handler("top")
# print("I'm here")
def resize_table_rows(self):
"""Resize the table if possible
"""
# cur_col_cnt = self.table.columnCount()
cur_row_cnt = self.table.rowCount()
new_row_cnt = int(self.field_counts.text())
if new_row_cnt < cur_row_cnt:
warningbox(self,
("Cannot update the table\n"
"New row counts are less than current"),
level=1)
return False
self.table.setRowCount(new_row_cnt)
class ObjectArray(object):
"""Wrapper for object ndarray in a class
"""
def __init__(self, cols=1, rows=1):
"""Start with an empty nested list
array members are also `ndarray`
"""
# TODO: what if cols and rows < 1?
self.array = np.empty((rows, cols), dtype=np.object)
# Maximum indices for row and column that are not empty
self.max_nonempty = (-1, -1)
def get_max_nonempty(self):
# TODO: update to `@getter
def _is_none(x):
"""Return if the object x is none
"""
return x is None
# morph into array version
is_none = np.frompyfunc(_is_none, 1, 1)
# Must use strong type conversion, otherwise invert will fail
flags = ~(is_none(self.array)).astype(np.bool)
# Non-empty rows and cols
row_idx, col_idx = np.where(flags)
# Get the max row and col indices, otherwise -1
try:
max_row = np.max(row_idx)
except ValueError:
max_row = -1
try:
max_col = np.max(col_idx)
except ValueError:
max_col = -1
self.max_nonempty = (max_row, max_col)
# TODO: is the return necessary?
return self.max_nonempty
def resize_array(self, rows, cols):
"""Try to resize the array while keeping current data untouched
"""
old_rows_nonempty, old_cols_nonempty = self.get_max_nonempty()
# Will the new table truncate existing data?
if (rows < old_rows_nonempty) or (cols < old_cols_nonempty):
return False
old_rows, old_cols = self.array.shape
new_array = self.array
# First do rows and then columns
if rows <= old_rows:
new_array = new_array[: rows + 1, :]
else:
new_array = add_rows(new_array, rows - old_rows)
if cols <= new_cols:
new_array = new_array[:, : cols + 1]
else:
new_array = add_columns(new_array, cols - old_cols)
self.array = new_array
return True
def get_shape(self):
# TODO: maybe getter
return self.array.shape
def get_item(self, row, col):
# TODO: better reload of ndarray?
return self.array[row, col]
def set_item(self, row, col, item):
# TODO: better reload of ndarray?
self.array[row, col] = item
return True
def main_loop():
# Set the high DPI display and icons
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
# Mainloop
app = QApplication([])
widget = MainWindow()
widget.show()
sys.exit(app.exec_())
######## OOP-independent functions ###########
######## To be moved to other libs ###########
def init_cam(cmd):
"""Use subprocess to execute a command, if needed to activate the camera
"""
cmd = cmd.strip()
if len(cmd) > 0:
print("Executing the activating cmd:")
print(cmd)
print("---------Output from process--------------")
proc = subprocess.run(cmd, shell=True)
return proc.returncode == 0
else:
# No command provided
return -1
def save_image(img, path):
# Give a PIL img matrix and Path
path = Path(path)
retcode = cv2.imwrite(path.as_posix(), img)
return retcode
def warningbox(parent, message, level=0):
"""Popup a QMessageBox for warning information
levels:
0 --- normal message (about)
1 --- warning message
2 --- error (critical)
"""
if level == 0:
QMessageBox.about(parent, "", message)
elif level == 1:
QMessageBox.warning(parent, "Warning", message)
else:
QMessageBox.critical(parent, "Error", message)
######### array-related methods####################
def add_columns(array, cols=1):
"""Add `cols` new columns to the right-side of the array
"""
# TODO: error handling
rows = array.shape[0]
new_cols = np.empty((rows, cols), dtype=np.object)
new_array = np.concatenate((array, new_cols),
axis=1)
return new_array
def add_rows(array, rows=1):
"""Add `rows` new rows below the array
"""
# TODO: error handling
cols = array.shape[1]
new_rows = np.empty((rows, cols), dtype=np.object)
new_array = np.concatenate((array, new_rows),
axis=0)
return new_array
def gen_empty_img(w=640, h=480):
"""Generate a black image
"""
return np.zeros((h, w, 3), np.uint8)
if __name__ == "__main__":
main_loop()