Class 2 - 身份证识别系统搭建-阿里云开发者社区

开发者社区> 阿里云高校学研支持计划> 正文

Class 2 - 身份证识别系统搭建

简介: 阿里云高校计划视觉AI五天训练营教程

一、基本诉求

能够通过调用阿里达摩院视觉平台提供的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):

<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地址中不能包含中文字符。

五、示例代码

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核心库。

pip install aliyun-python-sdk-core

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

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

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

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

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

人脸人体: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.代码示例

# -*- 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)

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
阿里云高校学研支持计划
使用钉钉扫一扫加入圈子
+ 订阅

阿里云高校学研支持计划旨在为高等院校师生提供包括算力、培训、平台 等全方位支持,助推高校计算机学科建设、培养数字经济时代的前沿技术人才、为多学科跨 领域科研探索提速增效。目前,我们已经开启了学生成长计划、教学支持计划和科研加速 三大板块。精彩内容,先睹为快。

官方博客
申请加入高校学研支持计划
友情链接