利用QT Designer設計一個python的介面, 來練習一些 影像處裡的python 程式.
Use QT Designer to design a python interface to practice some python programs in image processing.
1. 先用 Qt Designer 設計好界面,產生一個 .ui 檔
2. 進 Terminal 輸入以下指令產生可引入的 .py 檔
pyuic5 -x example.ui -o example_ui.py 主程式產生畫面用
2. 進 Terminal 輸入以下指令產生可引入的 .py 檔
pyuic5 -x example.ui -o example_ui.py 主程式產生畫面用
pyuic5 -x tab.ui -o tab.py
Fundamental:
Anaconda3 install Qt Designer
pip install pyqt5
pip install pyqt5-tools
Anaconda3 install Opencv
pip install opencv-python
Python code:
Tab.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'tab.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(894, 639)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 600, 560))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.label_2 = QtWidgets.QLabel(self.tab)
self.label_2.setGeometry(QtCore.QRect(0, 0, 491, 531))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(470, 0, 121, 31))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.tab)
self.pushButton_2.setGeometry(QtCore.QRect(470, 30, 121, 31))
self.pushButton_2.setObjectName("pushButton_2")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.label_3 = QtWidgets.QLabel(self.tab_2)
self.label_3.setGeometry(QtCore.QRect(0, 0, 491, 531))
self.label_3.setObjectName("label_3")
self.pushButton_3 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_3.setGeometry(QtCore.QRect(470, 0, 121, 31))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_4.setGeometry(QtCore.QRect(470, 30, 121, 31))
self.pushButton_4.setObjectName("pushButton_4")
self.tabWidget.addTab(self.tab_2, "")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(610, 0, 191, 560))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 894, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.label.setText(_translate("MainWindow", "TextLabel"))
'''
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
'''
main.py
from WebCam import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
webcam.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QIcon, QPixmap, QPalette, QBrush, QColor, QFontDatabase, QFont
import sys
import cv2
class FrameGrabber(QtCore.QThread):
def __init__(self, parent=None):
super(FrameGrabber, self).__init__(parent)
signal = QtCore.pyqtSignal(QtGui.QImage) # 建立信號物件
def run(self):
cap = cv2.VideoCapture(0)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
#cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
while cap.isOpened():
success, frame = cap.read()
if success:
#image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 色彩轉換
image = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_BGR888)
self.signal.emit(image) # 發送信號
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self, MainWindow):
super().__init__()
self.MainWindow = MainWindow
screen = QtWidgets.QApplication.desktop()
width = screen.width()
height = screen.height()
print(width, height)
line = str(width)+'\n'
line += str(height)
self.setupUi(self.MainWindow)
self.label.setText(line)
data = [
['test', 'gui', 20],
]
#-----------------------------------------------------------------------
#self.insert_data(self.tableWidget, data)
'''
self.grabber = FrameGrabber()
self.grabber.signal.connect(self.updateFrame) # 監聽信號
self.grabber.start()
'''
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(894, 639)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0, 0, 600, 560))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.label_2 = QtWidgets.QLabel(self.tab)
self.label_2.setGeometry(QtCore.QRect(0, 0, 491, 531))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(470, 0, 121, 31))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.videoRun)
self.pushButton_2 = QtWidgets.QPushButton(self.tab)
self.pushButton_2.setGeometry(QtCore.QRect(470, 30, 121, 31))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.quitApp)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.label_3 = QtWidgets.QLabel(self.tab_2)
self.label_3.setGeometry(QtCore.QRect(0, 0, 491, 531))
self.label_3.setObjectName("label_3")
self.pushButton_3 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_3.setGeometry(QtCore.QRect(470, 0, 121, 31))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.tab_2)
self.pushButton_4.setGeometry(QtCore.QRect(470, 30, 121, 31))
self.pushButton_4.setObjectName("pushButton_4")
self.tabWidget.addTab(self.tab_2, "")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(610, 0, 191, 560))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 894, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "OpenVedio"))
self.pushButton_2.setText(_translate("MainWindow", "Quit"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
self.label.setText(_translate("MainWindow", "TextLabel"))
@QtCore.pyqtSlot(QtGui.QImage)
def updateFrame(self, image):
self.label_2.setPixmap(QtGui.QPixmap.fromImage(image))
def insert_data(self, tableWidget, data):
row0 = data[0] if len(data) else []
tableWidget.setRowCount(len(data))
tableWidget.setColumnCount(len(row0))
for r, row in enumerate(data):
for c, item in enumerate(row):
tableWidget.setItem(r, c, QtWidgets.QTableWidgetItem(str(item)))
def quitApp(self):
QtWidgets.QApplication.quit()
'''
line = "line 1"+'\n'
line += "line 2"
self.label.setText(line) #You can't call setText() twice
'''
def videoRun(self):
self.grabber = FrameGrabber()
self.grabber.signal.connect(self.updateFrame) # 監聽信號
self.grabber.start()
YouTube Demo:
沒有留言:
張貼留言