项目背景:
有个大学同学在一所重点高中当信息技术老师,最近要参加一个信息技术比赛,自己打算开发一个基于python的机器视觉桌面应用程式软件,便于在比赛的时候进行演示,无奈他自己编程能力有限,只是粗略的规划出了界面功能,并未实现出来,于是向我求救开发。
项目分析:
1.由于是桌面端程式,而自己又比较擅长python,所以选定PyQt5框架开发窗体。
2.针对常用的图像处理部分,选用opencv-python库文件。
3.针对程序中的OCR功能、目标识别功能等,搜集相关的演算法。
界面规划:
部分代码:
# -*- coding: utf-8 -*-
import sys
if hasattr(sys, 'frozen'):
os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from PyQt5 import QtCore
from PyQt5.QtWidgets import (QApplication, QTabWidget, QWidget, QFormLayout, QLineEdit, QGridLayout,
QHBoxLayout, QRadioButton, QLabel, QCheckBox,QVBoxLayout, QPushButton,
QTextEdit, QFileDialog,QMainWindow,)
from PyQt5 import QtGui
from PyQt5.QtGui import QImage, QPixmap
import cv2
fileName_choose = None
class MainUI(QTabWidget):
def __init__(self, parent=None):
super(MainUI, self).__init__(parent)
self.setWindowTitle('机器视觉软件')
self.setFixedSize(960, 640)
self.initUI()
def initUI(self):
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.tab4 = QWidget()
self.addTab(self.tab1, "Tab 1")
self.addTab(self.tab2, "Tab 2")
self.addTab(self.tab3, "Tab 3")
self.addTab(self.tab4, "Tab 4")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.tab4UI()
def tab1UI(self):
self.main1_widget = QWidget()
self.main1_widget.setObjectName('main_widget')
self.main1_layout = QGridLayout()
self.main1_widget.setLayout(self.main1_layout)
# 创建左侧窗体
self.left1_widget = QWidget()
self.left1_widget.setObjectName('left_widget')
self.left1_layout = QGridLayout()
self.left1_widget.setLayout(self.left1_layout)
# 创建右侧窗体
self.right1_widget = QWidget()
self.right1_widget.setObjectName('right_widget')
self.right1_layout = QGridLayout()
self.right1_widget.setLayout(self.right1_layout)
# 在主窗体上分配左右区间
self.main1_layout.addWidget(self.left1_widget, 0, 0, 10, 8)
self.main1_layout.addWidget(self.right1_widget, 0, 8, 10, 2)
# 创建该界面上要显示的控件
self.img1_label1 = QLabel('图像显示!')
self.img1_label1.setStyleSheet('background-color: rgb(128, 128, 128)')
self.button1_1 = QPushButton('读入图片')
self.button1_2 = QPushButton('开始采集/暂停采集')
self.button1_3 = QPushButton('图像放大')
self.button1_4 = QPushButton('图像还原')
self.button1_5 = QPushButton('字符训练')
self.button1_6 = QPushButton('字符识别')
self.button1_7 = QPushButton('识别结果')
self.button1_8 = QPushButton('保存结果')
self.text1_1 = QTextEdit()
# 添加控件
self.left1_layout.addWidget(self.img1_label1, 0, 0, 10, 8)
self.right1_layout.addWidget(self.button1_1, 1, 8, 1, 2)
self.right1_layout.addWidget(self.button1_2, 2, 8, 1, 2)
self.right1_layout.addWidget(self.button1_3, 3, 8, 1, 2)
self.right1_layout.addWidget(self.button1_4, 4, 8, 1, 2)
self.right1_layout.addWidget(self.button1_5, 5, 8, 1, 2)
self.right1_layout.addWidget(self.button1_6, 6, 8, 1, 2)
self.right1_layout.addWidget(self.button1_7, 7, 8, 1, 2)
self.right1_layout.addWidget(self.text1_1, 8, 8, 2, 2)
self.right1_layout.addWidget(self.button1_8, 10, 8, 1, 2)
self.setTabText(0, "常规字符识别")
self.tab1.setLayout(self.main1_layout)
#信号触发
#self.main1_widget.setCentralWidget(self.centralWidget)
#QtCore.QMetaObject.connectSlotsByName(self.main1_widget )
self.button1_1.clicked.connect(self.openfile1)
def tab2UI(self):
self.main2_widget = QWidget()
self.main2_widget.setObjectName('main_widget')
self.main2_layout = QGridLayout()
self.main2_widget.setLayout(self.main2_layout)
# 创建左侧窗体
self.left2_widget = QWidget()
self.left2_widget.setObjectName('left_widget')
self.left2_layout = QGridLayout()
self.left2_widget.setLayout(self.left2_layout)
# 创建右侧窗体
self.right2_widget = QWidget()
self.right2_widget.setObjectName('right_widget')
self.right2_layout = QGridLayout()
self.right2_widget.setLayout(self.right2_layout)
# 在主窗体上分配左右区间
self.main2_layout.addWidget(self.left2_widget, 0, 0, 10, 8)
self.main2_layout.addWidget(self.right2_widget, 0, 8, 10, 2)
# 创建该界面上要显示的控件
self.img2_label1 = QLabel('图像显示!')
self.img2_label1.setStyleSheet('background-color: rgb(128, 128, 255)')
self.button2_1 = QPushButton('读入图片')
self.button2_2 = QPushButton('开始采集/暂停采集')
self.button2_3 = QPushButton('图像放大')
self.button2_4 = QPushButton('图像还原')
self.button2_5 = QPushButton('字符训练')
self.button2_6 = QPushButton('字符识别')
self.button2_7 = QPushButton('识别结果')
self.button2_8 = QPushButton('保存结果')
self.text2_1 = QTextEdit()
# 添加控件
self.left2_layout.addWidget(self.img2_label1, 0, 0, 10, 8)
self.right2_layout.addWidget(self.button2_1, 1, 8, 1, 2)
self.right2_layout.addWidget(self.button2_2, 2, 8, 1, 2)
self.right2_layout.addWidget(self.button2_3, 3, 8, 1, 2)
self.right2_layout.addWidget(self.button2_4, 4, 8, 1, 2)
self.right2_layout.addWidget(self.button2_5, 5, 8, 1, 2)
self.right2_layout.addWidget(self.button2_6, 6, 8, 1, 2)
self.right2_layout.addWidget(self.button2_7, 7, 8, 1, 2)
self.right2_layout.addWidget(self.text2_1, 8, 8, 2, 2)
self.right2_layout.addWidget(self.button2_8, 10, 8, 1, 2)
self.setTabText(1, "手写字符识别")
self.tab2.setLayout(self.main2_layout)
self.button2_1.clicked.connect(self.openfile2)
def tab3UI(self):
# 创建主窗体
self.main3_widget = QWidget()
self.main3_widget.setObjectName('main_widget')
self.main3_layout = QGridLayout()
self.main3_widget.setLayout(self.main3_layout)
# 创建左侧窗体
self.left3_widget = QWidget()
self.left3_widget.setObjectName('left_widget')
self.left3_layout = QGridLayout()
self.left3_widget.setLayout(self.left3_layout)
# 创建右侧窗体
self.right3_widget = QWidget()
self.right3_widget.setObjectName('right_widget')
self.right3_layout = QGridLayout()
self.right3_widget.setLayout(self.right3_layout)
# 在主窗体上分配左右区间
self.main3_layout.addWidget(self.left3_widget, 0, 0, 14, 12)
self.main3_layout.addWidget(self.right3_widget, 0, 14, 14, 2)
# 创建该界面上要显示的控件
self.img3_label1 = QLabel('图像显示!')
self.img3_label1.setStyleSheet('background-color: rgb(170, 255, 255)')
self.button3_1 = QPushButton('读入图片')
self.button3_2 = QPushButton('开始采集/暂停采集')
self.button3_3 = QPushButton('图像放大')
self.button3_4 = QPushButton('图像还原')
self.button3_5 = QPushButton('训练红色')
self.button3_6 = QPushButton('训练绿色')
self.button3_7 = QPushButton('训练蓝色')
self.button3_8 = QPushButton('训练黄色')
self.button3_9 = QPushButton('颜色识别')
self.button3_10 = QPushButton('跟踪识别')
self.button3_11 = QPushButton('识别结果')
self.button3_12 = QPushButton('保存结果')
self.text3_1 = QTextEdit()
# 添加控件
self.left3_layout.addWidget(self.img3_label1, 0, 0, 14, 12)
self.right3_layout.addWidget(self.button3_1, 1, 12, 1, 2)
self.right3_layout.addWidget(self.button3_2, 2, 12, 1, 2)
self.right3_layout.addWidget(self.button3_3, 3, 12, 1, 2)
self.right3_layout.addWidget(self.button3_4, 4, 12, 1, 2)
self.right3_layout.addWidget(self.button3_5, 5, 12, 1, 2)
self.right3_layout.addWidget(self.button3_6, 6, 12, 1, 2)
self.right3_layout.addWidget(self.button3_7, 7, 12, 1, 2)
self.right3_layout.addWidget(self.button3_8, 8, 12, 1, 2)
self.right3_layout.addWidget(self.button3_9, 9, 12, 1, 2)
self.right3_layout.addWidget(self.button3_10, 10, 12, 1, 2)
self.right3_layout.addWidget(self.button3_11, 11, 12, 1, 2)
self.right3_layout.addWidget(self.text3_1, 12, 12, 2, 2)
self.right3_layout.addWidget(self.button3_12, 14, 12, 1, 2)
self.setTabText(2, "颜色识别")
self.tab3.setLayout(self.main3_layout)
self.button3_1.clicked.connect(self.openfile3)
def tab4UI(self):
#创建主窗体
self.main4_widget = QWidget()
self.main4_widget.setObjectName('main_widget')
self.main4_layout = QGridLayout()
self.main4_widget.setLayout(self.main4_layout)
# 创建左侧窗体
self.left4_widget = QWidget()
self.left4_widget.setObjectName('left_widget')
self.left4_layout = QGridLayout()
self.left4_widget.setLayout(self.left4_layout)
# 创建右侧窗体
self.right4_widget = QWidget()
self.right4_widget.setObjectName('right_widget')
self.right4_layout = QGridLayout()
self.right4_widget.setLayout(self.right4_layout)
#在主窗体上分配左右区间
self.main4_layout.addWidget(self.left4_widget, 0, 0, 12, 10)
self.main4_layout.addWidget(self.right4_widget, 0, 10, 12, 4)
# 创建该界面上要显示的控件
self.img4_label1 = QLabel('图像显示!')
self.img4_label1.setStyleSheet('background-color: rgb(85, 170, 127)')
self.button4_1 = QPushButton('读入图片')
self.button4_2 = QPushButton('开始采集/暂停采集')
self.button4_3 = QPushButton('图像放大')
self.button4_4 = QPushButton('图像还原')
self.button4_5 = QPushButton('畸变校正')
self.button4_6 = QPushButton('手部色坐标检测')
self.button4_7 = QPushButton('手部目标提取')
self.button4_8 = QPushButton('手势识别')
self.button4_9 = QPushButton('手势跟踪')
self.button4_10 = QPushButton('保存结果')
self.text4_1 = QTextEdit()
# 添加控件
self.left4_layout.addWidget(self.img4_label1, 0, 0, 10, 10)
self.right4_layout.addWidget(self.button4_1, 1, 10, 1, 2)
self.right4_layout.addWidget(self.button4_2, 2, 10, 1, 2)
self.right4_layout.addWidget(self.button4_3, 3, 10, 1, 2)
self.right4_layout.addWidget(self.button4_4, 4, 10, 1, 2)
self.right4_layout.addWidget(self.button4_5, 5, 10, 1, 2)
self.right4_layout.addWidget(self.button4_6, 6, 10, 1, 2)
self.right4_layout.addWidget(self.button4_7, 7, 10, 1, 2)
self.right4_layout.addWidget(self.button4_8, 8, 10, 1, 2)
self.right4_layout.addWidget(self.button4_9, 9, 10, 1, 2)
self.right4_layout.addWidget(self.text4_1, 10, 10, 1, 2)
self.right4_layout.addWidget(self.button4_10, 11, 10, 1, 2)
# 设定表头
self.setTabText(3, "手势识别")
self.tab4.setLayout(self.main4_layout)
self.button4_1.clicked.connect(self.openfile4)
def openfile1(self):
global fileName_choose
fileName_choose,_= QFileDialog.getOpenFileName(self,
"选取图片",
' ',
"All Files (*);;Image Files (*.png, *.jpg, *.jpeg)")
if fileName_choose == ('', ''):
return
else:
print(fileName_choose)
jpg = QtGui.QPixmap(fileName_choose).scaled(self.img1_label1.width(), self.img1_label1.height())
self.img1_label1.setPixmap(jpg)
def openfile2(self):
global fileName_choose
fileName_choose,_= QFileDialog.getOpenFileName(self,
"选取图片",
' ',
"All Files (*);;Image Files (*.png, *.jpg, *.jpeg)")
if fileName_choose == ('', ''):
return
else:
jpg = QtGui.QPixmap(fileName_choose).scaled(self.img2_label1.width(), self.img2_label1.height())
self.img2_label1.setPixmap(jpg)
def openfile3(self):
fileName_choose,_= QFileDialog.getOpenFileName(self,
"选取图片",
' ',
"All Files (*);;Image Files (*.png, *.jpg, *.jpeg)")
if fileName_choose == ('', ''):
return
else:
jpg = QtGui.QPixmap(fileName_choose).scaled(self.img3_label1.width(), self.img3_label1.height())
self.img3_label1.setPixmap(jpg)
def openfile4(self):
fileName_choose,_= QFileDialog.getOpenFileName(self,
"选取图片",
' ',
"All Files (*);;Image Files (*.png, *.jpg, *.jpeg)")
if fileName_choose == ('', ''):
return
else:
jpg = QtGui.QPixmap(fileName_choose).scaled(self.img4_label1.width(), self.img4_label1.height())
self.img4_label1.setPixmap(jpg)
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MainUI()
myWin.show()
sys.exit(app.exec_())
结果呈现:
打包好的exe程序下载地址:程序下载地址
相关经验分享:
自己在做界面的时候,在界面上打开文件遇到了问题,最后通过查询网络成功找到了合适的代码,在这里分享一下。
代码1(Pyqt5打开文件):
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(386, 127)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.retranslateUi(MainWindow)
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(190, 90, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("打开")
MainWindow.setCentralWidget(self.centralWidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.openfile)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "一颗数据小白菜"))
def openfile(self):
openfile_name = QFileDialog.getOpenFileName(self,'选择文件','','Excel files(*.xlsx , *.xls)')
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_())
import sys
import os
from PyQt5.QtWidgets import *
class MainForm(QWidget):
def __init__(self, name = 'MainForm'):
super(MainForm,self).__init__()
self.setWindowTitle(name)
self.cwd = os.getcwd() # 获取当前程序文件位置
self.resize(300,200) # 设置窗体大小
# btn 1
self.btn_chooseDir = QPushButton(self)
self.btn_chooseDir.setObjectName("btn_chooseDir")
self.btn_chooseDir.setText("选择文件夹")
# btn 2
self.btn_chooseFile = QPushButton(self)
self.btn_chooseFile.setObjectName("btn_chooseFile")
self.btn_chooseFile.setText("选取文件")
# btn 3
self.btn_chooseMutiFile = QPushButton(self)
self.btn_chooseMutiFile.setObjectName("btn_chooseMutiFile")
self.btn_chooseMutiFile.setText("多文件选择")
# btn 4
self.btn_saveFile = QPushButton(self)
self.btn_saveFile.setObjectName("btn_saveFile")
self.btn_saveFile.setText("文件保存")
# 设置布局
layout = QVBoxLayout()
layout.addWidget(self.btn_chooseDir)
layout.addWidget(self.btn_chooseFile)
layout.addWidget(self.btn_chooseMutiFile)
layout.addWidget(self.btn_saveFile)
self.setLayout(layout)
# 设置信号
self.btn_chooseDir.clicked.connect(self.slot_btn_chooseDir)
self.btn_chooseFile.clicked.connect(self.slot_btn_chooseFile)
self.btn_chooseMutiFile.clicked.connect(self.slot_btn_chooseMutiFile)
self.btn_saveFile.clicked.connect(self.slot_btn_saveFile)
def slot_btn_chooseDir(self):
dir_choose = QFileDialog.getExistingDirectory(self,
"选取文件夹",
self.cwd) # 起始路径
if dir_choose == "":
print("\n取消选择")
return
print("\n你选择的文件夹为:")
print(dir_choose)
def slot_btn_chooseFile(self):
fileName_choose, filetype = QFileDialog.getOpenFileName(self,
"选取文件",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)") # 设置文件扩展名过滤,用双分号间隔
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择的文件为:")
print(fileName_choose)
print("文件筛选器类型: ",filetype)
def slot_btn_chooseMutiFile(self):
files, filetype = QFileDialog.getOpenFileNames(self,
"多文件选择",
self.cwd, # 起始路径
"All Files (*);;PDF Files (*.pdf);;Text Files (*.txt)")
if len(files) == 0:
print("\n取消选择")
return
print("\n你选择的文件为:")
for file in files:
print(file)
print("文件筛选器类型: ",filetype)
def slot_btn_saveFile(self):
fileName_choose, filetype = QFileDialog.getSaveFileName(self,
"文件保存",
self.cwd, # 起始路径
"All Files (*);;Text Files (*.txt)")
if fileName_choose == "":
print("\n取消选择")
return
print("\n你选择要保存的文件为:")
print(fileName_choose)
print("文件筛选器类型: ",filetype)
if __name__=="__main__":
app = QApplication(sys.argv)
mainForm = MainForm('测试QFileDialog')
mainForm.show()
sys.exit(app.exec_())