opencv+Qt获取监控画面

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的环境下运行,点击播放会自动调取摄像头的内容,暂停和停止功能暂未实现,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值