1.效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8558b1bb83036e099a31cfa48f8ced29.png)
2.Camera.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(1920,1080)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 20, 1910, 900))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.label_2 = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(20)
self.label_2.setFont(font)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.widget1 = QtWidgets.QWidget(self.centralwidget)
self.widget1.setGeometry(QtCore.QRect(600, 920, 631, 51))
self.widget1.setObjectName("widget1")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget1)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton = QtWidgets.QPushButton(self.widget1)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_2.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.widget1)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(self.widget1)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_2.addWidget(self.pushButton_3)
self.pushButton_4 = QtWidgets.QPushButton(self.widget1)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_2.addWidget(self.pushButton_4)
mainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "MainWindow"))
self.label.setText(_translate("mainWindow", "摄像头"))
self.label_2.setText(_translate("mainWindow", "采集图像"))
self.pushButton.setText(_translate("mainWindow", "打开摄像头"))
self.pushButton_2.setText(_translate("mainWindow", "采集图像"))
self.pushButton_3.setText(_translate("mainWindow", "关闭摄像头"))
self.pushButton_4.setText(_translate("mainWindow", "打开图像"))
3.Camera_main.py
import sys
from PyQt5 import QtGui, QtWidgets
from Camera import Ui_mainWindow
import cv2
import time
from PyQt5.Qt import *
class Open_Camera(QtWidgets.QMainWindow, Ui_mainWindow):
def __init__(self):
super(Open_Camera, self).__init__()
self.setupUi(self)
self.init()
self.cap = cv2.VideoCapture()
self.photo_flag = 0
self.label.setScaledContents(True)
self.label_2.setScaledContents(True)
def init(self):
self.camera_timer = QTimer()
self.camera_timer.timeout.connect(self.show_image)
self.pushButton.clicked.connect(self.open_camera)
self.pushButton_2.clicked.connect(self.taking_pictures)
self.pushButton_3.clicked.connect(self.close_camera)
self.pushButton_4.clicked.connect(self.loadphoto)
def open_camera(self):
self.cap = cv2.VideoCapture(0)
self.camera_timer.start(40)
self.show_image()
def show_image(self):
flag, self.image = self.cap.read()
width,height,_ = self.image.shape
ratio1 = width / self.label.width()
ratio2 = height / self.label.height()
ratio = max(ratio1, ratio2)
image_show = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
image_show = cv2.flip(image_show, 1)
self.showImage = QtGui.QImage(image_show.data, height, width, QImage.Format_RGB888)
self.showImage.setDevicePixelRatio(ratio)
self.label.setPixmap(QPixmap.fromImage(self.showImage))
def taking_pictures(self):
if self.cap.isOpened():
FName = fr"images/cap{time.strftime('%Y%m%d%H%M%S', time.localtime())}"
print(FName)
self.label_2.setPixmap(QtGui.QPixmap.fromImage(self.showImage))
self.showImage.save('../back.jpg')
else:
QMessageBox.critical(self, '错误', '摄像头未打开!')
return None
def close_camera(self):
self.camera_timer.stop()
self.cap.release()
self.label.clear()
self.label_2.clear()
self.label.setText("摄像头")
def loadphoto(self):
fname, _ = QFileDialog.getOpenFileName(self, '选择图片', '../', 'Image files(*.jpg *.gif *.png*.bmp)')
self.showImage = fname
self.label_2.setPixmap(QPixmap(self.showImage).scaled(self.label_2.width(), self.label_2.height()))
if __name__ == '__main__':
from PyQt5 import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
ui = Open_Camera()
ui.show()
sys.exit(app.exec_())