我想用Python构建一个PyQt5的应用程序,但是我遇到了一些错误,我试图解决很多次,但都无法解决
我想先打开摄像头屏幕。在屏幕中,我们将看到我们的网络摄像头屏幕,底部将有2个按钮(开始和捕获)。当我按下开始,网络摄像头将打开,如果我按下捕捉按钮,我想拍摄快照。在
在此之前我一直在构建工作代码。在
在按下捕捉按钮并拍摄快照后,我想更改屏幕以转到第二个屏幕(或清洁主屏幕),它只会在中间显示一个基本的“Hello World”句子,底部会有一个按钮。在
这是我的密码,我的错在哪里?你能修好吗?在import os
import cv2
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget
class video (QtWidgets.QDialog):
def __init__(self):
super(video, self).__init__()
uic.loadUi('test2.ui',self)
self.control_bt.clicked.connect(self.start_webcam)
self.capture.clicked.connect(self.capture_image)
self.capture.clicked.connect(self.startUIWindow())
self.image_label.setScaledContents(True)
self.capture = None
self.timer = QtCore.QTimer(self, interval=5)
self.timer.timeout.connect(self.update_frame)
self._image_counter = 0
@QtCore.pyqtSlot()
def start_webcam(self):
if self.capture is None:
self.capture =cv2.VideoCapture(0)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
self.timer.start()
@QtCore.pyqtSlot()
def update_frame(self):
ret, image=self.capture.read()
simage = cv2.flip(image, 1)
self.displayImage(image, True)
@QtCore.pyqtSlot()
def capture_image(self):
flag, frame= self.capture.read()
path = r'D:\Users\ardad\PycharmProjects\testtest'
if flag:
QtWidgets.QApplication.beep()
name = "my_image.jpg"
cv2.imwrite(os.path.join(path, name), frame)
self._image_counter += 1
def displayImage(self, img, window=True):
qformat = QtGui.QImage.Format_Indexed8
if len(img.shape)==3 :
if img.shape[2]==4:
qformat = QtGui.QImage.Format_RGBA8888
else:
qformat = QtGui.QImage.Format_RGB888
outImage = QtGui.QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
outImage = outImage.rgbSwapped()
if window:
self.image_label.setPixmap(QtGui.QPixmap.fromImage(outImage))
def startUIWindow(self):
self.Window = UIWindow(self)
self.setWindowTitle("UIWindow")
self.setCentralWidget(self.Window)
self.show()
class UIWindow(QWidget):
def __init__(self, parent=None):
super(UIWindow, self).__init__(parent)
self.ToolsBTN = QPushButton('text', self)
self.ToolsBTN.move(50, 350)
if __name__=='__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = video()
window.setWindowTitle('main code')
window.show()
sys.exit(app.exec_())
我正在添加从“test2.ui”转换的.py文件
^{pr2}$
当我运行这个代码时,它显示
AttributeError:“video”对象没有属性“setCentralWidget”