Ask Your Question

Revision history [back]

Python openCv Pyqt5

Hi I am Developing a Project with Python Opencv Pyqt. An application that starts the camera recognition that starts and stops the camera. But I can't turn on the camera in Qlabelin Codes This way I wonder how I can fix this error.

image description

       import cv2
      import sys

     from PyQt5.QtCore import QTimer
     from PyQt5.QtGui import QImage, QPixmap
     from PyQt5.QtWidgets import QApplication, QDialog
     from PyQt5 import QtCore, QtGui, QtWidgets


    class Ui_Yuztanima(object):


def setupUi(self, Yuztanima):

    Yuztanima.setObjectName("Yuztanima")
    Yuztanima.setWindowModality(QtCore.Qt.NonModal)
    Yuztanima.resize(1098, 896)
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap("Giris/Flag_of.png"), QtGui.QIcon.Normal,
                   QtGui.QIcon.Off)
    Yuztanima.setWindowIcon(icon)
    self.centralwidget = QtWidgets.QWidget(Yuztanima)
    self.centralwidget.setObjectName("centralwidget")
    self.imgLabel = QtWidgets.QLabel(self.centralwidget)
    self.imgLabel.setGeometry(QtCore.QRect(240, 80, 640, 480))
    self.imgLabel.setFrameShape(QtWidgets.QFrame.Box)
    self.imgLabel.setText("")
    self.imgLabel.setObjectName("imgLabel")
    self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
    self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 700, 1081, 151))
    self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
    self.Menuduzen_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
    self.Menuduzen_2.setContentsMargins(0, 0, 0, 0)
    self.Menuduzen_2.setObjectName("Menuduzen_2")
    self.startButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
    icon1 = QtGui.QIcon()
    icon1.addPixmap(QtGui.QPixmap("../../Desktop/camera-play-699330.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    self.startButton.setIcon(icon1)
    self.startButton.setIconSize(QtCore.QSize(100, 100))
    self.startButton.setObjectName("startButton")
    self.startButton.clicked.connect(self.start_webcam)
    self.Menuduzen_2.addWidget(self.startButton)
    self.stopButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
    icon2 = QtGui.QIcon()
    icon2.addPixmap(QtGui.QPixmap("../../Desktop/stop_song_red-512.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    self.stopButton.setIcon(icon2)
    self.stopButton.setIconSize(QtCore.QSize(100, 100))
    self.stopButton.setObjectName("stopButton")
    self.stopButton.clicked.connect(self.stop_webcam)
    self.Menuduzen_2.addWidget(self.stopButton)
    self.yuztanimabaslatbutton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
    icon3 = QtGui.QIcon()
    icon3.addPixmap(QtGui.QPixmap("Turkce/YuzTanima.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    self.yuztanimabaslatbutton.setIcon(icon3)
    self.yuztanimabaslatbutton.setIconSize(QtCore.QSize(100, 100))
    self.yuztanimabaslatbutton.setObjectName("yuztanimabaslatbutton")
    self.Menuduzen_2.addWidget(self.yuztanimabaslatbutton)
    self.cikisbutton = QtWidgets.QPushButton(self.horizontalLayoutWidget)
    icon4 = QtGui.QIcon()
    icon4.addPixmap(QtGui.QPixmap("Giris/erase_delete_remove_wipe_out-512.png"), QtGui.QIcon.Normal,
                    QtGui.QIcon.Off)
    self.cikisbutton.setIcon(icon4)
    self.cikisbutton.setIconSize(QtCore.QSize(100, 100))
    self.cikisbutton.setObjectName("cikisbutton")
    self.Menuduzen_2.addWidget(self.cikisbutton)
    Yuztanima.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(Yuztanima)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 1098, 26))
    self.menubar.setObjectName("menubar")
    Yuztanima.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(Yuztanima)
    self.statusbar.setObjectName("statusbar")
    Yuztanima.setStatusBar(self.statusbar)

    self.retranslateUi(Yuztanima)
    self.cikisbutton.clicked.connect(Yuztanima.close)
    QtCore.QMetaObject.connectSlotsByName(Yuztanima)
    self.image = None

def retranslateUi(self, Yuztanima):
    _translate = QtCore.QCoreApplication.translate
    Yuztanima.setWindowTitle(_translate("Yuztanima", "Yuz Tanıma Yazılımı"))
    self.startButton.setText(_translate("Yuztanima", "Kamerayı Başlat"))
    self.stopButton.setText(_translate("Yuztanima", "Kamerayı Durdur"))
    self.yuztanimabaslatbutton.setText(_translate("Yuztanima", "Yuz Tanımayı Başlat"))
    self.cikisbutton.setText(_translate("Yuztanima", "Çıkış"))

def start_webcam(self):

    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 = QTimer(self)
    self.timer.timeout.connect(self.update_frame)
    self.timer.start(5)

def update_frame(self):

    ret, self.image = self.capture.read()
    self.image = cv2.flip(self.image, 1)
    self.displayImage(self.image, 1)

def stop_webcam(self):

    self.timer.stop()

def displayImage(self, img):

    qformat = QImage.Format_Indexed8
    if len(img.shape) == 3:
        if img.shape[2] == 4:
            qformat = QImage.Format_RGBA8888
        else:
            qformat = QImage.Format_RGB888

    outImage = QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
    outImage = outImage.rgbSwapped()

    if Yuztanima == 1:
        self.imgLabel.setPixmap(QPixmap.fromImage(outImage))
        self.imgLabel.setScaledContents(True)
 if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
Yuztanima = QtWidgets.QMainWindow()
ui = Ui_Yuztanima()
ui.setupUi(Yuztanima)
Yuztanima.show()
sys.exit(app.exec_())