Python Open Cv Pyqt5

时间:2018-11-03 15:24:46

标签: python opencv pyqt5

嗨,我正在用Python Opencv Pyqt开发一个项目。启动相机识别的应用程序可启动和停止相机。但是我无法使用Qlabelin代码打开相机,因此我想知道如何解决此错误。

enter image description here

我要照此图片所示去做 我该怎么办?

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_())

0 个答案:

没有答案