Commit d5e9d9e4 authored by Tom Pillot's avatar Tom Pillot
Browse files

Show webcam and detected tags on the scoreboard window

parent c1829a63
......@@ -3,6 +3,8 @@ import cv2
class Detector(apriltag.Detector):
cam = cv2.VideoCapture(0)
def __init__(self):
super().__init__()
self.DECISION_MARGIN = 40
......@@ -14,14 +16,13 @@ class Detector(apriltag.Detector):
return det.tag_id
def detect_from_cam(self):
cam = cv2.VideoCapture(0)
cv2.namedWindow("Detection")
tag_id = None
while not tag_id:
ret, frame = cam.read()
ret, frame = self.cam.read()
if not ret:
print("failed to grab frame")
print("Failed to grab frame.")
break
cv2.imshow("Detection", frame)
......@@ -35,6 +36,21 @@ class Detector(apriltag.Detector):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
tag_id = self.detect(gray)
cam.release()
cv2.destroyAllWindows()
return tag_id
def show_tags_on_image(self, img):
greys = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = super().detect(greys)
for det in dets:
print(dir(det))
print(type(det))
if det.decision_margin >= self.DECISION_MARGIN:
rect = det.corners.astype(int).reshape((-1, 1, 2))
cv2.polylines(img, [rect], True, (0, 0, 255), 2)
ident = str(det.tag_id)
pos = det.center.astype(int) + (-10, 10)
cv2.putText(img, ident, tuple(pos), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
def stop_webcam(self):
self.cam.release()
import cv2
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from game.scoreboard_ui import ScoreWindowUi
from game.detector import Detector
class ScoreWindow(ScoreWindowUi):
WEBCAM_REFRESH_TIME = 50
def __init__(self, teams):
super().__init__()
self.teams = teams
self.time_is_red = False
self.detector = Detector()
def timer_start(self, time):
def start_scoreboard(self, time):
# Start and display the clock
self.time_edit.setTime(time)
self.timer.timeout.connect(self.timer_timeout)
self.timer.start(1000)
self.update_gui()
# Show the webcam
self.webcam_timer.timeout.connect(self.refresh_webcam)
self.webcam_timer.start(self.WEBCAM_REFRESH_TIME)
def timer_timeout(self):
self.timer.start(1000)
......@@ -46,3 +55,18 @@ class ScoreWindow(ScoreWindowUi):
self.table_widget.setItem(row, self.POINTS, qtw.QTableWidgetItem(str(team.points)))
self.table_widget.resizeRowsToContents()
self.table_widget.resizeColumnsToContents()
def refresh_webcam(self):
ret, frame = self.detector.cam.read()
self.detector.show_tags_on_image(frame)
if ret:
# Convert frame to QImage
cv2.cvtColor(frame, cv2.COLOR_RGB2BGR, frame)
rows, cols, _ = frame.shape
qimage = qtg.QImage(frame.data, cols, rows, qtg.QImage.Format_RGB888)
pixmap = qtg.QPixmap()
pixmap.convertFromImage(qimage, qtc.Qt.AutoColor)
self.webcam_label.setPixmap(pixmap)
else:
print("Failed to grab frame.")
self.webcam_timer.start(self.WEBCAM_REFRESH_TIME)
......@@ -14,25 +14,25 @@ class ScoreWindowUi(qtw.QMainWindow):
self.setCentralWidget(widget)
self.time_edit = qtw.QTimeEdit()
self.time_edit.setFont(qtg.QFont("Arial", 80, qtg.QFont.Bold))
self.time_edit.setFont(qtg.QFont("Arial", 100, qtg.QFont.Bold))
self.time_edit.setDisplayFormat("mm:ss")
self.time_edit.setAlignment(qtc.Qt.AlignCenter)
self.time_edit.setReadOnly(True)
self.time_edit.setFixedHeight(200)
with open("game/time_edit.qss", "r") as f:
self.time_edit.setStyleSheet(f.read())
self.list_view = qtw.QListView()
self.webcam_pixmap = qtg.QPixmap("/home/ozon/Pictures/icone.png")
self.webcam_label = qtw.QLabel()
self.webcam_label.setPixmap(self.webcam_pixmap)
self.webcam_timer = qtc.QTimer()
self.table_widget = qtw.QTableWidget(0, 3, self)
self.table_widget.setHorizontalHeaderItem(self.RANK, qtw.QTableWidgetItem("Classement"))
self.table_widget.setHorizontalHeaderItem(self.NAME, qtw.QTableWidgetItem("Nom"))
self.table_widget.setHorizontalHeaderItem(self.POINTS, qtw.QTableWidgetItem("Points"))
# self.table_widget.horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch)
self.table_widget.setFont(qtg.QFont("Arial", 40, qtg.QFont.Bold))
self.table_widget.setEditTriggers(qtw.QAbstractItemView.NoEditTriggers)
self.table_widget.verticalHeader().setVisible(False)
# self.table_widget.setShowGrid(False)
......@@ -47,7 +47,7 @@ class ScoreWindowUi(qtw.QMainWindow):
vlayout1.addWidget(self.webcam_label)
hlayout = qtw.QHBoxLayout()
hlayout.addWidget(self.table_widget)
hlayout.addWidget(self.table_widget, stretch=True)
hlayout.addLayout(vlayout1)
vlayout2 = qtw.QVBoxLayout()
......@@ -56,6 +56,8 @@ class ScoreWindowUi(qtw.QMainWindow):
widget.setLayout(vlayout2)
def resizeEvent(self, a0):
super().resizeEvent(a0)
self.table_widget.setColumnWidth(self.RANK, int(self.table_widget.width() / 6))
self.table_widget.setColumnWidth(self.NAME, int(self.table_widget.width() / 2))
self.table_widget.setColumnWidth(self.POINTS, int(self.table_widget.width() / 3))
self.time_edit.setFixedWidth(self.table_widget.width())
......@@ -17,6 +17,5 @@ QHeaderView::section:vertical {
QTableWidget {
color: #FFFFFF;
font-size: 20pt;
border-radius: 10px;
}
\ No newline at end of file
......@@ -21,7 +21,7 @@ class MainWindow(qtw.QWidget):
if self.team_window.can_start_activity():
self.scoreboard_window.add_teams()
time = self.team_window.time_edit.time()
self.scoreboard_window.timer_start(time)
self.scoreboard_window.start_scoreboard(time)
self.stacked_layout.setCurrentIndex(1)
self.scoreboard_window.showMaximized()
......@@ -34,7 +34,7 @@ if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "test":
for tag_id in w.team_window.team_colors.keys():
tag_str = str(tag_id)
name = "test" + str(tag_id)
name = "Test " + str(tag_id)
w.team_window.name_input.setText(name)
w.team_window.tag_label.setText(tag_str)
w.team_window.add_button.click()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment