【六期】驾驶证识别
风搅火 发布于2019-11 浏览:2561 回复:1
0
收藏
最后编辑于2022-04

支持对机动车驾驶证正本所有9个字段进行结构化识别,包括证号、姓名、性别、国籍、住址、出生日期、初次领证日期、准驾车型、有效期限

功能接入:
进入百度AI管理界面:https://console.bce.baidu.com/
进入界面后点击“创建应用”

输入应用信息及需要的AI功能即可。


百度现在提供每天一定量的免费调用额度,足够大家测试使用了。


获取Access Token
调用驾驶证识别功能前需要先调用鉴权API,确保安全。
具体说明请查阅:http://ai.baidu.com/docs#/Auth/top

请求URL数据格式
向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:
grant_type: 必须参数,固定为client_credentials;
client_id: 必须参数,应用的API Key;
client_secret: 必须参数,应用的Secret Key;
例如:
https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&


驾驶证识别AP调用
https://ai.baidu.com/docs#/OCR-API-DrivingLicense/top

接口描述
对机动车驾驶证所有关键字段进行识别

请求说明
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license

具体参数:


HTTP/1.1 200 OK
x-bce-request-id: 73c4e74c-3101-4a00-bf44-fe246959c05e
Cache-Control: no-cache
Server: BWS
Date: Tue, 18 Oct 2016 02:21:01 GMT
Content-Type: application/json;charset=UTF-8
{
"errno": 0,
"msg": "success",
"data": {
"words_result_num": 10,
"words_result": {
"证号": {
"words": "3208231999053090"
},
"有效期限": {
"words": "6年"
},
"准驾车型": {
"words": "B2"
},
"有效起始日期": {
"words": "20101125"
},
"住址": {
"words": "江苏省南通市海门镇秀山新城"
},
"姓名": {
"words": "小欧欧"
},
"国籍": {
"words": "中国"
},
"出生日期": {
"words": "19990530"
},
"性别": {
"words": "男"
},
"初次领证日期": {
"words": "20100125"
}
}
}
}


详细代码(C#):

public static class AccessToken
{
// 调用getAccessToken()获取的 access_token建议根据expires_in 时间 设置缓存
// 返回token示例
// 百度云中开通对应服务应用的 API Key 建议开通应用的时候多选服务
private static String clientId = "你获得的ID";
// 百度云中开通对应服务应用的 Secret Key
private static String clientSecret = "你获得的KEY";

public static String getAccessToken() {
String authHost = "https://aip.baidubce.com/oauth/2.0/token";
HttpClient client = new HttpClient();
List> paraList = new List>();
paraList.Add(new KeyValuePair("grant_type", "client_credentials"));
paraList.Add(new KeyValuePair("client_id", clientId));
paraList.Add(new KeyValuePair("client_secret", clientSecret));

HttpResponseMessage response = client.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
JObject jobject = (JObject)JsonConvert.DeserializeObject(result);
string token = jobject["access_token"].ToString();
return token;
//return result;
}
public class Program
{
// 驾驶证识别
public static string drivingLicense(String imagefilename)
{
string token = AccessToken.getAccessToken();
Console.WriteLine(token);
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
string base64 = getFileBase64(imagefilename);
String str = "image=" + HttpUtility.UrlEncode(base64);
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("结果:");
Console.WriteLine(result);
return result;
}

public static String getFileBase64(String fileName) {
FileStream filestream = new FileStream(fileName, FileMode.Open);
byte[] arr = new byte[filestream.Length];
filestream.Read(arr, 0, (int)filestream.Length);
string baser64 = Convert.ToBase64String(arr);
filestream.Close();
return baser64;
}
}

功能测试(所有测试图片都来自网络):


分析结果:

{'log_id': 7507699211576127096, 'words_result_num': 10, 'words_result': {'证号': {'words': '342222199412019841'}, '有效期限': {'words': '20170821'}, '准驾车型': {'words': 'C1'}, '住址': {'words': '广二东省深圳市南山区高新南四道10号'}, '至': {'words': '20230821'}, '姓名': {'words': '黄文文'}, '国籍': {'words': '中国'}, '出生日期': {'words': '19900420'}, '性别': {'words': '女'}, '初次领证日期': {'words': '20170821'}}}

测试结论及建议
测试后发现效果很好,调用速度很快,结果准确,可以大大的提高人们的工作效率。

收藏
点赞
0
个赞
共1条回复 最后由用户已被禁言回复于2022-04
#2洹樾回复于2020-06

随便提交任意一张图片也能返回驾驶证信息?

0
TOP
切换版块