我是一个甜甜的大橙子🍊,欢迎关注??! 我相信技术的力量💪 努力将所学分享给大家😎 你的点赞??分享🚀收藏📖就是对我最大的鼓励!
1.效果
选择要识别的类型,然后导入图片,利用百度API,完成图像中的文字识别,将结果显示在右侧,并且可以将识别结果复制到剪贴板。
2.运行前准备
2.1申请百度AI的API
- 这里给出参考文档 AI接入指南,按照步骤进行操作。
- 按照步骤操作后会得到
API Key 和Secret Key ,这是获取Access Token 的前提。 - 注意按照提示,申请免费的测试资源。
- 将自己的
API Key 和Secret Key 替换到下面代码中即可。
2.2安装pyqt5等相关依赖包
pip install sip
pip install pyqt5
pip install pyqt5-tools
pip install requests
3代码
在代码中找到AK 和SK ,替换成2.1中自己的API Key 和Secret Key 即可。
AK = 'API_KEY'
SK = 'SECRET_KEY'
完整代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import requests
import base64
import time
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1117, 787)
self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(130, 120, 421, 80))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.comboBox = QtWidgets.QComboBox(self.horizontalLayoutWidget)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.horizontalLayout.addWidget(self.comboBox)
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(Form)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(130, 230, 421, 80))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget_2)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_2.addWidget(self.pushButton)
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(130, 340, 421, 311))
self.label_3.setStyleSheet("border-width:1px; border-style:solid; border-color:rgb(0,0,0)")
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.verticalLayoutWidget = QtWidgets.QWidget(Form)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(609, 119, 321, 531))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)
self.label_4.setStyleSheet("border-width:1px; border-style:solid; border-color:rgb(0,0,0)")
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.verticalLayout.addWidget(self.label_4)
self.pushButton_2 = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "图像识别工具"))
self.label.setText(_translate("Form", "选择识别类型"))
self.comboBox.setItemText(0, _translate("Form", "银行卡"))
self.comboBox.setItemText(1, _translate("Form", "动物"))
self.comboBox.setItemText(2, _translate("Form", "植物"))
self.comboBox.setItemText(3, _translate("Form", "通用票据"))
self.comboBox.setItemText(4, _translate("Form", "营业执照"))
self.comboBox.setItemText(5, _translate("Form", "身份证"))
self.comboBox.setItemText(6, _translate("Form", "车牌号"))
self.comboBox.setItemText(7, _translate("Form", "驾驶证"))
self.comboBox.setItemText(8, _translate("Form", "行驶证"))
self.comboBox.setItemText(9, _translate("Form", "车型"))
self.comboBox.setItemText(10, _translate("Form", "Logo"))
self.label_2.setText(_translate("Form", "选择图片"))
self.pushButton.setText(_translate("Form", "选择"))
self.pushButton.clicked.connect(self.openfile)
self.pushButton_2.setText(_translate("Form", "复制到剪贴板"))
self.pushButton_2.clicked.connect(self.copyResult)
def openfile(self):
self.file_path = QtWidgets.QFileDialog.getOpenFileName(self.horizontalLayoutWidget_2, "选择要识别的图片", '/',
'*.jpg *.png')
if not self.file_path[0].strip():
pass
else:
self.lineEdit.setText(self.file_path[0])
pixmap = QtGui.QPixmap(self.file_path[0])
scaled_pixmap = pixmap.scaled(QtCore.QSize(311, 300), aspectRatioMode=Qt.KeepAspectRatio)
self.label_3.setPixmap(scaled_pixmap)
self.type_selected()
def copyResult(self):
clipboard = QApplication.clipboard()
clipboard.setText(self.label_4.text())
self.child_window = Child()
self.child_window.show()
def type_selected(self):
'''
0.银行卡
1.动物
2.植物
3.通用票据
4.营业执照
5.身份证
6.车牌号
7.驾驶证
8.行驶证
9.车型
10.Logo
'''
if self.comboBox.currentIndex() == 0:
pass
if self.comboBox.currentIndex() == 1:
pass
if self.comboBox.currentIndex() == 2:
pass
if self.comboBox.currentIndex() == 3:
pass
if self.comboBox.currentIndex() == 4:
pass
if self.comboBox.currentIndex() == 5:
self.get_idcard(self.get_token())
if self.comboBox.currentIndex() == 6:
pass
if self.comboBox.currentIndex() == 7:
pass
if self.comboBox.currentIndex() == 8:
pass
if self.comboBox.currentIndex() == 9:
pass
if self.comboBox.currentIndex() == 10:
pass
def get_token(self):
AK = 'API_KEY'
SK = 'SECRET_KEY'
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + AK + '&client_secret=' + SK
response = requests.get(host)
access_token = response.json().get('access_token')
return access_token
def get_idcard(self, access_token):
'''
身份证识别
'''
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
f = open(self.file_path[0], 'rb')
img = base64.b64encode(f.read())
params = {"id_card_side": "front", "image": img}
access_token = access_token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
info_idcard = response.json()
try:
words_result = info_idcard['words_result']
result_recoginition = '识别结果为: \n'
result_recoginition += '姓名:{} \n性别:{} \n民族:{} \n出生日期:{} \n住址:{} \n公民身份证号码:{}\n'.format(
words_result['姓名']['words'],
words_result['性别']['words'],
words_result['民族']['words'],
words_result['出生']['words'],
words_result['住址']['words'],
words_result['公民身份号码']['words']
)
except BaseException:
result_recoginition = '无法解析'
self.label_4.setText(result_recoginition)
class Child(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('复制成功')
layout = QVBoxLayout()
self.label = QLabel('复制成功')
layout.addWidget(self.label)
self.setLayout(layout)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Form()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
|