• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

opencv和pyqt融合(小例子)

人工智能 一三五 1575次浏览 0个评论

opencv单独打开摄像头或者打开本地视频请参考:《Python+OpenCV教程3:打开摄像头》


本篇介绍在pyqt中打开摄像头并进行显示,直接上代码吧。 运行环境:windows7,python3.6  

效果图:

  opencv和pyqt融合(小例子)  

源码:

 

import time
import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from cv2 import *
from PyQt5.QtCore import QTimer

class VideoBox(QWidget):

    VIDEO_TYPE_OFFLINE = 0
    VIDEO_TYPE_REAL_TIME = 1

    STATUS_INIT = 0
    STATUS_PLAYING = 1
    STATUS_PAUSE = 2

    video_url = ""

    def __init__(self, video_url="", video_type=VIDEO_TYPE_OFFLINE, auto_play=False):
        QWidget.__init__(self)
        self.video_url = video_url
        self.video_type = video_type  # 0: offline  1: realTime
        self.auto_play = auto_play
        self.status = self.STATUS_INIT  # 0: init 1:playing 2: pause

        # 组件展示
        self.pictureLabel = QLabel()
        # init_image = QPixmap("wzf.jpg").scaled(self.width(), self.height())
        # self.pictureLabel.setPixmap(init_image)

        self.playButton = QPushButton()
        self.playButton.setEnabled(True)
        self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
        self.playButton.clicked.connect(self.entry_info)

        control_box = QHBoxLayout()
        control_box.setContentsMargins(0, 0, 0, 0)
        control_box.addWidget(self.playButton)

        layout = QVBoxLayout()
        layout.addWidget(self.pictureLabel)
        layout.addLayout(control_box)

        self.setLayout(layout)

        self.playCapture = VideoCapture(0)
        
    def entry_info(self):
        print('已完成信息录入')
        
        
    def show_video_images(self):
        if self.playCapture.isOpened():
            
            success, frame = self.playCapture.read()
            print('frame.shape:',frame.shape)
            # frame = cvtColor(frame, COLOR_BGR2RGB)
            if success:
                height, width = frame.shape[:2]
                if frame.ndim == 3:
                    rgb = cvtColor(frame, COLOR_BGR2RGB)
                elif frame.ndim == 2:
                    rgb = cvtColor(frame, COLOR_GRAY2BGR)

                temp_image = QImage(rgb.flatten(), width, height, QImage.Format_RGB888)
                temp_pixmap =QPixmap.fromImage(temp_image)
                self.pictureLabel.setPixmap(temp_pixmap)
            else:
                print("read failed, no frame data")
                return
        else:
            print("open file or capturing device error, init again")




if __name__ == "__main__":
    mapp = QApplication(sys.argv)
    mw = VideoBox()

    
    timer = QTimer()
    timer.timeout.connect(mw.show_video_images) #计时结束调用operate()方法
    timer.start(100) #设置计时间隔并启动
    
    mw.show()
    sys.exit(mapp.exec_())

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明opencv和pyqt融合(小例子)
喜欢 (0)

您必须 登录 才能发表评论!

加载中……