百度API之身份證識別


 

今天的任務是調用百度的API,實現身份證識別,然后我糟了,感覺有點難,開啟百度模式,百度了半天,站到了別人寫的代碼,復制then粘貼→運行,成功了,可是不是自己的,於是在老師的指導下,閱讀了百度給出的官方文檔,決定自己寫一下。

步驟如下:

1.注冊登錄

瀏覽器搜索百度AI智能開放平台,進入官網注冊登錄

2.找到如下圖,點擊身份證識別

3.點擊立即使用

4.創建應用

5.填寫信息。點擊立即創建

6.返回應用列表

7.創建成功,注意圖中標出的Appid, API Key ,secret Key

8.下面進入代碼部分

環境依賴 requests,base65 庫

下載requests庫

pip install requests

推薦使用清華源,下載速度快

pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

使用python代碼如下,其他語言請參考https://ai.baidu.com/ai-doc/OCR/rk3h7xzck

import requests
import base64
​
'''
身份證識別
'''
​
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
# 二進制方式打開圖片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
​
params = {"id_card_side":"front","image":img}
access_token = '[調用鑒權接口獲取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())

注意文件路徑f = open('[本地文件]', 'rb')

params = {"id_card_side":"front","image":img}
id_card_side的參數front為身份證正面,背面為back

獲取Access Token

代碼中三個參數

  • grant_type: 必須參數,固定為client_credentials

  • client_id: 必須參數,應用的API Key

  • client_secret: 必須參數,應用的Secret Key

import requests 
​
# client_id 為官網獲取的AK, client_secret 為官網獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官網獲取的AK】&client_secret=【官網獲取的SK】'
response = requests.get(host)
if response:
    print(response.json())

例如

import requests
import base64
​
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=B5zZqwl2cO3b4lcWlPKoUuRO&client_secret=v4ZDzz8uT1pFaZkbOGLj5hSYupV7njKR'
response = requests.get(host)
if response:
    taken = response.json()

代碼運行結果為

{
  "refresh_token": "25.b55fe1d287227ca97aab219bb249b8ab.315360000.1798284651.282335-8574074",
  "expires_in": 2592000,
  "scope": "public wise_adapt",
  "session_key": "9mzdDZXu3dENdFZQurfg0Vz8slgSgvvOAUebNFzyzcpQ5EnbxbF+hfG9DQkpUVQdh4p6HbQcAiz5RmuBAja1JJGgIdJI",
  "access_token": "24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074",
  "session_secret": "dfac94a3489fe9fca7c3221cbf7525ff"
}

獲取access_token:taken['access_token']

最后,展示完整代碼

import requests
import base64
​
# client_id 為官網獲取的AK, client_secret 為官網獲取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=B5zZqwl2cO3b4lcWlPKoUuRO&client_secret=v4ZDzz8uT1pFaZkbOGLj5hSYupV7njKR'
response = requests.get(host)
if response:
    taken = response.json()
    print(taken)
    print(taken['access_token'])
    '''
    身份證識別
    '''
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
    f = open('E:\plus班上課\模擬教務系統\db\card\張笑輝-正面.png', 'rb')
    img = base64.b64encode(f.read())
    params = {"id_card_side": "front", "image": img}
    access_token = taken['access_token']
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        res = response.json()
        print(res)

返回的json字典,根據需要自行提取字典中的數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM