Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created September 22, 2017 03:31
Show Gist options
  • Save eyllanesc/8077be4409cc05c6bfe16c08dbc66210 to your computer and use it in GitHub Desktop.
Save eyllanesc/8077be4409cc05c6bfe16c08dbc66210 to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import cv2
class ShowVid(QObject):
signal = pyqtSignal(QImage)
def __init__(self):
super().__init__()
@pyqtSlot()
def startVideo(self):
camera_port = 0
camera = cv2.VideoCapture(camera_port)
run_video = True
while run_video:
ret, image = camera.read()
if ret:
color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
height, width, _ = color_swapped_image.shape
qt_image = QImage(color_swapped_image.data,
width,
height,
color_swapped_image.strides[0],
QImage.Format_RGB888)
self.signal.emit(qt_image)
def stop(self):
self.run_video = True
self.quit()
self.wait()
class VidViewer(QWidget):
def __init__(self):
super().__init__()
self.layout = QGridLayout()
self.image = QImage()
self.initUI()
def paintEvent(self, _):
print("paint image")
if not self.image.isNull():
painter = QPainter(self)
painter.drawImage(0, 0, self.image)
# self.paintImage = QImage()
def initUI(self):
print("INIT UI")
#image = self.image
self.push_button = QPushButton("Start")
self.layout.addWidget(self.push_button, 0, 0,1,1)
self.setLayout(self.layout)
self.setWindowTitle('Test')
self.show()
@pyqtSlot(QImage)
def setImage(self, image):
self.image = image
self.update()
QApplication.processEvents()
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.viewer = VidViewer()
self.vid = ShowVid()
self.viewer.push_button.clicked.connect(self.vid.startVideo)
self.central_widget.addWidget(self.viewer)
self.central_widget.setCurrentWidget(self.viewer)
self.vid.signal.connect(self.viewer.setImage)
self.setGeometry(10,10,1280,720)
self.setFixedSize(1280,720)
def closeEvent(self, event):
self.vid.stop()
QMainWindow.closeEvent(self, event)
app = QApplication(sys.argv)
run = Main()
run.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment