【动物识别】基于飞桨Paddle的动物分类识别软件设计
动物识别 猫狗识别
1.1实验内容
- 了解深度学习的基本原理;
- 能够使用 PaddleHub 深度学习开源工具进行图像识别和分类;
- 能够使用 PaddlePaddle 项目完成动物分类实验;
- 能够使用 Pycharm 完成 Paddle 项目的移植,并完成动物分类实验;
- 能够使用 Pyqt5 设计软件的可视化界面并调用移植后的动物分类程序。
1.2实验方法及步骤
先创建虚拟环境
conda create --name dw python=3.8
安装需要的库
conda activate dw
pip install PyQt5
pip install PyQt5-tools
pip install PyQt5 PyQtWebEngine
conda install matplotlib
pip install paddlepaddle
pip install paddlehub
pip install opencv-python
如果速度慢可以自行换源,就是缺什么安装什么
创建一个python 文件 recognize.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from tes1 import Ui_MainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QGraphicsWidget, QApplication, QFileDialog
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MyMainWin(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWin, self).__init__(parent)
self.setupUi(self)
self.Select.clicked.connect(self.openfile)
self.ClassButton.clicked.connect(self.recongonise)
def openfile(self):
self.download_path = QFileDialog.getOpenFileName(self.centralwidget, "选择图片", "D:\python_test\donwu","Image File(*.jpg *.gif *.png *.jpeg)")
print(self.download_path[0])
if not self.download_path[0].strip():
pass
else:
scarePixmap = QtGui.QPixmap(self.download_path[0]).scaled(self.tupian.width(), self.tupian.height())
self.tupian.setPixmap(scarePixmap)
with open('test.txt', 'w') as f:
f.write(self.download_path[0])
def recongonise(self):
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
with open('test.txt', 'r') as f:
test_img_path = []
for line in f:
test_img_path.append(line.strip())
with open('test.txt', 'r') as f:
test_img_path = []
for line in f:
test_img_path.append(line.strip())
import paddlehub as hub
module = hub.Module(name="mobilenet_v2_animals")
import cv2
image_path= test_img_path
np_images =[cv2.imread(test_img_path[0]) ]
results = module.classification(images=np_images)
#for result in results:
for result in results:
str1=str(list(result.keys()))
str2=str(list(result.values()))
self.b.setText(str2)
self.c.setText(str1)
if __name__ == '__main__':
app = QApplication(sys.argv)
mywindow = MyMainWin()
mywindow.show()
app.exec()
再创建一个test1.py文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'tes1.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Select = QtWidgets.QPushButton(self.centralwidget)
self.Select.setGeometry(QtCore.QRect(100, 430, 121, 51))
self.Select.setObjectName("Select")
self.ClassButton = QtWidgets.QPushButton(self.centralwidget)
self.ClassButton.setGeometry(QtCore.QRect(370, 430, 121, 51))
self.ClassButton.setObjectName("ClassButton")
self.dongwuzhonglei = QtWidgets.QTextBrowser(self.centralwidget)
self.dongwuzhonglei.setGeometry(QtCore.QRect(540, 70, 201, 51))
self.dongwuzhonglei.setStyleSheet("background-color: rgb(170, 255, 127);")
self.dongwuzhonglei.setObjectName("dongwuzhonglei")
self.zhengquelv = QtWidgets.QTextEdit(self.centralwidget)
self.zhengquelv.setGeometry(QtCore.QRect(540, 220, 201, 51))
self.zhengquelv.setStyleSheet("background-color: rgb(170, 255, 127);")
self.zhengquelv.setObjectName("zhengquelv")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(250, 10, 251, 51))
self.textEdit.setObjectName("textEdit")
self.tupian = QtWidgets.QLabel(self.centralwidget)
self.tupian.setGeometry(QtCore.QRect(60, 100, 381, 291))
self.tupian.setStyleSheet("background-color: rgb(255, 255, 255);")
self.tupian.setText("")
self.tupian.setObjectName("tupian")
self.b = QtWidgets.QTextBrowser(self.centralwidget)
self.b.setGeometry(QtCore.QRect(490, 330, 281, 41))
self.b.setObjectName("b")
self.c = QtWidgets.QTextBrowser(self.centralwidget)
self.c.setGeometry(QtCore.QRect(490, 150, 281, 31))
self.c.setObjectName("c")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "动物识别软件"))
self.Select.setText(_translate("MainWindow", "Select"))
self.ClassButton.setText(_translate("MainWindow", "ClassButton"))
self.dongwuzhonglei.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt;\">动物种类:</span></p></body></html>"))
self.zhengquelv.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt;\">正确率:</span></p></body></html>"))
self.textEdit.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:18pt;\">动物分类软件</span></p></body></html>"))
再在同目录下面创建一个孔空白的文本文件 test.txt
并在这个文件夹里面放入你需要检测的图片
格式如下:
然后运行recognize.py 即可弹出.ui界面。
python recognize.py
或者直接点击运行
test.txt是用来存路径的,当然也可以使用其余方式!
最后也可以打包成.exe程序
pip install Pyinstaller
Pyinstaller -F -w recognize.py
欢迎留言