利用opencv打开摄像头,将视频画面显示到QLabel中,通过调整滑动条可以改变摄像头的参数:亮度、对比度、饱和度、曝光度、色调
main.py
import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QMainWindow,QApplication
# from PyQt5.QtWebEngineWidgets import *
from Ui_untitled import Ui_MainWindow
import cv2 #引入CV库
import os
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
#start = pyqtSignal() #发送代理配置
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.cap = cv2.VideoCapture(0)
self.cap.set(6,cv2.VideoWriter.fourcc('M','J','P','G'))
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH,1920) # 宽度
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080) # 高度
def closeEvent(self, event):
self.cap.release()
event.accept() # 表示同意了,结束吧
os._exit(0) # 至此,程序结束,摄像头设备也释放了,安全关闭程序
#从摄像头读视频帧
def setPic(self):
fps = 30
while True:
ret,frame = self.cap.read()
QtImg = self.cvImgtoQtImg(frame) #将帧数据转换为PyQt图像格式
self.label_pic.setPixmap(QtGui.QPixmap.fromImage(QtImg)) #在ImgDisp显示图像
size = QtImg.size()
self.label_pic.resize(size)#根据帧大小调整标签大小
self.label_pic.show() #刷新界面
cv2.waitKey(int(1000/fps)) #休眠一会,确保每秒播放fps帧
# 完成所有操作后,释放捕获器
self.cap.release()
def setLD(self,value):
print(value)
self.cap.set(cv2.CAP_PROP_BRIGHTNESS,value) # 亮度
def setDB(self,value):
print(value)
self.cap.set(cv2.CAP_PROP_CONTRAST,value) # 对比度
def setBH(self,value):
print(value)
self.cap.set(cv2.CAP_PROP_SATURATION,value) # 饱和度
def setBG(self,value):
print(value)
self.cap.set(cv2.CAP_PROP_EXPOSURE,value) # 曝光
def setSD(self,value):
print(value)
self.cap.set(cv2.CAP_PROP_HUE,value) # 色调
def cvImgtoQtImg(self,cvImg): #定义opencv图像转PyQt图像的函数
QtImgBuf = cv2.cvtColor(cvImg, cv2.COLOR_BGR2BGRA)
QtImg = QtGui.QImage(QtImgBuf.data, QtImgBuf.shape[1], QtImgBuf.shape[0], QtGui.QImage.Format_RGB32)
return QtImg
def __main():
app = QApplication(sys.argv)
mw = MyWindow() #视图对象
mw.setStyleSheet("#MainWindow{background-color: rgb(91, 91, 91);}")
#mw.setStyleSheet("#MainWindow{background-color: #BA55D3;}")
mw.verticalSlider_ld.valueChanged.connect(mw.setLD)
mw.verticalSlider_db.valueChanged.connect(mw.setDB)
mw.verticalSlider_bh.valueChanged.connect(mw.setBH)
mw.verticalSlider_bg.valueChanged.connect(mw.setBG)
mw.verticalSlider_sd.valueChanged.connect(mw.setSD)
mw.show()
mw.setPic()
sys.exit(app.exec_())
if __name__=='__main__':
__main()
Ui_untitled.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'd:\All_workspace\vscode_pro\cv\untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1019, 714)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_pic = QtWidgets.QLabel(self.centralwidget)
self.label_pic.setObjectName("label_pic")
self.horizontalLayout_2.addWidget(self.label_pic)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(20)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.verticalSlider_ld = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider_ld.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_ld.setObjectName("verticalSlider_ld")
self.verticalLayout.addWidget(self.verticalSlider_ld)
self.horizontalLayout.addLayout(self.verticalLayout)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(20)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.verticalSlider_db = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider_db.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_db.setObjectName("verticalSlider_db")
self.verticalLayout_2.addWidget(self.verticalSlider_db)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(20)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.verticalLayout_3.addWidget(self.label_3)
self.verticalSlider_bh = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider_bh.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_bh.setObjectName("verticalSlider_bh")
self.verticalLayout_3.addWidget(self.verticalSlider_bh)
self.horizontalLayout.addLayout(self.verticalLayout_3)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(20)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.verticalLayout_4.addWidget(self.label_4)
self.verticalSlider_bg = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider_bg.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_bg.setObjectName("verticalSlider_bg")
self.verticalLayout_4.addWidget(self.verticalSlider_bg)
self.horizontalLayout.addLayout(self.verticalLayout_4)
self.verticalLayout_5 = QtWidgets.QVBoxLayout()
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(20)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.verticalLayout_5.addWidget(self.label_5)
self.verticalSlider_sd = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider_sd.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_sd.setObjectName("verticalSlider_sd")
self.verticalLayout_5.addWidget(self.verticalSlider_sd)
self.horizontalLayout.addLayout(self.verticalLayout_5)
self.horizontalLayout_2.addLayout(self.horizontalLayout)
self.horizontalLayout_2.setStretch(0, 7)
self.horizontalLayout_2.setStretch(1, 3)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1019, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_pic.setText(_translate("MainWindow", "TextLabel"))
self.label.setText(_translate("MainWindow", "亮度"))
self.label_2.setText(_translate("MainWindow", "对比度"))
self.label_3.setText(_translate("MainWindow", "饱和度"))
self.label_4.setText(_translate("MainWindow", "曝光度"))
self.label_5.setText(_translate("MainWindow", "色调"))