import cv2
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QTimer
from PyQt5 import QtGui
import sys
import numpy
# from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QHBoxLayout, QVBoxLayout)
class Video():
def __init__(self, capture):
self.capture = capture
self.currentFrame = numpy.array([])
# def caputureFrame(self):
# ret,readFrame=self.capture.read()
def captureNextFrame(self):
ret,readFrame=self.capture.read()
if (ret == True):
self.currentFrame=cv2.cvtColor(readFrame,cv2.COLOR_BGR2RGB)
def convertFrame(self):
img2=self.currentFrame
_image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3,
QtGui.QImage.Format_RGB888)
return _image
class Example(QWidget):
def __init__(self):
super().__init__()
self.Init_UI()
self.video = Video(cv2.VideoCapture("这里输入摄像头流地址"))
def Init_UI(self):
self.setGeometry(300,300,800,600)
self.setWindowTitle('cam')
bt1 = QPushButton('播放', self)
bt2 = QPushButton('暂停', self)
bt3 = QPushButton('停止', self)
bt1.clicked.connect(self.time_count)
self.player.setFixedSize(800,400)
self.player.setStyleSheet("QLabel{background:white;}" "QLabel{color:rgb(255,255,255,120);font-size:10px;font-weight:bold;font-family:宋体;}")
# self.player2 = QLabel('qynidayede')
# self.player2.setFixedSize(300,300)
hbox = QHBoxLayout()
hbox.addWidget(bt1)
hbox.addWidget(bt2)
hbox.addWidget(bt3)
hbox1 = QHBoxLayout()
hbox1.addWidget(self.player)
# hbox1.addWidget(self.player2)
vbox = QVBoxLayout()
vbox.addLayout(hbox)
vbox.addLayout(hbox1)
self.setLayout(vbox)
self.show()
def time_count(self):
self.time=QTimer()
self.time.timeout.connect(self.playvideo)
self.time.start(27)
# self.update
def playvideo(self):
self.video.captureNextFrame()
jpg_out = QtGui.QPixmap(self.video.convertFrame()).scaled(self.player.width(), self.player.height())
self.player.setPixmap(jpg_out)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
app.exit(app.exec_())
在ubuntu1604+python3.5+cv2的环境下运行,点击播放会自动调取摄像头的内容,暂停和停止功能暂未实现,