当前位置:   article > 正文

手把手教你:基于python的文字识别和情感分析系统_基于python的文字识别情感分类,情感分析以及识别项目,需要导入情感数据集

基于python的文字识别情感分类,情感分析以及识别项目,需要导入情感数据集

系列文章




一、项目简介

废话不多说,先上图:
在这里插入图片描述

本文主要介绍的基于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", " 文字识别 "))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

六、代码下载地址

由于项目代码量和数据集较大,感兴趣的同学可以直接下载代码,使用过程中如遇到任何问题可以在评论区进行评论,我都会一一解答。

代码下载:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/752856
推荐阅读
相关标签
  

闽ICP备14008679号