-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
298 lines (231 loc) · 10.4 KB
/
main.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
from PyQt6 import QtGui, uic
from PyQt6.QtWidgets import QWidget, QApplication, QTableWidgetItem, QMessageBox
from PyQt6.QtGui import QPixmap, QDesktopServices
import cv2
import sys
from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QThread, QUrl
import numpy as np
import time
from pyzbar import pyzbar
from PyQt6.QtWebEngineWidgets import QWebEngineView
import requests
import serial
import serial.tools.list_ports
from cvthread import CVThread
from dropper import DropperConnectionThread
class Ui(QWidget):
def __init__(self):
super(Ui, self).__init__() # Call the inherited classes __init__ method
uic.loadUi('interface.ui', self) # Load the .ui file
self.show() # Show the GUI
self.setWindowTitle("SEAL Challenge")
self.disply_width = 1280
self.display_height = 600
self.qrCodes = []
self.previousCapture = None
self.startCV.clicked.connect(self.startCVHandler)
# connect the table selection changed signal to the on_qrTable_itemSelectionChanged slot
self.qrTable.itemSelectionChanged.connect(self.on_qrTable_itemSelectionChanged)
#qr table double click
self.qrTable.doubleClicked.connect(self.on_qrTable_doubleClicked)
self.tabWidget.currentChanged.connect(self.onTabChange)
# browser
self.browser = QWebEngineView()
self.browser.setUrl(QUrl("https://www.example.com"))
self.browser.loadStarted.connect(self.loadStartedHandler)
self.browser.loadProgress.connect(self.loadProgressHandler)
self.browser.loadFinished.connect(self.loadFinishedHandler)
#set the self.webBrowser QWidget to the browser
self.browserLayout.addWidget(self.browser)
self.browser.hide()
self.browserProgressBar.hide()
self.clearTableButton.clicked.connect(self.clearTable)
self.dropLeftButton.clicked.connect(self.dropLeft)
self.dropRightButton.clicked.connect(self.dropRight)
self.dropCenterButton.clicked.connect(self.dropCenter)
# generate 1920x1080 black openCV image
blankImage = np.zeros((1080, 1920, 3), np.uint8)
self.update_image(blankImage, "live")
self.update_image(blankImage, "detection")
# get live mouse position on the live image
self.liveView.mouseMoveEvent = self.qrDetectMouse
self.liveView.setMouseTracking(True)
#get updates from slider named "bbox_slider"
self.bbox_slider.valueChanged.connect(self.bbox_slider_changed)
# disable window resizing
# self.setFixedSize(800,600)
self.openinbrowser.clicked.connect(self.open_in_browser)
self.refreshSerial.clicked.connect(self.update_serial_ports)
self.update_serial_ports()
self.connectSerial.clicked.connect(self.connect_disconnect_dropper)
self.dropper = None
self.dropperStatus.setText("Wireless: Disconnected")
#set red text
self.dropperStatus.setStyleSheet("QLabel { color : red; }")
def qrDetectMouse(self, event):
if hasattr(self, 'cv_thread') and type(self.cv_thread) == CVThread and self.cv_thread.isRunning():
self.cv_thread.update_mouse(event.pos().x(), event.pos().y())
def bbox_slider_changed(self):
if hasattr(self, 'cv_thread') and type(self.cv_thread) == CVThread and self.cv_thread.isRunning():
self.cv_thread.update_bbox(self.bbox_slider.value())
def wheelEvent(self,event):
delta = int(event.angleDelta().y()*.25)
# increase slider value
self.bbox_slider.setValue(self.bbox_slider.value() + delta)
def startCVHandler(self):
inText = self.cameraSource.text()
if inText == "":
self.cameraSource.setText("0")
inText = "0"
try:
inText = int(inText)
except:
pass
# check if there is already a thread running
if hasattr(self, 'cv_thread') and type(self.cv_thread) == CVThread and self.cv_thread.isRunning():
self.startCV.setText("Start")
# if there is, terminate it
self.cv_thread.endCV()
# wait for the thread to finish
self.cv_thread.wait()
else:
# create the video capture thread
self.cv_thread = CVThread(inText, self.liveView.width(), self.liveView.height())
# connect its signal to the update_image slot
self.cv_thread.live_signal.connect(self.update_image)
self.cv_thread.found_code_signal.connect(self.update_detected)
# start the thread
self.cv_thread.start()
self.startCV.setText("Stop")
def clearTable(self):
self.qrTable.clearContents()
self.qrTable.setRowCount(0)
self.qrCodes = []
self.browser.hide()
self.browserHelp.show()
def dropLeft(self):
if self.dropper != None:
self.dropper.left()
def dropCenter(self):
if self.dropper != None:
self.dropper.center()
def dropRight(self):
if self.dropper != None:
self.dropper.right()
def loadProgressHandler(self, progress):
self.browserProgressBar.setValue(progress)
def loadStartedHandler(self):
# if web browser is visible, show the progress bar
if self.browser.isVisible():
self.browserProgressBar.show()
def loadFinishedHandler(self):
self.browserProgressBar.setValue(0)
@pyqtSlot(bool, bool)
def updateDropperConnectionStatus(self, statusSerial, statusDropper):
if statusSerial:
self.connectSerial.setText("Disconnect")
else:
self.connectSerial.setText("Connect")
if statusDropper:
self.dropperStatus.setText("Wireless: Connected")
#set green text
self.dropperStatus.setStyleSheet("QLabel { color : green; }")
else:
self.dropperStatus.setText("Wireless: Disconnected")
#set red text
self.dropperStatus.setStyleSheet("QLabel { color : red; }")
# slot for when tab changed
def onTabChange(self):
# if there is a selection
if self.qrTable.selectedItems():
# get the selected row
row = self.qrTable.selectedItems()[0].row()
self.qrImage.setPixmap(self.convert_cv_qt(self.qrCodes[row].img, self.qrImage.width()))
# slot for when table selection changed
@pyqtSlot(np.ndarray, str)
def update_image(self, cv_img, target):
"""Updates the image_label with a new opencv image"""
qt_img = self.convert_cv_qt(cv_img)
if target == "live":
self.liveView.setPixmap(qt_img)
def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format.Format_RGB888)
#scale to fit inside the liveView widget
p = convert_to_Qt_format.scaled(self.liveView.width(), self.liveView.height(), Qt.AspectRatioMode.KeepAspectRatio)
# update liveView width
self.liveView.setFixedWidth(p.width())
if hasattr(self, 'cv_thread') and type(self.cv_thread) == CVThread and self.cv_thread.isRunning():
self.cv_thread.window_size = (p.width(), p.height())
return QPixmap.fromImage(p)
def updateTable(self):
self.qrTable.setRowCount(len(self.qrCodes))
for i, qr in enumerate(self.qrCodes):
self.qrTable.setItem(i, 0, QTableWidgetItem("No"))
self.qrTable.setItem(i, 1, QTableWidgetItem(qr.data))
def update_current_detected(self, foundCodes):
for i, code in enumerate(self.qrCodes):
if code not in foundCodes:
if code.consecutiveNotSeen > 10:
self.qrTable.setItem(i, 0, QTableWidgetItem("No"))
else:
self.qrTable.setItem(i, 0, QTableWidgetItem("Yes"))
else:
code.consecutiveNotSeen = 0
self.qrTable.setItem(i, 0, QTableWidgetItem("Yes"))
@pyqtSlot(list)
def update_detected(self, codes):
if codes != self.previousCapture:
self.previousCapture = codes
for code in codes:
if code not in self.qrCodes:
self.qrCodes.append(code)
self.updateTable()
self.update_current_detected(codes)
for i, code in enumerate(self.qrCodes):
if code not in codes:
code.consecutiveNotSeen += 1
if code.consecutiveNotSeen > 10:
self.update_current_detected(codes)
else:
code.consecutiveNotSeen = 0
def open_in_browser(self):
if len(self.qrTable.selectedItems()) > 0:
row = self.qrTable.selectedItems()[0].row()
QDesktopServices.openUrl(QUrl(self.qrCodes[row].data))
#when item is selected in qrTable
def on_qrTable_itemSelectionChanged(self):
if len(self.qrTable.selectedItems()) > 0:
row = self.qrTable.selectedItems()[0].row()
self.browser.setUrl(QUrl(self.qrCodes[row].data))
self.browser.show()
self.browserHelp.hide()
self.qrImage.setPixmap(self.convert_cv_qt(self.qrCodes[row].img, self.qrImage.width()))
self.openinbrowser.setEnabled(True)
def update_serial_ports(self):
self.serialPorts.clear()
# get list of serial ports
ports = serial.tools.list_ports.comports()
# add each port to the list
for port in ports:
self.serialPorts.addItem(port.device)
def connect_disconnect_dropper(self):
if self.dropper is None:
self.dropper = DropperConnectionThread(self.serialPorts.currentText())
self.dropper.connectionStatusSignal.connect(self.updateDropperConnectionStatus)
self.dropper.start()
else:
self.dropper.disconnect()
self.dropper = None
def on_qrTable_doubleClicked(self):
# show alert with qr code data
if len(self.qrTable.selectedItems()) > 0:
row = self.qrTable.selectedItems()[0].row()
QMessageBox.information(self, "QR Code Data", self.qrCodes[row].data)
if __name__=="__main__":
app = QApplication(sys.argv)
a = Ui()
sys.exit(app.exec())