Python中AI圖像識(shí)別實(shí)現(xiàn)身份證識(shí)別
圖像識(shí)別說白了就是把一張照片上面的文字進(jìn)行提取,提供工作效率
需求分析
身份證識(shí)別主要是把一張身份證照片上面的文字信息進(jìn)行提取,不用再使用人工去手動(dòng)抄寫了,下面給大家說的這個(gè)身份識(shí)別主要是使用python+flask+華為云OCR進(jìn)行實(shí)現(xiàn)的。
步驟
- 申請(qǐng)華為云OCR接口
- 獲取token
- 調(diào)用身份證識(shí)別接口
- 提取身份證信息
申請(qǐng)華為云OCR接口
圖像識(shí)別主要使用的就是華為云OCR平臺(tái)申請(qǐng)的接口,申請(qǐng)地址為:“https://www.huaweicloud.com”。訪問申請(qǐng)的地址后點(diǎn)擊菜單欄中的“控制臺(tái)”,點(diǎn)擊后會(huì)進(jìn)入登錄頁面。
登錄成功后進(jìn)入控制臺(tái),依次單擊“服務(wù)列表”-“人工智能”-“文字識(shí)別OCR”
進(jìn)入后點(diǎn)擊身份證識(shí)別后面的“開通服務(wù)”即可,(這里需要主要的是免費(fèi)開通的身份證識(shí)別服務(wù)僅有1000次的免費(fèi)調(diào)用次數(shù),超過次數(shù)將會(huì)收費(fèi),建議購買套餐包,購買可以找我,我可以給優(yōu)惠哦)
獲取token
獲取token值時(shí)你需要知道你的以下信息:
- IAM用戶所屬賬號(hào)名
- IAM用戶名
- IAM用戶的登錄密碼
- IAM用戶所屬賬號(hào)的項(xiàng)目名稱
首先就是獲取IAM用戶所屬賬號(hào)名,IAM用戶所屬賬號(hào)名為你登陸的那個(gè)賬號(hào)
獲取IAM用戶名,選擇右上角“用戶名”-點(diǎn)擊“統(tǒng)一身份認(rèn)證”
里面的用戶即為IAM用戶名
獲取IAM用戶所屬賬號(hào)的項(xiàng)目名稱,點(diǎn)擊“項(xiàng)目”
里面的項(xiàng)目很多,你只需要記錄你開啟OCR身份證識(shí)別區(qū)域的項(xiàng)目名稱ID就可以了,比如我選擇的北京四所以我的項(xiàng)目名稱就是:“cn-north-4”
知道了這些信息之后,后面就是如何通過這些信息去獲取token了,下面我就直接上代碼了哦
import requests # 下面url為華為云獲取token的接口,這個(gè)接口基本一致,需要注意的是“iam.cn-north-4.myhuaweicloud.com”這個(gè)需要根據(jù)具體需求進(jìn)行修改,不同區(qū)域項(xiàng)目不同 url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens" # 頭部信息 headers = {'Content-Type': 'application/json'} # json內(nèi)容,需要上傳進(jìn)行做驗(yàn)證 payload = { "auth": { "identity": { "methods": ["password" ], "password": {"user": {# name值為你的IAM賬號(hào)名 "name": "XXX", # password值為你的IAM密碼 "password": "XXX", "domain": { # name值為你的主賬號(hào)的賬號(hào)名 "name": "XXX" }} } }, "scope": { "project": { # name值為你的項(xiàng)目ID"name": "cn-north-4" } } } } # 使用POST上傳頭部?jī)?nèi)容和body內(nèi)容 response = requests.post(url, headers=headers, json=payload) # 獲取token值 token = response.headers['X-Subject-Token'] # 打印token值 print(token)
打印出來的值是類似于下面這種的字符串
MIIVfgYJKoZIhvcNAQcCoIIVbzCCFWsCAQExDTALBglghkgBZQMEAgEwghOQBgkqhkiG9w0BBwGgghOBBIITfXsidG9rZW4iOns123iZXhwaXJlc19hdCI6IjIwMjEtMDgtMTdUMTE6NTM6MTEuNTkyMDAwWiIsIm1ldGhvZHMiOlsicGFzc3dvcmQiXSwiY2F0YWxvZyI6W10sInJvbGVzIjpbeyJuYW1lIjoidGVfYWRtaW4iLCJpZCI6IjAifSx7Im5hbWUiOiJ0ZV9hZ2VuY3kiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9jc2UyLjBfY2ZnIiwiaWQiOiIwIn0seyJuYW1lIjoib3BfZ2F0ZWRfZWNzX3Nwb3RfaW5zdGFuY2UiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9pdmFzX3Zjcl92Y2EiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9hX2NuLXNvdXRoLTRjIiwiaWQiOiIwIn0seyJuYW1lIj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TjESMBAGA1UECAwJR3VhbmdEb25nMREwDwYDVQQH123DAhTaGVuWmhlbjEuMCwGA1UECgwlSHVhd2VpIFNvZnR3YXJlIFRlY2hub2xvZ2llcyBDby4sIEx0ZDEOMAwGA1UECwwFQ2xvdWQxEzARBgNVBAMMCmNhLmlhbS5wa2kCCQDcsytdEGFqEDALBglghkgBZQMEAgEwDQYJKoZIhvcNAQEBBQAEggEAJK1UkA7koUihoCyU8FwcUFpPA4x6ylmTvQDPk2KbYRMVA+gnSybo6vgxf32hcHi-1238KehFzgXhJe59fOYyc7hz+qZ5456iYD0pSGqjICZepOBeTvywtsE2fgHdlQq4RZMDm1bkVj46OVNGszzpz8IGecZxRDhxDfBbVsllo9T41QD28o9QZUNyhhXa-ZIS2UdDK-q7gBsR3mT2LRqjQ+J6vpcCzAe6v71IHAf77lgE+vwg7f6qOfhYjRNB9kSC2L1YxJ1eS9j5pzuqlnK+9BOv2U3BIaKsP9yNiP-luS78LzwuEG2rD9OInMst7bCr7HeWT2bZ8gCT4567BvsONvGqVvr3AA==
調(diào)用身份證識(shí)別接口
下面就是調(diào)用識(shí)別接口,調(diào)用這個(gè)接口需要了解以下信息
- URL
- Header參數(shù)
- Body參數(shù)
首先是URL
POST https://{endpoint}/v2/{project_id}/ocr/id-card
參數(shù) | 說明 |
---|---|
endpoint | 項(xiàng)目接口域名,這里還是需要確定開通服務(wù)的區(qū)域,每個(gè)區(qū)域不同的域名 |
project_id | 項(xiàng)目ID,不出意外的話就是你上面獲取token的項(xiàng)目ID |
請(qǐng)求參數(shù)
請(qǐng)求Header參數(shù)
參數(shù) | 說明 |
---|---|
X-Auth-Token | 用戶Token,就是上面獲取到的token值 |
Content-Type | 發(fā)送的實(shí)體的MIME類型,參數(shù)值為“application/json” |
請(qǐng)求Body參數(shù)
參數(shù) | 說明 |
---|---|
image | 圖像數(shù)據(jù),base64編碼,要求base64編碼后大小不超過10MB。圖片最小邊不小于15px,最長(zhǎng)邊不超過8000px。支持JPEG、JPG、PNG、BMP、TIFF格式。 |
url | 圖片的url路徑 |
side | front:身份證人像面,back:身份證國(guó)徽面 |
了解了上面的一系列信息之后,后面就是如何寫代碼了,下面就是直接代碼
import requests import base64 # url值 url = https://{endpoint}/v2/{project_id}/ocr/id-card # 上面獲取到的token值 token = "" # 設(shè)置頭部信息 headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} # 圖片路徑 imagepath = r'./static/XXX.jpg' # 設(shè)置圖片 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) # 打印識(shí)別的信息 response = response.text print(response)
最后識(shí)別出來的結(jié)果類似于下面的信息:
{ "result": { "name": "XXX", "sex": "男", "ethnicity": "漢", "birth": "XXX", "address": "XXX", "number": "XXX" } }
如果想獲取其中的某一個(gè)值的話需要先轉(zhuǎn)換為字典,通過字典再進(jìn)行單一讀取
import json # 轉(zhuǎn)換為字典 test = json.loads(response) # 從字典中獲取相應(yīng)的值 username = test['result']['name']
到這里就已經(jīng)完成了這個(gè)項(xiàng)目
總結(jié)
上面已經(jīng)簡(jiǎn)單說明了如何調(diào)用相應(yīng)的接口去識(shí)別身份證了,下面我把源碼給到大家吧,大家可以作為參考哦,下面是使用python+flask+redis+華為云OCR實(shí)現(xiàn)的
import json from flask import Flask, render_template import requests from flask_redis import FlaskRedis import base64 app = Flask(__name__) # 設(shè)置redis連接地址 REDIS_URL = 'redis://127.0.0.1:6379/0' # 實(shí)例化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)用,并設(shè)置緩存時(shí)間為24小時(shí) relue = rd.set('token', token, ex=60 * 1440) # 判斷是否再redis中添加token值成功 if relue != 1: error = "寫入錯(cuò)誤" 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) # 判斷是否識(shí)別成功 if not response.ok: error = "識(shí)別失敗,請(qǐng)聯(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()
到此這篇關(guān)于Python中AI圖像識(shí)別實(shí)現(xiàn)身份證識(shí)別的文章就介紹到這了,更多相關(guān)Python AI圖像識(shí)別 內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。