我想在Python中使用PyQt5构建一个应用程序,但是我遇到了一些错误,我试图解决很多次,但我不能。
我想先打开相机屏幕。在屏幕中,我们将看到我们的网络摄像头屏幕,底部会有2个按钮(开始和捕获)。当我按下Start时,摄像头将打开,如果我按下Capture按钮,我想拍摄快照。
我在这里构建了工作代码。
按下捕获按钮并拍摄快照后,我想更改屏幕以进入第二屏幕(或清理主屏幕),它将在中心显示一个基本的“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文件
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(525, 386)
self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.image_label = QtWidgets.QLabel(Form)
self.image_label.setText("")
self.image_label.setObjectName("image_label")
self.verticalLayout.addWidget(self.image_label)
self.control_bt = QtWidgets.QPushButton(Form)
self.control_bt.setObjectName("control_bt")
self.verticalLayout.addWidget(self.control_bt)
self.capture = QtWidgets.QPushButton(Form)
self.capture.setObjectName("capture")
self.verticalLayout.addWidget(self.capture)
self.horizontalLayout.addLayout(self.verticalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Cam view"))
self.control_bt.setText(_translate("Form", "Start"))
self.capture.setText(_translate("Form", "Capture"))
当我运行此代码时,它显示;
AttributeError:'video'对象没有属性'setCentralWidget'
试试吧:
卖弄.朋友
import os
import cv2
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets # uic
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget,
QLabel, QVBoxLayout) # +++
from test2_ui import Ui_Form # +++
class video (QtWidgets.QDialog, Ui_Form):
def __init__(self):
super().__init__()
# uic.loadUi('test2.ui',self) # ---
self.setupUi(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.cap = None # -capture <-> +cap
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.cap is None:
self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
self.timer.start()
@QtCore.pyqtSlot()
def update_frame(self):
ret, image = self.cap.read()
simage = cv2.flip(image, 1)
self.displayImage(image, True)
@QtCore.pyqtSlot()
def capture_image(self):
flag, frame = self.cap.read()
path = r'D:\_Qt\Test\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()
### +++ vvv
self.Window.ToolsBTN.clicked.connect(self.goWindow1)
self.hide()
self.Window.show()
def goWindow1(self):
self.show()
self.Window.hide()
### +++ ^^^
class UIWindow(QWidget):
def __init__(self, parent=None):
super(UIWindow, self).__init__(parent)
self.resize(300, 300)
self.label = QLabel("Hello World", alignment=QtCore.Qt.AlignCenter)
self.ToolsBTN = QPushButton('text')
# self.ToolsBTN.move(50, 350)
self.v_box = QVBoxLayout()
self.v_box.addWidget(self.label)
self.v_box.addWidget(self.ToolsBTN)
self.setLayout(self.v_box)
if __name__=='__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = video()
window.setWindowTitle('main code')
window.show()
sys.exit(app.exec_())
test2_UI.朋友
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(525, 386)
self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.image_label = QtWidgets.QLabel(Form)
self.image_label.setText("")
self.image_label.setObjectName("image_label")
self.verticalLayout.addWidget(self.image_label)
self.control_bt = QtWidgets.QPushButton(Form)
self.control_bt.setObjectName("control_bt")
self.verticalLayout.addWidget(self.control_bt)
self.capture = QtWidgets.QPushButton(Form)
self.capture.setObjectName("capture")
self.verticalLayout.addWidget(self.capture)
self.horizontalLayout.addLayout(self.verticalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Cam view"))
self.control_bt.setText(_translate("Form", "Start"))
self.capture.setText(_translate("Form", "Capture"))