本文适合于所有层次的Python学习者,包括AI爱好者和技术爱好者来学习。诚然,人工智能是个有着深刻数学逻辑的知识体系,包括机器学习,搜索问题,知识表示等等等等的理论。本文,严禁的说,我们只是简单调用了百度AI的成果,jacky通过本文希望给大家一个启发吧,毕竟理论是理论,实际是实际,赚钱是赚钱,就这样。
本文视频演示如下(由于环境因素的限制,这段演示视频是无声的,后期jacky 在Gitchat ,阿里云以及抖音都会有直播的分享,请大家关注):
视频演示链接
本文的视频演示如下:
系统开发环境要求:
1.开发工具:Pycharm 、PyQt5
2.开发模块: urllib、urllib.request、Base64、json、PyQt5
3.API 接口:百度API
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ocr.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
# 原创:朱元禄(jacky)首发:数据分析部落(datashuju)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import json
# 我们是Python3 ,在调用百度AI token 的时候不用官方文档中的urllib2,替换成 urllib.request
import urllib, urllib.request, sys
import ssl
import base64
# 这里输入自己获取的AK以及SK
API_KEY = "jacky提醒你,这里要输入你自己申请的AK,不要忘记"
SECRET_KEY = "jacky提醒你,这里要输入你自己申请的AK,不要忘记"
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(793, 452)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(80, 41, 631, 371))
self.widget.setObjectName("widget")
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(self.widget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 2, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 2, 1, 1, 1)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName("label_3")
self.label_3.setStyleSheet("background-color:gray")
self.gridLayout.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 3, 0, 1, 3)
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.gridLayout.addWidget(self.lineEdit_2, 7, 0, 1, 2)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 7, 2, 1, 1)
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.comboBox = QtWidgets.QComboBox(self.widget)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.gridLayout.addWidget(self.comboBox, 0, 1, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "智能银行卡识别系统_v1.0"))
self.pushButton.setText(_translate("Form", "选择"))
self.label_2.setText(_translate("Form", "选择要识别的图片"))
self.label_3.setText(_translate("Form", ""))
self.lineEdit_2.setText(_translate("Form", "显示识别结果"))
self.pushButton_2.setText(_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.pushButton.clicked.connect(self.openfile)
# 第二步,设置选择图片的路径
def openfile(self):
self.download_path = QFileDialog.getOpenFileName(self.widget,"请选择需要识别的图片","/","Image File(*.jpg *.png)")
# 判断是否选择了图片
if not self.download_path[0].strip():
pass
else:
self.lineEdit.setText(self.download_path[0])
# 自动解析图像
pixmap = QPixmap(self.download_path[0])
# 选择图片,图片可能有大有小,我们简单的处理一下图片
scarePixmap = pixmap.scaled(QSize(629,271),aspectRatioMode=Qt.KeepAspectRatio)
# 把图片设置到我们的控件里
self.label_3.setPixmap(scarePixmap)
self.typeTp()
# 第三步:选择类型
def typeTp(self):
if self.comboBox.currentIndex()==0:
self.get_token()
self.get_bankcard(self.get_token())
def get_token(self):
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+API_KEY+'&client_secret='+SECRET_KEY
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read()
if (content):
print(content)
# 解释返回的 token 数据
self.access_token = json.loads(content)['access_token']
return self.access_token
def get_bankcard(self,access_token):
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=' + access_token
f = open(self.download_path[0], 'rb') # 二进制方式打开图文件
img = base64.b64encode(f.read())
params = {"image": img}
# 百度AI给的示例是Python2
# params = urllib.urlencode(params)
params = urllib.parse.urlencode(params).encode('utf-8')
request = urllib.request.Request(url, params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib.request.urlopen(request)
content = response.read()
if (content):
print(content)
bankcards = json.loads(content)
stover = '识别结果:\n'
try:
if bankcards['result']['bank_card_type'] == 0:
bank_card_type = "不能识别"
elif bankcards['result']['bank_card_type'] == 1:
bank_card_type = "借记卡"
elif bankcards['result']['bank_card_type'] == 2:
bank_card_type = "信用卡"
stover += '卡号: {}\n 银行:{}\n 类型:{}'.format(bankcards['result']['bank_card_number'],bankcards['result']['bank_name'],bank_card_type)
except BaseException:
stover += '解析错误'
self.lineEdit_2.setText(stover)
if __name__ == "__main__":
import sys
from PyQt5.QtGui import QIcon
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow
widget = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(widget)
# widget.setWindowIcon(QIcon('web.png'))#增加icon图标,如果没有图片可以没有这句
widget.show()
sys.exit(app.exec_())