Class 2 身份证识别系统搭建

一、基本诉求

能够通过调用阿里达摩院视觉平台提供的API来识别出二代身份证关键字段内容,关键字段包括:姓名、性别、民族、身份证号、出生日期、地址信息、有效起始时间、签发机关,同时可输出身份证区域位置和人脸位置信息。

二、项目预览

1.进入页面

2.上传身份证照片信息界面。

3.识别之后的页面

4.源码展示

三、前期准备

1.查看官方说明文档

进入阿里达摩院视觉识别平台(https://vision.aliyun.com/),选择能力广场>文字识别>身份证识别。

2.查看SDK实例文档

3.获取OCR的SDK的Maven坐标(Java)

传送门

通过文字识别中查看产品文档,得知SDK支持包括Java、Java(本地接入)、Python、Node.js、PHP四种常用语言。通过检索Java(支持本地上传)的SDK,获取OCR的Maven代码(version=1.0.3):

1
2
<artifactId>ocr</artifactId>
<version>1.0.3</version>

四、框架分析

建立Springboot项目,Application为启动类;Controller为控制器层,负责模板的渲染和路由等;Ocrservice负责通过SDK调用视觉识别的能力。上传表单只允许后缀为.jpg,.jpeg,.png的文件,且不为空。

1.输入限制:

  • 图片格式:JPEG、JPG、PNG、BMP、GIF。
  • 图像大小:图像大小不超过3M。
  • 图像分辨率:不限制图片分辨率,但图片分辨率太高可能会导致API识别超时,超时时间为5秒。
  • URL地址中不能包含中文字符。

五、示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.ocr.model.v20191230.*;

public class RecognizeIdentityCard {

public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);

RecognizeIdentityCardRequest request = new RecognizeIdentityCardRequest();
request.setRegionId("cn-shanghai");
request.setSide("face");

try {
RecognizeIdentityCardResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}

}
}

代码是Java的srping-boot项目

项目代码下载地址:https://github.com/aliyun/alibabacloud-viapi-demo?spm=a2c6h.12873639.0.0.c08529f2JzpdtR

六、python代码

1.准备工作

在安装和使用阿里云SDK前,确保已经注册阿里云账号并生成访问密钥(AccessKey)。

2.安装Python SDK核心库。

该SDK包为阿里云Java核心库,无论使用哪个产品的SDK,都必须先#

3.安装该核心库。

使用依赖包工具安装(推荐)。

4.执行如下命令,安装阿里云SDK核心库。

1
pip install aliyun-python-sdk-core

5.自行下载安装:使用git clone或其它方式从GitHub下载aliyun-python-sdk-core并自行添加解决方案。

6.安装视觉智能API相关服务Python SDK。

使用依赖包工具安装(推荐)。

7.执行以下命令,安装需要的SDK包。

仅安装所需要的SDK包即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
人脸人体:pip install aliyun-python-sdk-facebody
文字识别:pip install aliyun-python-sdk-ocr
商品理解:pip install aliyun-python-sdk-goodstech
内容安全:pip install aliyun-python-sdk-imageaudit
图像识别:pip install aliyun-python-sdk-imagerecog
图像生产:pip install aliyun-python-sdk-imageenhan
分割抠图:pip install aliyun-python-sdk-imageseg
目标检测:pip install aliyun-python-sdk-objectdet
图像分析处理:pip install aliyun-python-sdk-imageprocess
视觉搜索:pip install aliyun-python-sdk-imgsearch
视频理解:pip install aliyun-python-sdk-videorecog
视频生产:pip install aliyun-python-sdk-videoenhan
视频分割:pip install aliyun-python-sdk-videoseg
自行下载安装:使用git clone或其它方式下载SDK包并自行添加解决方案。详细下载地址如下所示。
AI类目 SDK链接 pypi链接
人脸人体 aliyun-python-sdk-facebody aliyun-python-sdk-facebody
文字识别 aliyun-python-sdk-ocr aliyun-python-sdk-ocr
商品理解 aliyun-python-sdk-goodstech aliyun-python-sdk-goodstech
内容安全 aliyun-python-sdk-imageaudit aliyun-python-sdk-imageaudit
图像识别 aliyun-python-sdk-imagerecog aliyun-python-sdk-imagerecog
图像生产 aliyun-python-sdk-imageenhan aliyun-python-sdk-imageenhan
分割抠图 aliyun-python-sdk-imageseg aliyun-python-sdk-imageseg
目标检测 aliyun-python-sdk-objectdet aliyun-python-sdk-objectdet
视觉搜索 aliyun-python-sdk-imgsearch aliyun-python-sdk-imgsearch
图像分析处理 aliyun-python-sdk-imageprocess aliyun-python-sdk-imageprocess
视频生产 aliyun-python-sdk-videoenhan aliyun-python-sdk-videoenhan
视频理解 aliyun-python-sdk-videorecog aliyun-python-sdk-videorecog
视频分割 aliyun-python-sdk-videoseg aliyun-python-sdk-videoseg

8.代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# -*- coding: utf8 -*-
from aliyunsdkcore.client import AcsClient
from aliyunsdkimageenhan.request.v20190930 import MakeSuperResolutionImageRequest
from aliyunsdkimageenhan.request.v20190930 import ExtendImageStyleRequest
from aliyunsdkimageenhan.request.v20190930 import RecolorImageRequest
from aliyunsdkimageenhan.request.v20190930 import ChangeImageSizeRequest
from aliyunsdkimagerecog.request.v20190930 import RecognizeImageColorRequest
from aliyunsdkimagerecog.request.v20190930 import RecognizeImageStyleRequest
from aliyunsdkimagerecog.request.v20190930 import DetectImageElementsRequest
from aliyunsdkimagerecog.request.v20190930 import TaggingImageRequest
from aliyunsdkimagerecog.request.v20190930 import RecognizeSceneRequest
from aliyunsdkfacebody.request.v20191230 import DetectFaceRequest
from aliyunsdkfacebody.request.v20191230 import RecognizeFaceRequest
from aliyunsdkfacebody.request.v20191230 import CompareFaceRequest
from aliyunsdkocr.request.v20191230 import RecognizeCharacterRequest
from aliyunsdkocr.request.v20191230 import RecognizeTableRequest
from aliyunsdkocr.request.v20191230 import RecognizeIdentityCardRequest
from aliyunsdkocr.request.v20191230 import RecognizeBankCardRequest
from aliyunsdkocr.request.v20191230 import RecognizeBusinessCardRequest
from aliyunsdkocr.request.v20191230 import RecognizeAccountPageRequest
from aliyunsdkocr.request.v20191230 import RecognizeDriverLicenseRequest
from aliyunsdkocr.request.v20191230 import RecognizeDrivingLicenseRequest
from aliyunsdkocr.request.v20191230 import RecognizeLicensePlateRequest
from aliyunsdkocr.request.v20191230 import RecognizeVINCodeRequest
from aliyunsdkocr.request.v20191230 import RecognizeTaxiInvoiceRequest
from aliyunsdkocr.request.v20191230 import RecognizeTrainTicketRequest
from aliyunsdkocr.request.v20191230 import RecognizeBusinessLicenseRequest
from aliyunsdkocr.request.v20191230 import RecognizeStampRequest
from aliyunsdkgoodstech.request.v20191230 import ClassifyCommodityRequest
from aliyunsdkimageaudit.request.v20191230 import ScanImageRequest
from aliyunsdkimageseg.request.v20191230 import SegmentCommonImageRequest
from aliyunsdkimageseg.request.v20191230 import SegmentBodyRequest
from aliyunsdkimageseg.request.v20191230 import SegmentCommodityRequest
from aliyunsdkobjectdet.request.v20191230 import DetectMainBodyRequest
from aliyunsdkobjectdet.request.v20191230 import DetectVehicleRequest

# 创建 AcsClient 实例
client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "cn-shanghai")

# 清晰化/超分辨率
request = MakeSuperResolutionImageRequest.MakeSuperResolutionImageRequest()
request.set_Url("http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/MakeSuperResolution/sup-dog.png")
response = client.do_action_with_exception(request)
print(response)

# 尺寸变换
request = ChangeImageSizeRequest.ChangeImageSizeRequest()
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png")
request.set_Width(500)
request.set_Height(600)
response = client.do_action_with_exception(request)
print(response)

# 图像元素检测
request = DetectImageElementsRequest.DetectImageElementsRequest()
request.set_Url("http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/DetectImageElements/detect-elements-src.png")
response = client.do_action_with_exception(request)
print(response)

# 风格迁移
request = ExtendImageStyleRequest.ExtendImageStyleRequest()
request.set_StyleUrl("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ExtendImageStyle/styleUrl.jpeg")
request.set_MajorUrl("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ExtendImageStyle/majorUrl.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 色板识别
request = RecognizeImageColorRequest.RecognizeImageColorRequest()
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png")
request.set_ColorCount(3)
response = client.do_action_with_exception(request)
print(response)

# 风格识别
request = RecognizeImageStyleRequest.RecognizeImageStyleRequest()
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeImageStyle/technology.png")
response = client.do_action_with_exception(request)
print(response)

# 色彩迁移
request = RecolorImageRequest.RecolorImageRequest()
#自动拓色
request.set_Mode("AUTO")
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png")
request.set_ColorCount(3)
response = client.do_action_with_exception(request)
print(response)
#参考图拓色
request.set_Mode("REF_PIC")
request.set_RefUrl("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-1-src.png")
response = client.do_action_with_exception(request)
print(response)
#色板拓色
request.set_Mode("TEMPLATE")
request.set_Url("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecolorImage/recolor-2-src.png")
colorTemplateList = []
colorTemplateList.append({"Color": "FF0000"})
colorTemplateList.append({"Color": "00FF00"})
colorTemplateList.append({"Color": "0000FF"})
request.set_ColorTemplates(colorTemplateList)
response = client.do_action_with_exception(request)
print(response)

# 通用图像打标
request = TaggingImageRequest.TaggingImageRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 场景识别
request = RecognizeSceneRequest.RecognizeSceneRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 人脸检测定位
request = DetectFaceRequest.DetectFaceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmengImage.png")
response = client.do_action_with_exception(request)
print(response)

# 人脸属性识别
request = RecognizeFaceRequest.RecognizeFaceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 人脸比对
request = CompareFaceRequest.CompareFaceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURLA("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
request.set_ImageURLB("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 通用识别
request = RecognizeCharacterRequest.RecognizeCharacterRequest();
request.set_MinHeight(10);
request.set_OutputProbability(True);
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai-internal.aliyuncs.com/viapi-demo/images/RecognizeCharacter/recognizeCharacter_demo.jpg")
response = client.do_action_with_exception(request)
print(response)

# 表格识别
request = RecognizeTableRequest.RecognizeTableRequest();
request.set_UseFinanceModel(False);
request.set_AssureDirection(False);
request.set_HasLine(False);
request.set_SkipDetection(False);
request.set_OutputFormat("json");
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 身份证识别
request = RecognizeIdentityCardRequest.RecognizeIdentityCardRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeIdentityCard/identityCard.jpg")
request.set_Side("face")
response = client.do_action_with_exception(request)
print(response)

# 银行卡识别
request = RecognizeBankCardRequest.RecognizeBankCardRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeBankCard/bankcard.jpg")
response = client.do_action_with_exception(request)
print(response)

# 名片识别
request = RecognizeBusinessCardRequest.RecognizeBusinessCardRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 户口页识别
request = RecognizeAccountPageRequest.RecognizeAccountPageRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 驾驶证识别
request = RecognizeDriverLicenseRequest.RecognizeDriverLicenseRequest();
request.set_Side("face")
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 行驶证识别
request = RecognizeDrivingLicenseRequest.RecognizeDrivingLicenseRequest();
request.set_Side("face")
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 车牌识别
request = RecognizeLicensePlateRequest.RecognizeLicensePlateRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# VIN码识别
request = RecognizeVINCodeRequest.RecognizeVINCodeRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 出租车发票识别
request = RecognizeTaxiInvoiceRequest.RecognizeTaxiInvoiceRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 火车票识别
request = RecognizeTrainTicketRequest.RecognizeTrainTicketRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 营业执照识别
request = RecognizeBusinessLicenseRequest.RecognizeBusinessLicenseRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 公章识别
request = RecognizeStampRequest.RecognizeStampRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

# 商品分类
request = ClassifyCommodityRequest.ClassifyCommodityRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 图片智能鉴黄
request = ScanImageRequest.ScanImageRequest();
scenes = []
scenes.append("porn")
request.set_Scenes(scenes)
tasks = []
## 如下url替换为自有的上海region的oss文件地址
tasks.append({"DataId":"<your-data-id>", "ImageURL":"https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/ChangeImageSize/change-image-size-src.png"})
request.set_Tasks(tasks)
response = client.do_action_with_exception(request)
print(response)

# 通用分割
request = SegmentCommonImageRequest.SegmentCommonImageRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 人像分割
request = SegmentBodyRequest.SegmentBodyRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentBody/%E4%BA%BA%E5%83%8F%E5%88%86%E5%89%B2.png")
response = client.do_action_with_exception(request)
print(response)

# 商品分割
request = SegmentCommodityRequest.SegmentCommodityRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommodity/%E5%95%86%E5%93%81.png")
response = client.do_action_with_exception(request)
print(response)

# 主体检测
request = DetectMainBodyRequest.DetectMainBodyRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/SegmentCommonImage/segmentimage-src-hu.jpeg")
response = client.do_action_with_exception(request)
print(response)

# 机动车检测
request = DetectVehicleRequest.DetectVehicleRequest();
## 如下url替换为自有的上海region的oss文件地址
request.set_ImageURL("https://xxx.oss-cn-shanghai.aliyuncs.com/xxx.jpg")
response = client.do_action_with_exception(request)
print(response)

参考


Class 2 身份证识别系统搭建
https://blog.baixf.tk/2020/11/28/阿里云/Class 2 身份证识别系统搭建/
作者
白小飞
发布于
2020年11月28日
许可协议