forked from Make3DPrintingProjects/Skycam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
236 lines (197 loc) · 7.4 KB
/
gui.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
#!/usr/bin/python3.5
import queue
from PyQt5 import QtCore
import cv2
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QUrl, QThread, QObject, Qt, QPoint
import sys
from PyQt5.QtGui import QKeySequence, QImage, QPixmap, QFont
from PyQt5.QtWidgets import QMainWindow, QWidget, QAction, QTabWidget, QApplication, QBoxLayout, QHBoxLayout, \
QVBoxLayout, QLabel, QGridLayout, QScrollArea, QListWidget, QPushButton,
QInputDialog, QListWidgetItem, QMenu
from flask import Flask, Response
from skycam import Skycam
from streamer import StreamReciever
from tools import StdoutFilter, StderrFilterscript
from functools import partial
class SkycamWidget(QWidget):
def __init__(self, master, cam, streams=[]):
print('initiating skycam widget')
super().__init__()
self.skycam = Skycam(self, cam)
self.master = master
self.master.setCentralWidget(self)
self.streams = dict()
self.th = list()
for i in range(len(streams)):
self.streams[str(i)] = streams[i]
print(self.streams)
self.init_tabwidget()
self.init_streams()
self.init_layout()
self.update_tabs()
print('initiated skycam widget')
def init_layout(self):
hbox = QHBoxLayout()
vbox = QVBoxLayout()
hbox.addWidget(self.tw)
hbox.addLayout(vbox)
self.setLayout(hbox)
def init_tabwidget(self):
self.tw = QTabWidget()
self.tb = self.tw.tabBar()
def update_tabs(self):
for id, stream in self.streams.items():
self.tb.moveTab(self.tw.indexOf(stream.lb), int(id))
def init_streams(self):
for id, stream in self.streams.items():
self.add_stream(id, stream)
def add_stream(self, id, stream):
print('initiating stream', id)
lb = QLabel()
stream.id = id
stream.lb = lb
self.tw.addTab(stream.lb, 'Stream ' + id)
th = QThread()
stream.moveToThread(th)
th.started.connect(stream.run)
stream.thread = th
th.start()
self.streams[id] = stream
stream.newImage.connect(self.update_stream)
@pyqtSlot(QImage, str)
def update_stream(self, frame, id):
pix = QPixmap.fromImage(frame)
pix = pix.scaledToHeight(self.frameGeometry().height() - 150)
for id1, stream in self.streams.items():
if id == id1:
stream.lb.setPixmap(pix)
class MyMainWindow(QMainWindow):
def __init__(self, app, *args, **kwargs):
super().__init__(*args, **kwargs)
self.init_menubar()
self.app = app
self.fs = False
self.settings = None
self.cam = cv2.VideoCapture(0)
self.resize(640, 480)
self.move(self.app.desktop().screen().rect().center() - self.rect().center())
print('showing ...')
self.st = [StreamReciever(self, 'http://localhost:7777'), StreamReciever(self, 'http://96.10.1.168/mjpg/1/video.mjpg')]
self.w = SkycamWidget(self, self.cam, self.st)
self.setWindowTitle('Skycam')
self.show()
def init_menubar(self):
self.bar = self.menuBar()
file = self.bar.addMenu("File")
fullscreen = QAction("Fullsceen", self, checkable=True)
fullscreen.setShortcut(QKeySequence('Ctrl+F'))
fullscreen.setStatusTip("Toggle fullscreen")
fullscreen.triggered.connect(self.fullscreen)
quit = QAction("Quit", self)
quit.setShortcut(QKeySequence('Ctrl+Q'))
quit.setStatusTip('Quit Application')
quit.triggered.connect(self.exit)
settings = QAction("Settings", self)
settings.setShortcut(QKeySequence('Ctrl+S'))
settings.setStatusTip('Open settings menu')
settings.triggered.connect(self.open_settings)
view = self.bar.addMenu("View")
# file.addAction(settings) # Uncomment to add a settings menu, but be warned that it doesn't work
file.addAction(quit)
view.addAction(fullscreen)
@pyqtSlot(object)
def pr(self, s):
print(repr(s))
@pyqtSlot()
def exit(self):
self.cam.release()
self.app.quit()
sys.exit()
@pyqtSlot()
def fullscreen(self):
if self.fs == False:
self.showFullScreen()
self.fs = True
else:
self.showNormal()
self.fs = False
@pyqtSlot()
def open_settings(self):
if self.settings:
self.settings.setWindowState(
self.settings.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
self.settings.activateWindow()
else:
self.settings = Settings(self)
self.settings.setWindowState(
self.settings.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
self.settings.activateWindow()
@pyqtSlot()
def settings_closed(self):
self.settings = None
class Settings(QWidget):
closed = pyqtSignal()
def __init__(self, master, *args, **kwargs):
super().__init__(*args, **kwargs)
self.master = master
self.setWindowTitle('Settings')
self.closed.connect(master.settings_closed)
self.init_layout()
self.show()
def init_layout(self):
vbox = QVBoxLayout()
hbox = QHBoxLayout()
font = QFont()
font.setPointSize(15)
title = QLabel('Streams', self)
title.setFont(font)
title.setAlignment(Qt.AlignCenter)
self.listw = QListWidget()
for stream in self.master.st:
lwi = QListWidgetItem('Stream - ' + stream.address, self.listw)
self.listw.setContextMenuPolicy(Qt.CustomContextMenu)
self.listw.customContextMenuRequested.connect(partial(self.open_menu, stream.id))
scroll = QScrollArea()
scroll.setWidget(self.listw)
vbox.addWidget(title)
vbox.addWidget(scroll)
new = QPushButton("New", self)
new.clicked.connect(self.new_stream)
hbox.addStretch(1)
hbox.addWidget(new)
vbox.addLayout(hbox)
self.setLayout(vbox)
def closeEvent(self, event):
self.closed.emit()
event.accept()
def new_stream(self):
text, ok = QInputDialog.getText(self, 'Address', 'Enter the full url:')
if ok:
sr = StreamReciever(self.master, text)
self.master.w.add_stream(str(len(self.master.w.streams)), sr)
self.master.st.append(sr)
self.listw.addItem('Stream - ' + text)
@pyqtSlot(QPoint)
def open_menu(self, id, QPos):
self.menu = QMenu()
remove = self.menu.addAction('Remove Stream')
remove.triggered.connect(partial(self.remove_stream, id))
pos = self.listw.mapToGlobal(QPoint(0, 0))
self.menu.move(pos + QPos)
self.menu.show()
def remove_stream(self, id):
self.listw.takeItem(self.listw.currentRow())
streams1 = None
for id1, stream in self.master.w.streams.items():
if id == id1:
self.master.w.tw.removeTab(self.master.w.tw.indexOf(stream.lb))
print(stream.id)
print(stream.thread)
stream.stop()
streams1 = {y:x for y, x in self.master.w.streams.items() if x._isrunning}
else:
continue
if streams1:
print(self.master.w.streams)
print(streams1)
self.master.w.streams = streams1