项目使用PyQt5接口百度AI文字识别服务,使用SQLite为数据库引擎,开发实现普通文字识别、身份证识别、银行卡识别、驾驶证识别、行驶证识别和车牌识别的集成系统,百度AI的文字识别还有很多别的功能,可以根据个体不同需要调入。
Github地址:https://github.com/shanghaixuhuan/myOCR
不同的文字识别用的是一个界面代码,主要区别在于对图片处理后的字符串提取的区别。
import sys
import time
import random
from aip import AipOcr
from PyQt5.QtWidgets import (QDialog,QApplication,QLabel,QPushButton,
QFileDialog,QMessageBox,QPlainTextEdit,QHBoxLayout,
QVBoxLayout)
from PyQt5.QtGui import QIcon,QFont
from PyQt5.QtSql import QSqlQuery,QSqlDatabase
import qdarkstyle
from PyQt5 import QtGui
APP_ID = ' ' #写入APP_ID
API_KEY = ' ' #写入API_KEY
SECRET_KEY = ' ' #写入SECRET_KEY
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
options = {
'detect_direction': 'true',
'language_type': 'CHN_ENG',
}
class BasicRecognize(QDialog):
def __init__(self):
super(BasicRecognize,self).__init__()
self.text = ""
self.strTime = ""
self.basicid = ""
self.filePath = ""
self.initUI()
def initUI(self):
self.resize(700,600)
self.setWindowTitle("myOCR——普通文字识别")
self.setWindowIcon(QIcon("./images/Icon.png"))
self.plabel = QLabel(self)
self.plabel.setFixedSize(400,300)
self.obtn = QPushButton(self)
self.obtn.setText("打开本地图片")
self.obtn.setFont(QFont("苏新诗柳楷繁", 15))
self.obtn.clicked.connect(self.openimage)
self.obtn.setFixedSize(180,40)
self.sbtn = QPushButton(self)
self.sbtn.setText("开 始 识 别")
self.sbtn.setFont(QFont("苏新诗柳楷繁", 15))
self.sbtn.clicked.connect(self.recognize)
self.sbtn.setFixedSize(180,40)
self.v1box = QVBoxLayout()
self.v1box.addWidget(self.obtn)
self.v1box.addWidget(self.sbtn)
self.h1box = QHBoxLayout()
self.h1box.addWidget(self.plabel)
self.h1box.addLayout(self.v1box)
self.tlabel = QLabel(self)
self.tlabel.setText("识\n别\n结\n果")
self.tlabel.setFont(QFont("苏新诗柳楷繁", 15))
self.tlabel.resize(200, 50)
self.tedit = QPlainTextEdit(self)
self.tedit.setFont(QFont("宋体",10))
self.tedit.setFixedSize(600,350)
self.h2box = QHBoxLayout()
self.h2box.addStretch(1)
self.h2box.addWidget(self.tlabel)
self.h2box.addStretch(1)
self.h2box.addWidget(self.tedit)
self.h2box.addStretch(1)
self.vbox = QVBoxLayout()
self.vbox.addLayout(self.h1box)
self.vbox.addStretch(1)
self.vbox.addLayout(self.h2box)
self.setLayout(self.vbox)
def openimage(self):
self.filePath, imgType = QFileDialog.getOpenFileName(self, "打开本地图片", "", "*.jpg;;*.png;;All Files(*)")
self.jpg = QtGui.QPixmap(self.filePath).scaled(self.plabel.width(), self.plabel.height())
self.plabel.setPixmap(self.jpg)
def recognize(self):
if(self.filePath == ""):
print(QMessageBox.warning(self, "警告", "请插入图片", QMessageBox.Yes, QMessageBox.Yes))
return
now = int(time.time())
timeStruct = time.localtime(now)
self.strTime = time.strftime("%Y/%m/%d %H:%M", timeStruct)
self.basicid = 'b' + str(time.strftime("%g%m%d")) + str(random.randint(0, 9999)).zfill(4)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/myOCR.db')
db.open()
query = QSqlQuery()
sql = "select * from records where RecordId = '%s'"%(self.basicid)
query.exec_(sql)
if (query.next()):
print(QMessageBox.warning(self, "警告", "系统错误,请重新提交", QMessageBox.Yes, QMessageBox.Yes))
return
result = aipOcr.basicAccurate(self.get_file_content(self.filePath), options)
words_result = result['words_result']
for i in range(len(words_result)):
self.text = self.text + words_result[i]['words']
sql = "insert into records values('%s','%s','%s','普通文字识别','%s','')"%(
self.basicid,self.filePath,self.strTime,self.text)
query.exec_(sql)
db.commit()
db.close()
print(QMessageBox.information(self, "提醒", "您已成功识别文字!", QMessageBox.Yes, QMessageBox.Yes))
self.tedit.setPlainText(self.text)
def get_file_content(self,filePath):
with open(filePath, 'rb') as fp:
return fp.read()
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
basicrecognizeWindow = BasicRecognize()
basicrecognizeWindow.show()
sys.exit(app.exec_())
result = aipOcr.idcard(self.get_file_content(self.filePath_f),"front",options)
address = result['words_result']['住址']['words']
birthday = result['words_result']['出生']['words']
year_i = birthday[0:4]
month_i = birthday[4:6]
day_i = birthday[6:8]
name = result['words_result']['姓名']['words']
id = result['words_result']['公民身份号码']['words']
sex = result['words_result']['性别']['words']
nation =result['words_result']['民族']['words']
result = aipOcr.idcard(self.get_file_content(self.filePath_b), "back", options)
date_q = result['words_result']['签发日期']['words']
date_q_y = date_q[0:4]
date_q_m = date_q[4:6]
date_q_d = date_q[6:8]
office = result['words_result']['签发机关']['words']
date_s = result['words_result']['失效日期']['words']
date_s_y = date_s[0:4]
date_s_m = date_s[4:6]
date_s_d = date_s[6:8]
self.text = "姓名:" + name + "\n性别:" + sex + " 民族:" + nation \
+ "\n出生:" + year_i + "年" + month_i + "月" + day_i + "日\n住址:" \
+ address + "\n公民身份号码:" + id + "\n\n签发日期:" + date_q_y \
+ "年" + date_q_m + "月" + date_q_d + "日\n签发机关:" + office \
+ "\n失效日期:" + date_s_y + "年" + date_s_m + "月" + date_s_d + "日"
result = aipOcr.drivingLicense(self.get_file_content(self.filePath),options)
id = result['words_result']['证号']['words']
deadline = result['words_result']['有效期限']['words']
type = result['words_result']['准驾车型']['words']
address = result['words_result']['住址']['words']
to = result['words_result']['至']['words']
name = result['words_result']['姓名']['words']
nation = result['words_result']['国籍']['words']
birthday = result['words_result']['出生日期']['words']
sex = result['words_result']['性别']['words']
first = result['words_result']['初次领证日期']['words']
self.text = "中华人民共和国机动车驾驶证\n证号:" + id + "\n姓名:" + name \
+ " 性别:" + sex + " 国籍:" + nation + "\n住址:" + address \
+ "\n\n出生日期:" + birthday + "\n初次领证日期:" + first \
+ "\n准驾车型:" + type + "\n有效期限:" + deadline + "至" + to
result = aipOcr.vehicleLicense(self.get_file_content(self.filePath),options)
type = result['words_result']['品牌型号']['words']
date = result['words_result']['发证日期']['words']
property = result['words_result']['使用性质']['words']
id = result['words_result']['发动机号码']['words']
carid = result['words_result']['号牌号码']['words']
person = result['words_result']['所有人']['words']
address = result['words_result']['住址']['words']
setup = result['words_result']['注册日期']['words']
reco = result['words_result']['车辆识别代号']['words']
car_type = result['words_result']['车辆类型']['words']
self.text = "中华人民共和国机动车行驶证\n号牌号码:" + carid + " 车辆类型:" \
+ car_type + "\n所有人:" + person + "\n住址:" + address + "\n\n使用性质:" \
+ property + " 品牌型号:" + type + "\n车辆识别代码:" + reco + "\n发动机号码:" \
+ id + "\n注册日期:" + setup + " 发证日期:" + date
result = aipOcr.licensePlate(self.get_file_content(self.filePath),options)
plate = result['words_result']['number']
self.text = "车牌号码:" + plate
result = aipOcr.bankcard(self.get_file_content(self.filePath),options)
number = result['result']['bank_card_number']
date = result['result']['valid_date']
type = result['result']['bank_card_type']
name = result['result']['bank_name']
if(type == 1):
type = "借记卡"
else:
type = "信用卡"
self.text = "卡号:" + number + "\n发卡行:" + name + "\n卡片类型:" + type + "\n有效期:" + date