Last active
February 26, 2017 07:16
-
-
Save chris-pws/59eb3449a31d607e8af493cd6d07d4a3 to your computer and use it in GitHub Desktop.
Using PyQt4 and OpenCV, runs a thread to suck in a MJPEG stream and display it in a text label. Based on an orphaned solution at http://stackoverflow.com/questions/35103302/what-is-the-best-way-to-display-a-videostream-in-pyqt4#35316662
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import cv2 | |
from PyQt4 import QtGui, QtCore, uic | |
class videoThread(QtCore.QThread): | |
# Requires IP address of streaming server | |
def __init__(self,address): | |
super(videoThread,self).__init__() | |
self.ip = address | |
def run(self): | |
# Create a capture object using the IP address specified at init. | |
cap = cv2.VideoCapture("http://"+ str(self.ip) + | |
":8081/?action=stream?dummy=param.mjpg") | |
while cap.isOpened(): | |
_,frame = cap.read() | |
image = QtGui.QImage(frame.tostring(),1280,720,QtGui.QImage.Format_RGB888) | |
self.emit(QtCore.SIGNAL('newImage(QImage)'), image) | |
class MyWindow(QtGui.QMainWindow): | |
def __init__(self,template): | |
super(MyWindow,self).__init__() | |
uic.loadUi(template,self) | |
self.video = videoThread("192.168.1.150") | |
self.video.start() | |
self.label.connect(self.video,QtCore.SIGNAL('newImage(QImage)'),self.setFrame) | |
self.statusBar().hide() | |
def setFrame(self,frame): | |
pixmap = QtGui.QPixmap.fromImage(frame) | |
self.label.setPixmap(pixmap) | |
if __name__ == '__main__': | |
app = QtGui.QApplication(sys.argv) | |
window = MyWindow('~/PyQt4/template.ui') | |
window.setWindowFlags(QtCore.Qt.FramelessWindowHint) | |
window.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment