import sys
from sys import argv,exit
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import time
import cv2
import numpy as np
import Camera
class MainCode(QMainWindow,Camera.Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
Camera.Ui_MainWindow.__init__(self)
self.setupUi(self)
#定时器
self.timer_camera = QtCore.QTimer()
#准备获取图像
self.cap = cv2.VideoCapture()
self.CAM_NUM = 0
self.pushButton_open.clicked.connect(self.button_open_camera_click)
self.pushButton_close.clicked.connect(self.closeEvent)
self.pushButton_take_2.clicked.connect(self.takePhoto_2)
def button_open_camera_click(self):
if self.timer_camera.isActive() == False:
flag = self.cap.open(self.CAM_NUM, cv2.CAP_DSHOW)
if flag == False:
msg = QtWidgets.QMessageBox.warning(
self, u"Warning", u"请检测相机与电脑是否连接正确",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
else:
self.timer_camera.start(30)
def takePhoto_2(self):
if self.timer_camera.isActive() != False:
while True:
flag, frame = self.cap.read()
show = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
self.label_face_6.setPixmap(QtGui.QPixmap.fromImage(showImage))
self.label_face_6.setScaledContents(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
md=MainCode()
md.show()
sys.exit(app.exec_())
Camera.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Camera1.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowModality(QtCore.Qt.NonModal)
MainWindow.resize(765, 645)
MainWindow.setMinimumSize(QtCore.QSize(765, 645))
MainWindow.setMaximumSize(QtCore.QSize(16777215, 16777215))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/newPrefix/pic/pai.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
MainWindow.setToolTip("")
MainWindow.setAutoFillBackground(False)
MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_open = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_open.setGeometry(QtCore.QRect(90, 380, 120, 40))
self.pushButton_open.setMinimumSize(QtCore.QSize(100, 40))
self.pushButton_open.setMaximumSize(QtCore.QSize(120, 40))
font = QtGui.QFont()
font.setFamily("华文彩云")
font.setPointSize(12)
self.pushButton_open.setFont(font)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(":/newPrefix/pic/g1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_open.setIcon(icon1)
self.pushButton_open.setObjectName("pushButton_open")
self.pushButton_take_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_take_2.setGeometry(QtCore.QRect(270, 380, 100, 40))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_take_2.sizePolicy().hasHeightForWidth())
self.pushButton_take_2.setSizePolicy(sizePolicy)
self.pushButton_take_2.setMinimumSize(QtCore.QSize(100, 40))
self.pushButton_take_2.setMaximumSize(QtCore.QSize(100, 40))
font = QtGui.QFont()
font.setFamily("华文彩云")
font.setPointSize(12)
self.pushButton_take_2.setFont(font)
self.pushButton_take_2.setText("")
self.pushButton_take_2.setIcon(icon)
self.pushButton_take_2.setObjectName("pushButton_take_2")
self.label_face_6 = QtWidgets.QLabel(self.centralwidget)
self.label_face_6.setGeometry(QtCore.QRect(20, 60, 431, 300))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_face_6.sizePolicy().hasHeightForWidth())
self.label_face_6.setSizePolicy(sizePolicy)
self.label_face_6.setMinimumSize(QtCore.QSize(0, 0))
self.label_face_6.setMaximumSize(QtCore.QSize(500, 300))
font = QtGui.QFont()
font.setFamily("楷体")
font.setPointSize(16)
self.label_face_6.setFont(font)
self.label_face_6.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_face_6.setStyleSheet("background-color: rgb(192, 218, 255);")
self.label_face_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_face_6.setObjectName("label_face_6")
MainWindow.setCentralWidget(self.centralwidget)
self.actionGoogle_Translate = QtWidgets.QAction(MainWindow)
self.actionGoogle_Translate.setObjectName("actionGoogle_Translate")
self.actionHTML_type = QtWidgets.QAction(MainWindow)
self.actionHTML_type.setObjectName("actionHTML_type")
self.actionsoftware_version = QtWidgets.QAction(MainWindow)
self.actionsoftware_version.setObjectName("actionsoftware_version")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "铝皮除胶(窝牛)"))
self.pushButton_open.setToolTip(_translate("MainWindow", "点击打开摄像头"))
self.pushButton_open.setText(_translate("MainWindow", "打开摄像头"))
self.pushButton_take_2.setToolTip(_translate("MainWindow", "点击拍照"))
self.label_face_6.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><br/></p></body></html>"))
self.actionGoogle_Translate.setText(_translate("MainWindow", "Google Translate"))
self.actionHTML_type.setText(_translate("MainWindow", "HTML type"))
self.actionsoftware_version.setText(_translate("MainWindow", "software version"))
import icon_rc