最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pyqt5+opencv?實(shí)現(xiàn)讀取視頻數(shù)據(jù)的方法

 更新時(shí)間:2022年01月25日 14:19:47   作者:郭慶汝  
這篇文章主要介紹了pyqt5+opencv?實(shí)現(xiàn)讀取視頻數(shù)據(jù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、openCV讀取視頻數(shù)據(jù)

import cv2

if __name__ == '__main__':
    videoPath = "./dataSet/3700000000003_13-38-20.055.mp4"
    nameWindow = "Detection window"  # 窗體名稱
    cv2.namedWindow(nameWindow)  # 設(shè)置窗體
    capture = cv2.VideoCapture(videoPath)
    if capture.isOpened():
        size = (capture.get(cv2.CAP_PROP_FRAME_WIDTH), capture.get(cv2.CAP_PROP_FRAME_HEIGHT))  # 讀取幀的寬、高
        speed = capture.get(cv2.CAP_PROP_FPS)  # 獲得幀速
        while True:
            ret, frame = capture.read()
            if ret:
                frame = cv2.resize(frame, (960, 540))
                cv2.imshow(nameWindow, frame)
                if cv2.waitKey(1) & 0xFF == 27:
                    break
            else:
                break
        capture.release()
        cv2.destroyAllWindows()
    else:
        print("攝像頭或視頻讀取失敗")

2、openCV集成pyqt5讀取視頻數(shù)據(jù)

import cv2
import numpy as np
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Video():
    def __init__(self, capture):
        self.capture = capture
        self.currentFrame = np.array([])
    def captureNextFrame(self):
        ret, readFrame = self.capture.read()
        if (ret == True):
            self.currentFrame = cv2.resize(readFrame, (960, 540))
    def convertFrame(self):
        try:
            height, width, channel = self.currentFrame.shape
            bytesPerLine = 3 * width
            qImg = QImage(self.currentFrame.data, width, height, bytesPerLine,
                               QImage.Format_RGB888).rgbSwapped()
            qImg = QPixmap.fromImage(qImg)
            return qImg
        except:
            return None
class win(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.setGeometry(250, 80, 800, 600)  # 從屏幕(250,80)開(kāi)始建立一個(gè)800*600的界面
        self.setWindowTitle('camera')
        self.videoPath = "./dataSet/3700000000003_13-38-20.055.mp4"
        self.video = Video(cv2.VideoCapture(self.videoPath))
        self._timer = QTimer(self)
        self._timer.timeout.connect(self.play)
        self._timer.start(27)
        self.update()
        self.videoFrame = QLabel('VideoCapture')
        self.videoFrame.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(self.videoFrame)          # 設(shè)置圖像數(shù)據(jù)填充控件
    def play(self):
            self.video.captureNextFrame()
            self.videoFrame.setPixmap(self.video.convertFrame())
            self.videoFrame.setScaledContents(True)     # 設(shè)置圖像自動(dòng)填充控件
        except TypeError:
            print('No Frame')
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = win()
    win.show()
    sys.exit(app.exec_())

界面美化版:

import sys
import os
import cv2

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QPalette, QBrush, QPixmap
class Ui_MainWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__(parent)
        self.timer_camera = QtCore.QTimer()  # 初始化定時(shí)器
        self.cap = cv2.VideoCapture()  # 初始化攝像頭
        self.CAM_NUM = r"D:\PycharmProjects\ele_good_pyqt5\dataSet\00.flv"
        self.set_ui()
        self.slot_init()
        self.__flag_work = 0
        self.x = 0
        self.count = 0
    def set_ui(self):
        self.__layout_main = QtWidgets.QHBoxLayout()  # 采用QHBoxLayout類,按照從左到右的順序來(lái)添加控件
        self.__layout_fun_button = QtWidgets.QHBoxLayout()
        self.__layout_data_show = QtWidgets.QVBoxLayout()  # QVBoxLayout類垂直地?cái)[放小部件
        self.button_open_camera = QtWidgets.QPushButton(u'打開(kāi)相機(jī)')
        self.button_close = QtWidgets.QPushButton(u'退出')
        # button顏色修改
        button_color = [self.button_open_camera, self.button_close]
        for i in range(2):
            button_color[i].setStyleSheet("QPushButton{color:black}"
                                           "QPushButton:hover{color:red}"
                                           "QPushButton{background-color:rgb(78,255,255)}"
                                           "QpushButton{border:2px}"
                                           "QPushButton{border_radius:10px}"
                                           "QPushButton{padding:2px 4px}")
        self.button_open_camera.setMinimumHeight(50)
        self.button_close.setMinimumHeight(50)
        # move()方法是移動(dòng)窗口在屏幕上的位置到x = 500,y = 500的位置上
        self.move(500, 500)
        # 信息顯示
        self.label_show_camera = QtWidgets.QLabel()
        self.label_move = QtWidgets.QLabel()
        self.label_move.setFixedSize(100, 100)
        self.label_show_camera.setFixedSize(641, 481)
        self.label_show_camera.setAutoFillBackground(False)
        self.__layout_fun_button.addWidget(self.button_open_camera)
        self.__layout_fun_button.addWidget(self.button_close)
        self.__layout_fun_button.addWidget(self.label_move)
        self.__layout_main.addLayout(self.__layout_fun_button)
        self.__layout_main.addWidget(self.label_show_camera)
        self.setLayout(self.__layout_main)
        self.label_move.raise_()            # 設(shè)置控件在最上層
        self.setWindowTitle(u'攝像頭')
        '''
        # 設(shè)置背景顏色
        palette1 = QPalette()
        palette1.setBrush(self.backgroundRole(),QBrush(QPixmap('background.jpg')))
        self.setPalette(palette1)
    def slot_init(self):  # 建立通信連接
        self.button_open_camera.clicked.connect(self.button_open_camera_click)
        self.timer_camera.timeout.connect(self.show_camera)
        self.button_close.clicked.connect(self.close)
    def button_open_camera_click(self):
        if self.timer_camera.isActive() == False:
            flag = self.cap.open(self.CAM_NUM)      # 打開(kāi)攝像頭操作
            if flag == False:
                msg = QtWidgets.QMessageBox.Warning(self, u'Warning', u'請(qǐng)檢測(cè)相機(jī)與電腦是否連接正確',
                                                    buttons=QtWidgets.QMessageBox.Ok,
                                                    defaultButton=QtWidgets.QMessageBox.Ok)
                # if msg==QtGui.QMessageBox.Cancel:
                #                     pass
            else:
                self.timer_camera.start(30)
                self.button_open_camera.setText(u'關(guān)閉相機(jī)')        # 將控件內(nèi)容設(shè)置為關(guān)閉
        else:
            self.timer_camera.stop()
            self.cap.release()
            self.label_show_camera.clear()
            self.button_open_camera.setText(u'打開(kāi)相機(jī)')
    def show_camera(self):
        flag, self.image = self.cap.read()      # 讀取攝像頭數(shù)據(jù)
        show = cv2.resize(self.image, (640, 480))
        show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
        showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
        self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
    def closeEvent(self, event):
        print("關(guān)閉")
        ok = QtWidgets.QPushButton()
        cancel = QtWidgets.QPushButton()
        msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, u'關(guān)閉', u'是否關(guān)閉!')
        msg.addButton(ok, QtWidgets.QMessageBox.ActionRole)
        msg.addButton(cancel, QtWidgets.QMessageBox.RejectRole)
        ok.setText(u'確定')
        cancel.setText(u'取消')
        if msg.exec_() == QtWidgets.QMessageBox.RejectRole:
            event.ignore()
            if self.cap.isOpened():
                self.cap.release()
            if self.timer_camera.isActive():
                self.timer_camera.stop()
            event.accept()
if __name__ == '__main__':
    App = QApplication(sys.argv)
    win = Ui_MainWindow()
    win.show()
    sys.exit(App.exec_())

顯示效果如下所示:

到此這篇關(guān)于pyqt5+opencv 實(shí)現(xiàn)讀取視頻數(shù)據(jù)的文章就介紹到這了,更多相關(guān)pyqt5 opencv讀取視頻數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 新年快樂(lè)! python實(shí)現(xiàn)絢爛的煙花綻放效果

    新年快樂(lè)! python實(shí)現(xiàn)絢爛的煙花綻放效果

    這篇文章主要為大家詳細(xì)介紹了python利用可視化技巧實(shí)現(xiàn)煙花綻放效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python實(shí)現(xiàn)Excel文件轉(zhuǎn)換為T(mén)XT文件

    python實(shí)現(xiàn)Excel文件轉(zhuǎn)換為T(mén)XT文件

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Excel文件轉(zhuǎn)換為T(mén)XT文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python干貨實(shí)戰(zhàn)之八音符醬小游戲全過(guò)程詳解

    Python干貨實(shí)戰(zhàn)之八音符醬小游戲全過(guò)程詳解

    讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python實(shí)現(xiàn)一個(gè)八音符醬小游戲,大家可以在過(guò)程中查缺補(bǔ)漏,提升水平
    2021-10-10
  • Python+Flask實(shí)現(xiàn)自定義分頁(yè)的示例代碼

    Python+Flask實(shí)現(xiàn)自定義分頁(yè)的示例代碼

    分頁(yè)操作在web開(kāi)發(fā)中幾乎是必不可少的,而flask不像django自帶封裝好的分頁(yè)操作。所以本文將自定義實(shí)現(xiàn)分頁(yè)效果,需要的可以參考一下
    2022-09-09
  • python中super()函數(shù)的理解與基本使用

    python中super()函數(shù)的理解與基本使用

    super( )函數(shù)是用來(lái)調(diào)用父類的一個(gè)方法,super( )函數(shù)還用來(lái)解決多重繼承的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于python中super()函數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Windows下pycharm安裝第三方庫(kù)失敗(通用解決方案)

    Windows下pycharm安裝第三方庫(kù)失敗(通用解決方案)

    這篇文章主要介紹了Windows下pycharm安裝第三方庫(kù)失敗(通用解決方案),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python打開(kāi)url并按指定塊讀取網(wǎng)頁(yè)內(nèi)容的方法

    python打開(kāi)url并按指定塊讀取網(wǎng)頁(yè)內(nèi)容的方法

    這篇文章主要介紹了python打開(kāi)url并按指定塊讀取網(wǎng)頁(yè)內(nèi)容的方法,涉及Python操作URL及網(wǎng)頁(yè)內(nèi)容的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • python正則表達(dá)式匹配IP代碼實(shí)例

    python正則表達(dá)式匹配IP代碼實(shí)例

    這篇文章主要介紹了python正則表達(dá)式匹配IP代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python生成任意范圍任意精度的隨機(jī)數(shù)方法

    Python生成任意范圍任意精度的隨機(jī)數(shù)方法

    下面小編就為大家分享一篇Python生成任意范圍任意精度的隨機(jī)數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Matlab中如何實(shí)現(xiàn)將長(zhǎng)字符串換行寫(xiě)

    Matlab中如何實(shí)現(xiàn)將長(zhǎng)字符串換行寫(xiě)

    這篇文章主要介紹了Matlab中如何實(shí)現(xiàn)將長(zhǎng)字符串換行寫(xiě)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評(píng)論

双城市| 文成县| 岚皋县| 临桂县| 南京市| 水富县| 吴江市| 建瓯市| 博白县| 普定县| 达孜县| 宜川县| 临汾市| 买车| 南投市| 香港| 石家庄市| 余干县| 大埔县| 义马市| 厦门市| 海南省| 日喀则市| 丰原市| 山东| 广南县| 潢川县| 偃师市| 滦南县| 会东县| 郯城县| 宿迁市| 合水县| 台前县| 高雄市| 石阡县| 武义县| 达拉特旗| 黄龙县| 石台县| 澎湖县|