Python實現AI影像識別-身份證識別

避涼閒庭發表於2021-08-17

影像識別說白了就是把一張照片上面的文字進行提取,提供工作效率

需求分析

身份證識別主要是把一張身份證照片上面的文字資訊進行提取,不用再使用人工去手動抄寫了,下面給大家說的這個身份識別主要是使用python+flask+華為雲OCR進行實現的。

步驟

  1. 申請華為雲OCR介面
  2. 獲取token
  3. 呼叫身份證識別介面
  4. 提取身份證資訊

申請華為雲OCR介面

影像識別主要使用的就是華為雲OCR平臺申請的介面,申請地址為:"https://www.huaweicloud.com"。訪問申請的地址後點選選單欄中的“控制檯”,點選後會進入登入頁面。
在這裡插入圖片描述
在這裡插入圖片描述
登入成功後進入控制檯,依次單擊“服務列表”-“人工智慧”-“文字識別OCR”
在這裡插入圖片描述
進入後點選身份證識別後面的“開通服務”即可,(這裡需要主要的是免費開通的身份證識別服務僅有1000次的免費呼叫次數,超過次數將會收費,建議購買套餐包,購買可以找我,我可以給優惠哦)

獲取token

獲取token值時你需要知道你的以下資訊:

  1. IAM使用者所屬賬號名
  2. IAM使用者名稱
  3. IAM使用者的登入密碼
  4. IAM使用者所屬賬號的專案名稱

首先就是獲取IAM使用者所屬賬號名,IAM使用者所屬賬號名為你登陸的那個賬號

獲取IAM使用者名稱,選擇右上角“使用者名稱”-點選“統一身份認證”
在這裡插入圖片描述
裡面的使用者即為IAM使用者名稱
在這裡插入圖片描述
獲取IAM使用者所屬賬號的專案名稱,點選“專案”
在這裡插入圖片描述
裡面的專案很多,你只需要記錄你開啟OCR身份證識別區域的專案名稱ID就可以了,比如我選擇的北京四所以我的專案名稱就是:“cn-north-4”在這裡插入圖片描述
知道了這些資訊之後,後面就是如何通過這些資訊去獲取token了,下面我就直接上程式碼了哦

import requests
# 下面url為華為雲獲取token的介面,這個介面基本一致,需要注意的是“iam.cn-north-4.myhuaweicloud.com”這個需要根據具體需求進行修改,不同區域專案不同
url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
# 頭部資訊
headers = {'Content-Type': 'application/json'}
# json內容,需要上傳進行做驗證
payload = {
            "auth": {
                "identity": {
                    "methods": [
                        "password"
                    ],
                    "password": {
                        "user": {
                        # name值為你的IAM賬號名
                            "name": "XXX",
                            # password值為你的IAM密碼
                            "password": "XXX",
                            "domain": {
                            # name值為你的主賬號的賬號名
                                "name": "XXX"
                            }
                        }
                    }
                },
                "scope": {
                    "project": {
                    # name值為你的專案ID
                        "name": "cn-north-4"
                    }
                }
            }
        }
# 使用POST上傳頭部內容和body內容
response = requests.post(url, headers=headers, json=payload)
# 獲取token值
token = response.headers['X-Subject-Token']
# 列印token值
print(token)

列印出來的值是類似於下面這種的字串

MIIVfgYJKoZIhvcNAQcCoIIVbzCCFWsCAQExDTALBglghkgBZQMEAgEwghOQBgkqhkiG9w0BBwGgghOBBIITfXsidG9rZW4iOns123iZXhwaXJlc19hdCI6IjIwMjEtMDgtMTdUMTE6NTM6MTEuNTkyMDAwWiIsIm1ldGhvZHMiOlsicGFzc3dvcmQiXSwiY2F0YWxvZyI6W10sInJvbGVzIjpbeyJuYW1lIjoidGVfYWRtaW4iLCJpZCI6IjAifSx7Im5hbWUiOiJ0ZV9hZ2VuY3kiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9jc2UyLjBfY2ZnIiwiaWQiOiIwIn0seyJuYW1lIjoib3BfZ2F0ZWRfZWNzX3Nwb3RfaW5zdGFuY2UiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9pdmFzX3Zjcl92Y2EiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9hX2NuLXNvdXRoLTRjIiwiaWQiOiIwIn0seyJuYW1lIjoib3BfZ2F0ZWRfZWNzX2thZTEiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9ldnNfZXNzZCIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2NzZTIuMF9ndm4iLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9kd3NfcG9jIiwiaW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BZQMEAgEwDQYJKoZIhvcNAQEBBQAEggEAJK1UkA7koUihoCyU8FwcUFpPA4x6ylmTvQDPk2KbYRMVA+gnSybo6vgxf32hcHi-1238KehFzgXhJe59fOYyc7hz+qZ5456iYD0pSGqjICZepOBeTvywtsE2fgHdlQq4RZMDm1bkVj46OVNGszzpz8IGecZxRDhxDfBbVsllo9T41QD28o9QZUNyhhXa-ZIS2UdDK-q7gBsR3mT2LRqjQ+J6vpcCzAe6v71IHAf77lgE+vwg7f6qOfhYjRNB9kSC2L1YxJ1eS9j5pzuqlnK+9BOv2U3BIaKsP9yNiP-luS78LzwuEG2rD9OInMst7bCr7HeWT2bZ8gCT4567BvsONvGqVvr3AA==

呼叫身份證識別介面

下面就是呼叫識別介面,呼叫這個介面需要了解以下資訊

  1. URL
  2. Header引數
  3. Body引數

首先是URL
POST https://{endpoint}/v2/{project_id}/ocr/id-card

引數 說明
endpoint 專案介面域名,這裡還是需要確定開通服務的區域,每個區域不同的域名
project_id 專案ID,不出意外的話就是你上面獲取token的專案ID

請求引數
請求Header引數

引數 說明
X-Auth-Token 使用者Token,就是上面獲取到的token值
Content-Type 傳送的實體的MIME型別,引數值為“application/json”

請求Body引數

引數 說明
image 影像資料,base64編碼,要求base64編碼後大小不超過10MB。圖片最小邊不小於15px,最長邊不超過8000px。支援JPEG、JPG、PNG、BMP、TIFF格式。
url 圖片的url路徑
side front:身份證人像面,back:身份證國徽面

瞭解了上面的一系列資訊之後,後面就是如何寫程式碼了,下面就是直接程式碼

import requests
import base64

# url值
url = https://{endpoint}/v2/{project_id}/ocr/id-card
# 上面獲取到的token值
token = ""
# 設定頭部資訊
headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
# 圖片路徑
imagepath = r'./static/XXX.jpg'
# 設定圖片
with open(imagepath, "rb") as bin_data:
     image_data = bin_data.read()
# 使用圖片的base64編碼
image_base64 = base64.b64encode(image_data).decode("utf-8")
# url與image引數二選一
payload = {"image": image_base64}
# 上傳引數
response = requests.post(url, headers=headers, json=payload)
# 列印識別的資訊
response = response.text
print(response)

最後識別出來的結果類似於下面的資訊:

{
  "result": {
    "name": "XXX",
    "sex": "男",
    "ethnicity": "漢",
    "birth": "XXX",
    "address": "XXX",
    "number": "XXX"
  }
}

如果想獲取其中的某一個值的話需要先轉換為字典,通過字典再進行單一讀取

import json

# 轉換為字典
test = json.loads(response)
# 從字典中獲取相應的值
username = test['result']['name']

到這裡就已經完成了這個專案

總結

上面已經簡單說明了如何呼叫相應的介面去識別身份證了,下面我把原始碼給到大家吧,大家可以作為參考哦,下面是使用python+flask+redis+華為雲OCR實現的

import json
from flask import Flask, render_template
import requests
from flask_redis import FlaskRedis
import base64

app = Flask(__name__)

# 設定redis連線地址
REDIS_URL = 'redis://127.0.0.1:6379/0'
# 例項化redis
rd = FlaskRedis(app, decode_responses=True)	


@app.route('/')
def hello_world():
	# 獲取redis快取中token
    token = rd.get('token')
    # 判斷redis中是否有token值,有則直接使用,沒有則獲取token值並寫入redis中
    if token is None:
        url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
        headers = {'Content-Type': 'application/json'}
        payload = {
            "auth": {
                "identity": {
                    "methods": [
                        "password"
                    ],
                    "password": {
                        "user": {
                            "name": "XXX",
                            "password": "XXX",
                            "domain": {
                                "name": "XXX"
                            }
                        }
                    }
                },
                "scope": {
                    "project": {
                        "name": "cn-north-4"
                    }
                }
            }
        }
        response = requests.post(url, headers=headers, json=payload)
        requst = response.ok
        # 判斷是否獲取token值成功
        if not requst:
            error = "獲取token失敗"
            return render_template('test.html', db=error)
        # 從返回值中獲取token值    
        token = response.headers['X-Subject-Token']
        # 在redis新增token值,下次不再呼叫,並設定快取時間為24小時
        relue = rd.set('token', token, ex=60 * 1440)
        # 判斷是否再redis中新增token值成功
        if relue != 1:
            error = "寫入錯誤"
            return render_template('test.html', db=error)
	
    url = "https://ocr.cn-north-4.myhuaweicloud.com/v2/cn-north-4/ocr/id-card"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}

    imagepath = r'./static/XXX.jpg'
    with open(imagepath, "rb") as bin_data:
        image_data = bin_data.read()
    image_base64 = base64.b64encode(image_data).decode("utf-8")  # 使用圖片的base64編碼
    payload = {"image": image_base64}  # url與image引數二選一
    response = requests.post(url, headers=headers, json=payload)
    # 判斷是否識別成功
    if not response.ok:
        error = "識別失敗,請聯絡管理員"
        return render_template('test.html', db=error)
    response = response.text
    test = json.loads(response)
    username = test['result']['name']
    return render_template('test.html', db=username)


if __name__ == '__main__':
    app.run()

相關文章