python基於百度AI開發文字識別


很多場景都會用到文字識別,比如app或者網站里都會上傳身份證等證件以及財務系統識別報銷證件等等

第一步,你需要去百度AI里去注冊一個賬號,然后新建一個文字識別的應用 然后你將得到一個API Key 和Secret Key,如下圖

百度AI地址 https://ai.baidu.com/tech/imagerecognition

百度AI文檔 https://cloud.baidu.com/doc/OCR/s/zk3h7xz52

 

 

接下來,你需要安裝百度ai的包

pip install baidu-aip

下載成功之后直接上代碼,哈哈

# 基於百度AI開發的文字識別
# encoding:utf-8
import requests
import json
import base64

host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=新建應用得到的API Key&client_secret=新建應用得到的Secret Key'
response = requests.get(host)
if response:
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
    f = open('./word.png', 'rb')
    img = base64.b64encode(f.read())
    params = {"image": img}
    access_token = json.loads(response.text).get("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:
        print(response.json())

注意,open圖片的地址我是直接放在和此py文件同一級目錄下,結果如下

 

 識別結果為

 

 識別結果還是很准的,比pytesser3等識別的結果還是有保證些


免責聲明!

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



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