-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_camerafeed_display.py
87 lines (75 loc) · 2.85 KB
/
single_camerafeed_display.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
import sys
import cv2
import numpy as np
from PyQt4 import QtGui, QtCore, Qt
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(823, 602)
self.centralwidget = QtGui.QWidget(MainWindow)
self.videoFrame = QtGui.QLabel(self.centralwidget)
self.videoFrame.setGeometry(QtCore.QRect(40, 32, 721, 521))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 823, 21))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
MainWindow.setStatusBar(self.statusbar)
self.button = QtGui.QPushButton('Quit', MainWindow)
self.button.setGeometry(QtCore.QRect(0, 0, 100, 30))
self.button.clicked.connect(self.close_application)
self.slider = QtGui.QSlider(MainWindow)
self.slider.setRange(0, 3)
self.slider.setValue(0)
self.slider.setGeometry(QtCore.QRect( 0, 250, 40, 100))
def close_application(self):
print("Closing")
sys.exit()
class Video():
def __init__(self,capture):
self.capture = capture
self.currentFrame=np.array([])
def captureNextFrame(self):
"""
capture frame and reverse RBG BGR and return opencv image
"""
ret, readFrame=self.capture.read()
if(ret==True):
self.currentFrame=cv2.cvtColor(readFrame,cv2.COLOR_BGR2RGB)
def convertFrame(self):
""" converts frame to format suitable for QtGui """
try:
height,width=self.currentFrame.shape[:2]
img=QtGui.QImage(self.currentFrame,
width,
height,
QtGui.QImage.Format_RGB888)
img=QtGui.QPixmap.fromImage(img)
self.previousFrame = self.currentFrame
return img
except:
return None
class Gui(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.video = Video(cv2.VideoCapture(0))
self._timer = QtCore.QTimer(self)
self._timer.timeout.connect(self.play)
self._timer.start(27)
self.update()
def play(self):
try:
self.video.captureNextFrame()
self.ui.videoFrame.setPixmap(
self.video.convertFrame())
self.ui.videoFrame.setScaledContents(True)
except TypeError:
print("No frame")
def main():
app = QtGui.QApplication(sys.argv)
ex = Gui()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()