python可以通过python+Opencv来实现很多文字识别之类的工作,因为OpenCV库的功能可以说是相当强大,很多功能都可以完成。但是实现起来需要自己造轮子,所以很费时间和精力,我们可以直接学会调用百度AI智能平台的接口来实现许多有用的功能,今天来介绍其中一种——python用百度云接口实现身份证识别
文字识别的接口:https://ai.baidu.com/ai-doc/OCR/3k3h7yeqa
用户向服务请求识别某张图中的所有文字
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
idCardSide = "back"
""" 调用身份证识别 """
client.idcard(image, idCardSide);
""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["detect_risk"] = "false"
""" 带参数调用身份证识别 """
client.idcard(image, idCardSide, options)
本地文件夹中的图片和远程url图片均可实现。上述代码给出了一个身份证识别的代码框架,其中的options可以不选。
- 工欲善其事,必先利其器。我们首先得安装对应的第三方库——baidu-aip.
windows+R打开命令行模式
输入pip install baidu-aip
(可以选择其他的镜像网站,速度更快)
由于我已经安装完成,会出现Requirement already satisfied,新安装的应该会显示Successfully install.
C:\Users\lenovo>pip install baidu-aip
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: baidu-aip in e:\anaconda\lib\site-packages (2.2.18.0)
Requirement already satisfied: requests in e:\anaconda\lib\site-packages (from baidu-aip) (2.22.0)
Requirement already satisfied: certifi>=2017.4.17 in e:\anaconda\lib\site-packages (from requests->baidu-aip) (2020.4.5.1)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in e:\anaconda\lib\site-packages (from requests->baidu-aip) (1.24.2)
Requirement already satisfied: idna<2.9,>=2.5 in e:\anaconda\lib\site-packages (from requests->baidu-aip) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in e:\anaconda\lib\site-packages (from requests->baidu-aip) (3.0.4)
2.调用接口需要三个很重要的东西,分别是APP_ID, API_KEY, SECEET_KEY。
Q:如何获取?
A:在百度AI平台找到开放能力,在文字识别里面找到 身份证识别,之后点击“立即使用”
随后需要你创建应用,创建之后就可以看到这三个重点了。
之后完全可以按照百度给的框架来进行身份证识别啦。
安装的时候需要的是import baidu-aip,导入的时候需要from aip import AipOcr。
其中的信息是用json形式组织的,每种识别方式都可以在前面给出的接口中找到,例如身份证识别的实例:
{
"log_id": 2648325511,
"direction": 0,
"image_status": "normal",
"idcard_type": "normal",
"edit_tool": "Adobe Photoshop CS3 Windows",
"words_result": {
"住址": {
"location": {
"left": 267,
"top": 453,
"width": 459,
"height": 99
},
"words": "南京市江宁区弘景大道3889号"
},
"公民身份号码": {
"location": {
"left": 443,
"top": 681,
"width": 589,
"height": 45
},
"words": "330881199904173914"
},
"出生": {
"location": {
"left": 270,
"top": 355,
"width": 357,
"height": 45
},
"words": "19990417"
},
"姓名": {
"location": {
"left": 267,
"top": 176,
"width": 152,
"height": 50
},
"words": "伍云龙"
},
"性别": {
"location": {
"left": 269,
"top": 262,
"width": 33,
"height": 52
},
"words": "男"
},
"民族": {
"location": {
"left": 492,
"top": 279,
"width": 30,
"height": 37
},
"words": "汉"
}
},
"words_result_num": 6
}
#python百度ai的身份证识别代码
from aip import AipOcr
# 定义常量
APP_ID = '22566031' #你百度帐号上的APP_ID
API_KEY = 'joVlDhxGwHp45YnNV0DxNBSw' #你百度帐号上的API_KEY
SECRET_KEY = 'xxxxxxxxxx'#你百度帐号上的SECRET_KEY
# 初始化AipFace对象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('微信图片_202003202347431.jpg')#将左侧括号内替换为待识别的图片路径
idCardSide = "front"
print(type(image))
""" 调用身份证识别 """
result=client.idcard(image, idCardSide)
print("姓名:",result[ "words_result"]["姓名"]["words"])
print("性别:",result[ "words_result"]["性别"]["words"])
print("民族:",result[ "words_result"]["民族"]["words"])
print("生日:",result[ "words_result"]["出生"]["words"])
print("身份证号:",result[ "words_result"]["公民身份号码"]["words"])
print("住址:",result[ "words_result"]["住址"]["words"])
自己的例子实在没法通过,请各位自己测试吧~~
更多有趣的实例可以关注我的专栏~~
python调用百度云接口实例