系列文章
一、项目简介
废话不多说,先上图:
本文主要介绍的基于Python和Pyqt框架构建的图片文字识别及情感分析系统。
完整代码在最下方,想要先看源码的同学可以移步本文最下方进行下载。
博主也参考过文本分类相关模型的文章,但大多是理论大于方法。很多同学肯定对原理不需要过多了解,只需要搭建出一个可视化系统即可。
也正是因为我发现网上大多的帖子只是针对原理进行介绍,功能实现的相对很少。
如果您有以上想法,那就找对地方了!
不多废话,直接进入正题!
二、系统功能
1、图像导入
可以将自己需要识别文字的图片,上传导入到系统中。
选择需要识别的文字图片:
导入成功!!
2、文字识别
可以识别图片中文字并返回结果到文字识别框中,比如上述图片的结果如下:
点击文字识别按钮
我们换一张图片试下:
基本上不是太过复杂场景的文字,基本都可以准确识别。
3、情感分析
文字识别完成后,就可以基于NLP技术对文本进行情感分析,判断文本的积极、消极或中性情绪。
请确保文字识别有结果后,点击情感分析按钮:
结果如下:
我们再识别一下上面的图片。
五、代码功能介绍
1、依赖环境
本项目使用的是pycharm的python编译环境,如不清楚如何使用的同学可以参考csdn上其他博主的基础教程,这里就不进行赘述。
后端:Python, NLTK(自然语言处理工具包)
前端:PYQT5
2、系统界面UI
- new_ui.py 文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'new_ui.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_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1055, 571)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
Form.setSizePolicy(sizePolicy)
Form.setMinimumSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setBold(False)
font.setWeight(50)
Form.setFont(font)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("ico.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
Form.setStyleSheet("#Form{\n"
" background-color:rgb(229, 229, 229)\n"
"}\n"
"\n"
"QPushButton{\n"
" background-color: #0072C6;\n"
" color: white;\n"
" border-style: outset;\n"
" border-width: 2px;\n"
" border-radius: 10px;\n"
" border-color: black;\n"
" font-size: 17px;\n"
" padding: 6px;\n"
"}\n"
"\n"
"QPushButton:hover {\n"
" background-color: #148CD2;\n"
"}\n"
"QPushButton:pressed {\n"
" background-color: #005CA3;\n"
"}")
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.sentiment_analysis_button = QtWidgets.QPushButton(Form)
self.sentiment_analysis_button.setEnabled(True)
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(-1)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.sentiment_analysis_button.setFont(font)
self.sentiment_analysis_button.setLayoutDirection(QtCore.Qt.LeftToRight)
self.sentiment_analysis_button.setAutoFillBackground(False)
self.sentiment_analysis_button.setStyleSheet("")
self.sentiment_analysis_button.setCheckable(False)
self.sentiment_analysis_button.setChecked(False)
self.sentiment_analysis_button.setObjectName("sentiment_analysis_button")
self.gridLayout.addWidget(self.sentiment_analysis_button, 2, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.upload_img_button = QtWidgets.QPushButton(Form)
self.upload_img_button.setEnabled(True)
self.upload_img_button.setMaximumSize(QtCore.QSize(466, 16777215))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(-1)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.upload_img_button.setFont(font)
self.upload_img_button.setLayoutDirection(QtCore.Qt.LeftToRight)
self.upload_img_button.setAutoFillBackground(False)
self.upload_img_button.setStyleSheet("QPushButton#upload_img_button{\n"
" background-color: #0072C6;\n"
" color: white;\n"
" border-style: outset;\n"
" border-width: 2px;\n"
" border-radius: 10px;\n"
" border-color: black;\n"
" font-size: 17px;\n"
" padding: 6px;\n"
"}\n"
"\n"
"QPushButton#upload_img_button:hover {\n"
" background-color: #148CD2;\n"
"}\n"
"QPushButton#upload_img_button:pressed {\n"
" background-color: #005CA3;\n"
"}")
self.upload_img_button.setCheckable(False)
self.upload_img_button.setChecked(False)
self.upload_img_button.setObjectName("upload_img_button")
self.gridLayout.addWidget(self.upload_img_button, 0, 0, 1, 1, QtCore.Qt.AlignHCenter)
self.textBrowser_sa = QtWidgets.QTextBrowser(Form)
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(-1)
self.textBrowser_sa.setFont(font)
self.textBrowser_sa.setStyleSheet("QTextBrowser{\n"
" background-color: #FFFFFF;\n"
" color: black;\n"
" border-style: outset;\n"
" border-width: 2px;\n"
" border-radius: 10px;\n"
" border-color: black;\n"
" font-size: 15px;\n"
" padding: 6px;\n"
"}\n"
"")
self.textBrowser_sa.setObjectName("textBrowser_sa")
self.gridLayout.addWidget(self.textBrowser_sa, 3, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.textBrowser_cr = QtWidgets.QTextBrowser(Form)
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(-1)
self.textBrowser_cr.setFont(font)
self.textBrowser_cr.setStyleSheet("QTextBrowser{\n"
" background-color: #FFFFFF;\n"
" color: black;\n"
" border-style: outset;\n"
" border-width: 2px;\n"
" border-radius: 10px;\n"
" border-color: black;\n"
" font-size: 15px;\n"
" padding: 6px;\n"
"}\n"
"")
self.textBrowser_cr.setObjectName("textBrowser_cr")
self.gridLayout.addWidget(self.textBrowser_cr, 1, 1, 1, 1, QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.character_recognition_button = QtWidgets.QPushButton(Form)
self.character_recognition_button.setEnabled(True)
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(-1)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
self.character_recognition_button.setFont(font)
self.character_recognition_button.setLayoutDirection(QtCore.Qt.LeftToRight)
self.character_recognition_button.setAutoFillBackground(False)
self.character_recognition_button.setStyleSheet("")
self.character_recognition_button.setCheckable(False)
self.character_recognition_button.setChecked(False)
self.character_recognition_button.setObjectName("character_recognition_button")
self.gridLayout.addWidget(self.character_recognition_button, 0, 1, 1, 1, QtCore.Qt.AlignHCenter)
self.img_view = QtWidgets.QLabel(Form)
self.img_view.setMinimumSize(QtCore.QSize(600, 300))
self.img_view.setBaseSize(QtCore.QSize(950, 450))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(-1)
self.img_view.setFont(font)
self.img_view.setStyleSheet("#img_view{\n"
" background-color: #FFFFFF;\n"
" color: black;\n"
" border-style: outset;\n"
" border-width: 2px;\n"
" border-radius: 10px;\n"
" border-color: black;\n"
" font-size: 15px;\n"
" padding: 6px;\n"
"}\n"
"")
self.img_view.setText("")
self.img_view.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
self.img_view.setObjectName("img_view")
self.gridLayout.addWidget(self.img_view, 1, 0, 3, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "情感识别"))
self.sentiment_analysis_button.setText(_translate("Form", " 情感分析 "))
self.upload_img_button.setText(_translate("Form", " 本地上传图片 "))
self.textBrowser_sa.setPlaceholderText(_translate("Form", "上传图片后点击上方【情感分析】按钮,输出分析结果。"))
self.textBrowser_cr.setPlaceholderText(_translate("Form", "上传图片后点击上方【文字识别】按钮,输出识别结果。"))
self.character_recognition_button.setText(_translate("Form", " 文字识别 "))
3、文字识别及情感分析部分代码
class Emotion(QFrame):
def __init__(self, parent=None):
super(Emotion, self).__init__(parent)
self.ui = ui.Ui_Form()
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
app.setStyleSheet(qdarkstyle.load_stylesheet(qt_api='pyqt5'))
self.ui.setupUi(self)
self.setWindowTitle("情感识别")
self.ui.pushButton.clicked.connect(self.open_picture_file)
self.ui.pushButton_2.clicked.connect(self.pic)
self.ui.pushButton_3.clicked.connect(self.word)
self.pic_path = ""
self.AP_KEY = ""
self.SECRET_KEY = ""
self.str = ""
def open_picture_file(self):
self.pic_path, Type = QtWidgets.QFileDialog.getOpenFileName(self, "选择图片", os.getcwd(), "*.png;;*.jpg;")
def get_access_token(self):
params = {"grant_type": "client_credentials", "client_id": self.API_KEY, "client_secret": self.SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
def pic(self):
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
f = open(self.pic_path, 'rb')
img = base64.b64encode(f.read()).decode("utf-8")
host = url + '?access_token=' + self.get_access_token() # 我的access_token
params = {'image': img}
response = requests.post(host, data=params, headers=headers) # 注意用data传值,使用json会报错
self.str = response.json()['words_result'][0]['words']
self.ui.label_2.setText(self.str)
def word(self):
payload = json.dumps({
"text": self.str
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
confidence = response.json()['items'][0]['confidence']
neg = response.json()['items'][0]['negative_prob']
pos = response.json()['items'][0]['positive_prob']
sen = response.json()['items'][0]['sentiment']
if sen == 0:
sen = "负向"
if sen == 1:
sen = "中性"
if sen == 2:
sen = "正向"
self.ui.label_3.setText("置信度:" + str(confidence))
self.ui.label_4.setText("积极类别的概率:" + str(neg))
self.ui.label_5.setText("消极类别的概率:" + str(pos))
self.ui.label_6.setText("分类结果:" + sen)
六、代码下载地址
由于项目代码量和数据集较大,感兴趣的同学可以直接下载代码,使用过程中如遇到任何问题可以在评论区进行评论,我都会一一解答。
代码下载: