1.首先需申请一个百度AI接口http://ai.baidu.com/,点击进入
选择控制台->图像处理,进入后选择创建应用,选择文字识别->银行卡识别
如下图,获得API Key和Secret Key,通过如下http请求获得access_token,此值后续有用
详细鉴权认证见此链接: http://ai.baidu.com/docs#/Auth/top
银行卡识别调用方式接口文档见:
https://cloud.baidu.com/doc/OCR/OCR-API.html#.E8.B0.83.E7.94.A8.E6.96.B9.E5.BC.8F.E4.B8.80
2.pyqt5制作界面Main.UI,按钮下面放两个label,一个用来显示银行卡原图,一张显示识别的结果
3.将ui转为python,在此基础上添加业务代码
代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainForm.ui'
#
# Created by: PyQt5 UI code generator 5.12.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import os
import base64
import urllib.request
import json
class Ui_Dialog(object):
access_token = '24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074'
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(566, 526)
self.widget = QtWidgets.QWidget(Dialog)
self.widget.setGeometry(QtCore.QRect(20, 40, 541, 461))
self.widget.setObjectName("widget")
self.pb_get = QtWidgets.QPushButton(self.widget)
self.pb_get.setGeometry(QtCore.QRect(170, 40, 75, 31))
self.pb_get.setObjectName("pb_get")
self.pb_choose = QtWidgets.QPushButton(self.widget)
self.pb_choose.setGeometry(QtCore.QRect(60, 40, 75, 31))
self.pb_choose.setObjectName("pb_choose")
self.label_image = QtWidgets.QLabel(self.widget)
self.label_image.setGeometry(QtCore.QRect(30, 120, 471, 171))
self.label_image.setText("")
self.label_image.setObjectName("label_image")
self.label_Result = QtWidgets.QLabel(self.widget)
self.label_Result.setGeometry(QtCore.QRect(20, 330, 481, 111))
self.label_Result.setText("")
self.label_Result.setObjectName("label_Result")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pb_choose.clicked.connect(self.openfile)
self.pb_get.clicked.connect(self.gain_bankcard)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "银行卡识别"))
self.pb_get.setText(_translate("Dialog", "自动识别"))
self.pb_choose.setText(_translate("Dialog", "选择图片"))
# 如下为添加业务代码
def openfile(self):
# 启动选择文件对话框,查找jpg以及png图片
self.download_path =QtWidgets.QFileDialog.getOpenFileName(self.widget,caption='选择要识别的图片',\
directory='/',filter='Image Files(*.jpg *.png)')
# 判断是否选择图片
if not self.download_path[0].strip(): # 没有选择图片
pass
else:
pixmap = QtGui.QPixmap(self.download_path[0]) # pixmap解析图片
scaredPixmap = pixmap.scaled(QtCore.QSize(311,301)) # d等比例缩放图片
self.label_image.setPixmap(scaredPixmap) # 标签控件中设置图片
self.label_image.show() #标签控件中显示图片
pass
# 读取文件
def get_file_content(self,filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 获得银行卡信息
def gain_bankcard(self):
try:
#access_token
request_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard'
# 二进制方式打开图片文件
print(str(self.download_path[0]))
f = self.get_file_content(self.download_path[0])
img = base64.b64encode(f)
params = {'image':img}
params = urllib.parse.urlencode(params).encode('utf-8')
request_url = request_url + "?access_token="+self.access_token
print(request_url)
# 请求百度云AI银行卡接口
request = urllib.request.Request(url=request_url,data=params)
request.add_header('Content-Type','application/x-www-form-urlencoded')
response = urllib.request.urlopen(request)
content = response.read()
Info = '结果'
if content:
bankcards =json.loads(content)
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 = '信用卡'
card_number = bankcards['result']['bank_card_number']
bank_name = bankcards['result']['bank_name']
Info = (' 卡号:{0}\n 银行:{1}\n 类型:{2}').format(card_number,bank_name,bank_card_type)
except BaseException:
error_msg = bankcards['error_msg']
Info = 'error:'+error_msg
#设置识别显示结果
self.label_Result.setText(Info)
except Exception as e:
print(e)
def Show_MainWindow():
app = QtWidgets.QApplication(sys.argv) # 实例化QApplication类,作为GUI主程序入口
MainWindow = QtWidgets.QMainWindow() # 创建MainWindow
ui = Ui_Dialog() # 实例UI类
ui.setupUi(MainWindow) # 设置窗体UI
MainWindow.show() # 显示窗体
sys.exit(app.exec_()) # 当窗口创建完成,需要结束主循环过程
if __name__ == "__main__":
Show_MainWindow()
程序运行:选中图片,点击自动识别即可正确识别