-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
243 lines (192 loc) · 8.35 KB
/
interface.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
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt, QTimer, QDateTime, QThread, QEventLoop
from PyQt5 import QtTest
from main import cv2, Path
from main import init_camera, video_loop, numpy_to_pixmap, build_log
sourceDict = {
"Cam": 0,
"Image": 1,
"Video": 2
}
class Window(QWidget):
def __init__(self, width=800, height=600):
super().__init__()
self.setWindowTitle('Recon App')
layout = QGridLayout()
image_frame = QFrame(self)
layout.addWidget(image_frame, 0, 0, 2, 2)
self.image_label = QLabel(image_frame)
self.image_label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
image_frame_layout = QVBoxLayout()
image_frame_layout.addWidget(self.image_label)
image_frame.setLayout(image_frame_layout)
button_layout = QVBoxLayout()
self.comboBox = QComboBox()
self.comboBox.addItems(['Cam', 'Image', 'Video'])
self.comboBox.currentIndexChanged.connect(self.handle_combobox_change_loadButt)
button_layout.addWidget(self.comboBox)
self.combo_outputType = QComboBox()
self.combo_outputType.addItems(['*CSV', '*JSON'])
self.combo_outputType.currentIndexChanged.connect(self.handle_combobox_change_outfileType)
button_layout.addWidget(self.combo_outputType)
# TODO: add camera sources combobox
self.load_button = QPushButton('Load file')
self.load_button.clicked.connect(self.button_click_Load)
self.load_button.setEnabled(False)
button_layout.addWidget(self.load_button)
self.load_work_button = QPushButton('Load work-file')
self.load_work_button.clicked.connect(self.button_click_Load_workFile)
button_layout.addWidget(self.load_work_button)
self.start_button = QPushButton('Start')
self.start_button.clicked.connect(self.button_click_Start)
button_layout.addWidget(self.start_button)
self.stop_button = QPushButton('Stop')
self.stop_button.clicked.connect(self.button_click_Stop)
button_layout.addWidget(self.stop_button)
self.circle_label = QLabel()
layout.addWidget(self.circle_label)
layout.addLayout(button_layout, 0, 2, 2, 1)
layout.setColumnStretch(0, 2)
layout.setColumnStretch(1, 1)
self.set_circle_color(Qt.red)
self.setLayout(layout)
## ATRIBUTES
self.selected_source = None
self.current_image = None
self.videoCollectionThread = None
self.input_path = None
self.video = None
self.outfile = None
self.session = None
self.output_filetype = "csv"
self.image_width = width
self.image_height = height
self.set_empty_image()
def set_circle_color(self, color):
pixmap = QPixmap(20, 20)
pixmap.fill(Qt.transparent)
painter = QPainter(pixmap)
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(QColor(color))
painter.drawEllipse(0, 0, 20, 20)
painter.end()
self.circle_label.setPixmap(pixmap)
def cam_process(self, vid_source):
self.video, self.outfile, self.session = init_camera(vid_source, self)
if self.video is None or not self.video.isOpened():
print('Warning: unable to open video source: ', self.video)
else:
self.videoCollectionThread = VideoCaptureThread(self, self.video)
self.videoCollectionThread.setTerminationEnabled(True)
self.videoCollectionThread.start()
def updateUi_image(self, image):
pixmap = numpy_to_pixmap(image)
self.image_label.setPixmap(pixmap)
def set_empty_image(self):
empty_image = QImage(self.image_width, self.image_height, QImage.Format_RGB32)
empty_image.fill(Qt.white)
self.image_label.setPixmap(QPixmap.fromImage(empty_image))
def load_image(self):
file_dialog = QFileDialog()
file_path, _ = file_dialog.getOpenFileName(self, 'Choose image', '',
'Images (*.png *.jpg *.jpeg *.gif)')
if file_path:
pixmap = QPixmap(file_path)
self.image_label.setPixmap(pixmap)
def load_video(self):
file_dialog = QFileDialog()
self.input_path, _ = file_dialog.getOpenFileName(self, 'Choose video', '',
'Video (*.mp4 *.flv *.ts *.mts *.avi *.mov)')
def get_file_path(self, is_workfile=False):
self.selected_source = self.comboBox.currentText()
sel_type = None
file_format = None
if not is_workfile:
if self.selected_source == "Video":
sel_type = "video"
file_format = 'Video (*.mp4 *.flv *.ts *.mts *.avi *.mov)'
elif self.selected_source == "Image":
sel_type = "image"
file_format = 'Images (*.png *.jpg *.jpeg *.gif)'
try:
if sel_type is not None:
file_dialog = QFileDialog()
self.input_path, _ = file_dialog.getOpenFileName(self, 'Choose ' + sel_type, '',
file_format)
self.show_info_message("Success!", "Chosen file: " + self.input_path)
except Exception as ex:
print(ex)
else:
sel_type = "workfile"
file_format = 'Text file (*.txt)'
file_dialog = QFileDialog()
path, _ = file_dialog.getOpenFileName(self, 'Choose ' + sel_type, '',
file_format)
self.convertWorkfile_toJson(path)
def convertWorkfile_toJson(self, path):
try:
session = Path(path).stem
build_log(self)
self.show_info_message("Success!", "Operation completed. Successfully saved collected data.")
except Exception as ex:
self.show_info_message("Something went wrong!", str(ex))
pass
def button_click_Load(self):
self.get_file_path()
def button_click_Load_workFile(self):
self.get_file_path(is_workfile=True)
def show_info_message(self, title: str, message: str):
dialog = QMessageBox(self)
dialog.setWindowTitle(title)
dialog.setText(message)
dialog.setIcon(QMessageBox.Information)
dialog.exec_()
def button_click_Stop(self):
if self.videoCollectionThread is not None:
self.videoCollectionThread.terminate()
self.set_circle_color(Qt.red)
self.video.release()
self.outfile.release()
try:
build_log(self)
self.show_info_message("Success!", "Operation completed. Successfully saved collected data.")
except Exception as ex:
self.show_info_message("Something went wrong!", str(ex))
pass
def button_click_Start(self):
self.selected_source = self.comboBox.currentText()
self.set_circle_color(Qt.green)
selection = self.selected_source
source = None
if selection in sourceDict.keys():
index = sourceDict.get(selection)
if index > 0:
source = self.input_path
else:
source = 0
if source is not None:
if self.videoCollectionThread is None or not self.videoCollectionThread.isRunning():
self.cam_process(source)
def handle_combobox_change_loadButt(self, index):
if index == 0:
self.load_button.setEnabled(False)
else:
self.load_button.setEnabled(True)
def handle_combobox_change_outfileType(self, index):
if index == 0:
self.output_filetype = "csv"
else:
self.output_filetype = "json"
class VideoCaptureThread(QThread):
def __init__(self, relative_app: QWidget, video: cv2.VideoCapture, *args, **kwargs):
QThread.__init__(self, *args, **kwargs)
self.dataCollectionTimer = QTimer()
self.dataCollectionTimer.moveToThread(self)
self.dataCollectionTimer.setSingleShot(True)
self.frame = None
self.dataCollectionTimer.timeout.connect(lambda: video_loop(video, relative_app, self))
def run(self):
self.dataCollectionTimer.start()
loop = QEventLoop()
loop.exec_()