【百度大脑新品体验】驾驶证识别
用户已被禁言 发布于2019-04 浏览:2713 回复:4
1
收藏
最后编辑于2022-04

话不多说,直奔主题……

一.平台接入

此步骤比较简单,不多阐述。可参照上篇文档:

https://ai.baidu.com/forum/topic/show/943028

二.分析接口文档

1.打开https://ai.baidu.com/docs#/OCR-API/6a5f6b1b

   (1)接口描述

    支持对机动车驾驶证正页所有关键字段进行识别。

    (2)请求说明

    需要用到的信息有:

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

    Header格式:Content-Type:application/x-www-form-urlencoded

    请求参数:image, 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式 。注意:图片需要base64编码、去掉编码头后再进行urlencode。

  detect_direction:是否检测图像朝向,默认不检测,即:false。置为true,则可对非正向图片进行检测并纠正。

    (3)返回示例

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"

        }

      }

   }

}

2.获取access_token

#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】

#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content.decode("utf-8"))
token_key = token_info['access_token']
return token_key
3.准备一张用于识别的图片,存储在本地

三.识别结果

四.源码共享

# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
import urllib.parse
import urllib.request
import base64
import json
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id = '****************'
client_secret = '********************'

#获取token
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    token_content = response.read()
    if token_content:
        token_info = json.loads(token_content.decode("utf-8"))
        token_key = token_info['access_token']
    return token_key

     # 读取图片
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


#获取车牌号信息
def get_license_plate(path):

    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license"
    
    f = get_file_content(path)
    access_token=get_token()
    img = base64.b64encode(f)
    params = {"custom_lib": False, "image": img}
    params = urllib.parse.urlencode(params).encode('utf-8')
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/x-www-form-urlencoded')
    response = urllib.request.urlopen(request)
    content = response.read()
    if content:
        driving_licenses = json.loads(content.decode("utf-8"))
        strover = '识别结果:\n'
        words_result = driving_licenses['words_result']
        # 姓名
        name = words_result['姓名']['words']
        strover += '  姓名: {} \n '.format(name)
        # 国籍
        nationality = words_result['国籍']['words']
        strover += ' 国籍: {} \n '.format(nationality)
        # 出生日期
        date_of_birth = words_result['出生日期']['words']
        strover += ' 出生日期: {} \n '.format(date_of_birth)
        # 性别
        sex = words_result['性别']['words']
        strover += ' 性别: {} \n '.format(sex)
        # 证号
        Citizenship_number = words_result['证号']['words']
        strover += ' 证号: {} \n '.format(Citizenship_number)
        # 准驾车型
        Full_name = words_result['准驾车型']['words']
        strover += ' 准驾车型:{} \n '.format(Full_name)

        # 初次领证日期
        first_certificate_date = words_result['初次领证日期']['words']
        strover += ' 初次领证日期: {} \n '.format(first_certificate_date)
        # 有效期限
        Nation = words_result['有效期限']['words']
        # 到期日期
        to = words_result['至']['words']
        strover += ' 有效期限:{}至{}\n '.format(Nation, to)
        # 住址
        address = words_result['住址']['words']
        strover += ' 住址:{} \n '.format(address)
#        print (content)
        print (strover)
        return content
    else:
        return ''

image_path='F:\paddle\jszh.jpg'
get_license_plate(image_path)

 

 

收藏
点赞
1
个赞
共4条回复 最后由用户已被禁言回复于2022-04
#5用户已被禁言回复于2019-05
#3 小雨青青润无声回复
以后各种证件能迅速识别真伪了

辨真伪有一定难度

0
#4小雨青青润无声回复于2019-05
#2 用户已被禁言回复
OCR识别准确率很高了

这种识别基数应用很广泛

0
#3小雨青青润无声回复于2019-05

以后各种证件能迅速识别真伪了

0
#2用户已被禁言回复于2019-05

OCR识别准确率很高了

0
TOP
切换版块