I want to build an app with PyQt5 in Python but I got some errors that I tried to solve many times but I can't.
I want to open a camera screen firstly. In screen, we will see our webcam screen and there will 2 buttons at the bottom (Start and Capture). While I pressing Start, webcam will be on and if I press Capture button, I want to take snapshot.
I built the working code until here.
After pressing Capture button and taking snapshot, I want to change the screen in order to go 2nd screen (or clean main screen) and it will just show a basic "Hello World" sentence in the center and there will a button on the bottom.
Here it is my code, where is my fault? Can you fix it?
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_())
I'm adding the .py file converted from "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"))
When I'm running this code, it shows;
AttributeError: 'video' object has no attribute 'setCentralWidget'