主頁 > 知識庫 > Python中AI圖像識別實現(xiàn)身份證識別

Python中AI圖像識別實現(xiàn)身份證識別

熱門標簽:長沙高頻外呼系統(tǒng)原理是什么 百度地圖標注沒有了 地圖標注審核表 ai電話機器人哪里好 外呼并發(fā)線路 湛江智能外呼系統(tǒng)廠家 西藏房產(chǎn)智能外呼系統(tǒng)要多少錢 ai電銷機器人源碼 宿遷星美防封電銷卡

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

需求分析

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

步驟

  • 申請華為云OCR接口
  • 獲取token
  • 調(diào)用身份證識別接口
  • 提取身份證信息

申請華為云OCR接口

圖像識別主要使用的就是華為云OCR平臺申請的接口,申請地址為:“https://www.huaweicloud.com”。訪問申請的地址后點擊菜單欄中的“控制臺”,點擊后會進入登錄頁面。



登錄成功后進入控制臺,依次單擊“服務列表”-“人工智能”-“文字識別OCR”


進入后點擊身份證識別后面的“開通服務”即可,(這里需要主要的是免費開通的身份證識別服務僅有1000次的免費調(diào)用次數(shù),超過次數(shù)將會收費,建議購買套餐包,購買可以找我,我可以給優(yōu)惠哦)

獲取token

獲取token值時你需要知道你的以下信息:

  • IAM用戶所屬賬號名
  • IAM用戶名
  • IAM用戶的登錄密碼
  • IAM用戶所屬賬號的項目名稱

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

獲取IAM用戶名,選擇右上角“用戶名”-點擊“統(tǒng)一身份認證”


里面的用戶即為IAM用戶名


獲取IAM用戶所屬賬號的項目名稱,點擊“項目”


里面的項目很多,你只需要記錄你開啟OCR身份證識別區(qū)域的項目名稱ID就可以了,比如我選擇的北京四所以我的項目名稱就是:“cn-north-4”


知道了這些信息之后,后面就是如何通過這些信息去獲取token了,下面我就直接上代碼了哦

import requests
# 下面url為華為云獲取token的接口,這個接口基本一致,需要注意的是“iam.cn-north-4.myhuaweicloud.com”這個需要根據(jù)具體需求進行修改,不同區(qū)域項目不同
url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
# 頭部信息
headers = {'Content-Type': 'application/json'}
# json內(nèi)容,需要上傳進行做驗證
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上傳頭部內(nèi)容和body內(nèi)容
response = requests.post(url, headers=headers, json=payload)
# 獲取token值
token = response.headers['X-Subject-Token']
# 打印token值
print(token)

打印出來的值是類似于下面這種的字符串

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

調(diào)用身份證識別接口

下面就是調(diào)用識別接口,調(diào)用這個接口需要了解以下信息

  • URL
  • Header參數(shù)
  • Body參數(shù)

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

參數(shù) 說明
endpoint 項目接口域名,這里還是需要確定開通服務的區(qū)域,每個區(qū)域不同的域名
project_id 項目ID,不出意外的話就是你上面獲取token的項目ID

請求參數(shù)
請求Header參數(shù)

參數(shù) 說明
X-Auth-Token 用戶Token,就是上面獲取到的token值
Content-Type 發(fā)送的實體的MIME類型,參數(shù)值為“application/json”

請求Body參數(shù)

參數(shù) 說明
image 圖像數(shù)據(jù),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參數(shù)二選一
payload = {"image": image_base64}
# 上傳參數(shù)
response = requests.post(url, headers=headers, json=payload)
# 打印識別的信息
response = response.text
print(response)

最后識別出來的結(jié)果類似于下面的信息:

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

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

import json

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

到這里就已經(jīng)完成了這個項目

總結(jié)

上面已經(jīng)簡單說明了如何調(diào)用相應的接口去識別身份證了,下面我把源碼給到大家吧,大家可以作為參考哦,下面是使用python+flask+redis+華為云OCR實現(xiàn)的

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值,下次不再調(diào)用,并設置緩存時間為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參數(shù)二選一
    response = requests.post(url, headers=headers, json=payload)
    # 判斷是否識別成功
    if not response.ok:
        error = "識別失敗,請聯(lián)系管理員"
        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()

到此這篇關于Python中AI圖像識別實現(xiàn)身份證識別的文章就介紹到這了,更多相關Python AI圖像識別 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 基于Python的身份證驗證識別和數(shù)據(jù)處理詳解
  • Python+Opencv身份證號碼區(qū)域提取及識別實現(xiàn)

標簽:南平 海南 林芝 盤錦 寧夏 普洱 漯河 大同

巨人網(wǎng)絡通訊聲明:本文標題《Python中AI圖像識別實現(xiàn)身份證識別》,本文關鍵詞  Python,中,圖像,識別,實現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python中AI圖像識別實現(xiàn)身份證識別》相關的同類信息!
  • 本頁收集關于Python中AI圖像識別實現(xiàn)身份證識別的相關信息資訊供網(wǎng)民參考!
  • 推薦文章