图像识别说白了就是把一张照片上面的文字进行提取,提供工作效率
需求分析
身份证识别主要是把一张身份证照片上面的文字信息进行提取,不用再使用人工去手动抄写了,下面给大家说的这个身份识别主要是使用python+flask+华为云OCR进行实现的。
步骤
- 申请华为云OCR接口
- 获取token
- 调用身份证识别接口
- 提取身份证信息
申请华为云OCR接口
图像识别主要使用的就是华为云OCR平台申请的接口,申请地址为:“https://www.huaweicloud.com”。访问申请的地址后点击菜单栏中的“控制台”,点击后会进入登录页面。 登录成功后进入控制台,依次单击“服务列表”-“人工智能”-“文字识别OCR” 进入后点击身份证识别后面的“开通服务”即可,(这里需要主要的是免费开通的身份证识别服务仅有1000次的免费调用次数,超过次数将会收费,建议购买套餐包,购买可以找我,我可以给优惠哦)
获取token
获取token值时你需要知道你的以下信息:
- IAM用户所属账号名
- IAM用户名
- IAM用户的登录密码
- IAM用户所属账号的项目名称
首先就是获取IAM用户所属账号名,IAM用户所属账号名为你登陆的那个账号
获取IAM用户名,选择右上角“用户名”-点击“统一身份认证” 里面的用户即为IAM用户名 获取IAM用户所属账号的项目名称,点击“项目” 里面的项目很多,你只需要记录你开启OCR身份证识别区域的项目名称ID就可以了,比如我选择的北京四所以我的项目名称就是:“cn-north-4” 知道了这些信息之后,后面就是如何通过这些信息去获取token了,下面我就直接上代码了哦
import requests
url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
headers = {'Content-Type': 'application/json'}
payload = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "XXX",
"password": "XXX",
"domain": {
"name": "XXX"
}
}
}
},
"scope": {
"project": {
"name": "cn-north-4"
}
}
}
}
response = requests.post(url, headers=headers, json=payload)
token = response.headers['X-Subject-Token']
print(token)
打印出来的值是类似于下面这种的字符串
MIIVfgYJKoZIhvcNAQcCoIIVbzCCFWsCAQExDTALBglghkgBZQMEAgEwghOQBgkqhkiG9w0BBwGgghOBBIITfXsidG9rZW4iOns123iZXhwaXJlc19hdCI6IjIwMjEtMDgtMT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Y2ZlNWUyNGMwIn0sIm5hbWUiOiJjbi1ub3J0aC00IiwiaWQiOiIwNGRmZDYwOTk2MDAwZmU4MmZiNWMwMWMxZDU5NDFjOSJ9LCJpc3N1ZWRfYXQiOiIyMDIxLTA4LTE2VDExOjUzOjExLjU5MjAwMFoiLCJ1c4562VyIjp7ImRvbWFpbiI6eyJuYW1lIjoiYmx4dGNsb3VkIiwiaWQiOiIwNGRmZDYwOTQxODAwZmU4MGZhZmMwMWNmZTVlMjRjMCJ9LCJuYW1lIjoiYmx4dCIsInBhc3N3b3JkX2V4cGlyZXNfYXQiOiIiLCJpZCI6IjBkNGUyNGI3YmEwMGY1ODgxZjcyYzAxYzVhYjU1NmY5In19fTGCAcEwggG9AgEBMIGXMIGJMQswCQYDVQQGEwJDTjESMBAGA1UECAwJR3VhbmdEb25nMREwDwYDVQQH123DAhTaGVuWmhlbjEuMCwGA1UECgwlSHVhd2VpIFNvZnR3YXJlIFRlY2hub2xvZ2llcyBDby4sIEx0ZDEOMAwGA1UECwwFQ2xvdWQxEzARBgNVBAMMCmNhLmlhbS5wa2kCCQDcsytdEGFqEDALBglghkgBZQMEAgEwDQYJKoZIhvcNAQEBBQAEggEAJK1UkA7koUihoCyU8FwcUFpPA4x6ylmTvQDPk2KbYRMVA+gnSybo6vgxf32hcHi-1238KehFzgXhJe59fOYyc7hz+qZ5456iYD0pSGqjICZepOBeTvywtsE2fgHdlQq4RZMDm1bkVj46OVNGszzpz8IGecZxRDhxDfBbVsllo9T41QD28o9QZUNyhhXa-ZIS2UdDK-q7gBsR3mT2LRqjQ+J6vpcCzAe6v71IHAf77lgE+vwg7f6qOfhYjRNB9kSC2L1YxJ1eS9j5pzuqlnK+9BOv2U3BIaKsP9yNiP-luS78LzwuEG2rD9OInMst7bCr7HeWT2bZ8gCT4567BvsONvGqVvr3AA==
调用身份证识别接口
下面就是调用识别接口,调用这个接口需要了解以下信息
- URL
- Header参数
- Body参数
首先是URL POST https://{endpoint}/v2/{project_id}/ocr/id-card
参数 | 说明 |
---|
endpoint | 项目接口域名,这里还是需要确定开通服务的区域,每个区域不同的域名 | project_id | 项目ID,不出意外的话就是你上面获取token的项目ID |
请求参数 请求Header参数
参数 | 说明 |
---|
X-Auth-Token | 用户Token,就是上面获取到的token值 | Content-Type | 发送的实体的MIME类型,参数值为“application/json” |
请求Body参数
参数 | 说明 |
---|
image | 图像数据,base64编码,要求base64编码后大小不超过10MB。图片最小边不小于15px,最长边不超过8000px。支持JPEG、JPG、PNG、BMP、TIFF格式。 | url | 图片的url路径 | side | front:身份证人像面,back:身份证国徽面 |
了解了上面的一系列信息之后,后面就是如何写代码了,下面就是直接代码
import requests
import base64
url = https://{endpoint}/v2/{project_id}/ocr/id-card
token = ""
headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
imagepath = r'./static/XXX.jpg'
with open(imagepath, "rb") as bin_data:
image_data = bin_data.read()
image_base64 = base64.b64encode(image_data).decode("utf-8")
payload = {"image": image_base64}
response = requests.post(url, headers=headers, json=payload)
response = response.text
print(response)
最后识别出来的结果类似于下面的信息:
{
"result": {
"name": "XXX",
"sex": "男",
"ethnicity": "汉",
"birth": "XXX",
"address": "XXX",
"number": "XXX"
}
}
如果想获取其中的某一个值的话需要先转换为字典,通过字典再进行单一读取
import json
test = json.loads(response)
username = test['result']['name']
到这里就已经完成了这个项目
总结
上面已经简单说明了如何调用相应的接口去识别身份证了,下面我把源码给到大家吧,大家可以作为参考哦,下面是使用python+flask+redis+华为云OCR实现的
import json
from flask import Flask, render_template
import requests
from flask_redis import FlaskRedis
import base64
app = Flask(__name__)
REDIS_URL = 'redis://127.0.0.1:6379/0'
rd = FlaskRedis(app, decode_responses=True)
@app.route('/')
def hello_world():
token = rd.get('token')
if token is None:
url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
headers = {'Content-Type': 'application/json'}
payload = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "XXX",
"password": "XXX",
"domain": {
"name": "XXX"
}
}
}
},
"scope": {
"project": {
"name": "cn-north-4"
}
}
}
}
response = requests.post(url, headers=headers, json=payload)
requst = response.ok
if not requst:
error = "获取token失败"
return render_template('test.html', db=error)
token = response.headers['X-Subject-Token']
relue = rd.set('token', token, ex=60 * 1440)
if relue != 1:
error = "写入错误"
return render_template('test.html', db=error)
url = "https://ocr.cn-north-4.myhuaweicloud.com/v2/cn-north-4/ocr/id-card"
headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
imagepath = r'./static/XXX.jpg'
with open(imagepath, "rb") as bin_data:
image_data = bin_data.read()
image_base64 = base64.b64encode(image_data).decode("utf-8")
payload = {"image": image_base64}
response = requests.post(url, headers=headers, json=payload)
if not response.ok:
error = "识别失败,请联系管理员"
return render_template('test.html', db=error)
response = response.text
test = json.loads(response)
username = test['result']['name']
return render_template('test.html', db=username)
if __name__ == '__main__':
app.run()
|