Python+OpenCV+PyQt5+多线程实现桌面监控程序

需要自行安装opencv,复制即用

其余废话少说,上代码

######################################################
# PyCamera.py                                        #
# Copyright (c) 2021 By LiuHui. All Rights Reserved. #
#                                                    #
# Email: [email protected]                           #
######################################################

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QCoreApplication, QThread, QObject, pyqtSignal
import cv2
import sys
import window
import math
import numpy as np
import time
import os

'''PyQt5窗口页面'''
class Ui_MainWindow(QtWidgets.QWidget):
    
    '''窗口初始化'''
    def __init__(self, window):
        super().__init__(window)
        self.MainWindow = window #定义窗口控件
        self.threads = [] #定义多线程数组
        self.caps = [] #定义摄像头数组
        self.labels = [] #定义摄像头显示位置
        self.timers = [] #定义定时器, 用于控制显示帧率
        self.setupUi() #定义窗口页面布局

        
    '''设置页面布局'''    
    def setupUi(self): 
        self.MainWindow.setObjectName("MainWindow") #窗口控件名称
        self.MainWindow.resize(800, 600) #窗口控件尺寸
        
        self.centralwidget = QtWidgets.QWidget(self.MainWindow) #设置底层控件
        self.centralwidget.setObjectName("centralwidget") #设置底层控件名称
        
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) #设置网格布局控件
        self.gridLayout.setObjectName("gridLayout") #设置网格控件名称
        
        self.MainWindow.setCentralWidget(self.centralwidget) #绑定窗口底层控件
        
        self.statusbar = QtWidgets.QStatusBar(self.MainWindow) #添加状态栏
        self.statusbar.setObjectName("statusbar")
        self.MainWindow.setStatusBar(self.statusbar)
        
        self.action = QtWidgets.QAction(self.MainWindow) #添加菜单动作
        self.action.setObjectName("action")
        self.action_2 = QtWidgets.QAction(self.MainWindow) #添加菜单动作
        self.action_2.setObjectName("action_2")
        
        self.menubar = QtWidgets.QMenuBar(self.MainWindow) #添加菜单栏
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        
        self.menu = QtWidgets.QMenu(self.menubar) #添加菜单事件
        self.menu.setObjectName("menu")
        self.menu.addAction(self.action)
        self.menu.addAction(self.action_2)
        
        self.menubar.addAction(self.menu.menuAction()) #菜单栏添加事件
        self.MainWindow.setMenuBar(self.menubar)
        self.retranslateUi() #设置控件文本
        QtCore.QMetaObject.connectSlotsByName(self.MainWindow)
    
    '''设置控件显示文本'''
    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.MainWindow.setWindowTitle(_translate("MainWindow", "智慧视频"))
        self.menu.setTitle(_translate("MainWindow", "设置"))
        
        self.action.setText(_translate("MainWindow", "摄像头"))
        self.action.triggered.connect(self.refreshCamera)
        
        self.action_2.setText(_translate("MainWindow", "关闭"))
        self.action_2.triggered.connect(self.close_Window)
        
    '''扫码已有摄像头,返回编号'''
    def GetUsbCameraIO(self):
        ids = []
        for i in range(10):
            camera = cv2.VideoCapture(i)
            if camera.isOpened():
                ids.append(i)
        return ids
    
    '''关闭窗口'''
    def close_Window(self):
        for thread in self.threads:
            thread.quit()
        self.MainWindow.close()
    
    '''刷新摄像头布局'''
    def refreshCamera(self):
        #停止现有的多线程
        for thread in self.threads:
            thread.quit()
        self.ids = self.GetUsbCameraIO() #获取当前所有可连接摄像头的id

        ids = np.array(self.ids) #将数组转换为矩阵
        n = len(ids) #获取矩阵长度
        cols = math.ceil(math.sqrt(n)) #取矩阵开方下的最接近整数,向下取整
        rows = math.floor(math.sqrt(n)) #取矩阵开方下的最接近整数,向上取整
        if cols*rows

程序使用效果

Python+OpenCV+PyQt5+多线程实现桌面监控程序_第1张图片

你可能感兴趣的:(python,python,opencv,pyqt5,多线程,监控程序)