Python基于百度AI實現(xiàn)OCR文字識別
百度AI功能還是很強大的,百度AI開放平臺真的是測試接口的天堂,免費接口很多,當(dāng)然有量的限制,但個人使用是完全夠用的,什么人臉識別、MQTT服務(wù)器、語音識別等等,應(yīng)有盡有。


看看OCR識別免費的量


快速安裝:執(zhí)行pip install baidu-aip即可
新建一個AipOcr:
from aip import AipOcr """ 你的 APPID AK SK """ APP_ID = '你的 App ID' API_KEY = '你的 Api Key' SECRET_KEY = '你的 Secret Key' client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
通用文字識別
""" 讀取圖片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
""" 調(diào)用通用文字識別, 圖片參數(shù)為本地圖片 """
client.basicGeneral(image);
""" 如果有可選參數(shù) """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
""" 帶參數(shù)調(diào)用通用文字識別, 圖片參數(shù)為本地圖片 """
client.basicGeneral(image, options)
url = "http//www.x.com/sample.jpg"
""" 調(diào)用通用文字識別, 圖片參數(shù)為遠程url圖片 """
client.basicGeneralUrl(url);
""" 如果有可選參數(shù) """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
""" 帶參數(shù)調(diào)用通用文字識別, 圖片參數(shù)為遠程url圖片 """
client.basicGeneralUrl(url, options)
通用文字識別 請求參數(shù)詳情

通用文字識別 返回數(shù)據(jù)參數(shù)詳情

通用文字識別
from aip import AipOcr
#更換為自己的注冊信息
APP_ID = '---'
API_KEY = '---'
SECRET_KEY = '---'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)#創(chuàng)建連接
fp=open("tu2.png","rb").read()#打開并讀取文件內(nèi)容
res=client.basicGeneral(fp)#普通
#print(res)
#將所有的文字都合并到一起
strx=""
for tex in res["words_result"]:#遍歷結(jié)果
strx+=tex["words"]#每一行
print(strx)#輸出內(nèi)容
最終代碼
from aip import AipOcr
# 定義常量
APP_ID = '14544448'
API_KEY = 'yRZGUXAlCd0c9vQj1kAjBEfY'
SECRET_KEY = 'sc0DKGy7wZ9MeWFGZnbscbRyoDB2IQlj'
# 初始化AipFace對象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('binary_best.jpg')
# 調(diào)用通用文字識別, 圖片為本地圖片
res=client.general(image)
print(res)
for item in res['words_result']:
print(item['words'])
例:
from aip import AipOcr import re APP_ID='17010327' API_KEY='X2MWCU1LG1PX5H6GAXgdlWD7' SECRET_KEY='vz6GZ6TkhSFvY3quqcuC3EG8oEW3kThB' client=AipOcr(APP_ID,API_KEY,SECRET_KEY) i=open(r'C:\Users\Administrator\Desktop\example.png','rb') image = i.read() result=client.basicGeneral(image) #將所有的文字都合并到一起 for item in result['words_result']: print(item['words'])

通用文字識別client.basicGeneral(image)
通用文字識別(高精度版)client.basicAccurate(image);
通用文字識別(含位置信息版)client.general(image);
通用文字識別(含位置高精度版)client.accurate(image);
通用文字識別(含生僻字版)client.enhancedGeneral(image);
網(wǎng)絡(luò)圖片文字識別client.webImage(image);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python實現(xiàn)在線加密解密網(wǎng)站系統(tǒng)
在這個數(shù)字化時代,數(shù)據(jù)的安全和隱私變得越來越重要,所以本文小編就來帶大家實現(xiàn)一個簡單但功能強大的加密解密系統(tǒng),并深入探討它是如何工作的,有興趣的可以了解下2023-09-09
基于python分享一款地理數(shù)據(jù)可視化神器keplergl
這篇文章主要介紹了分享一款地理數(shù)據(jù)可視化神器keplergl,keplergl是由Uber開源的一款地理數(shù)據(jù)可視化工具,通過keplergl我們可以在Jupyter?notebook中使用,下文分享需要的小伙伴可以參考一下2022-02-02
Python使用pyppeteer模塊實現(xiàn)無頭瀏覽器自動化
pyppeteer是一個基于Python的模塊,它提供了一個高級的API,可以通過控制無頭瀏覽器來實現(xiàn)自動化網(wǎng)頁操作,下面我們就來看看Python如何使用pyppeteer模塊實現(xiàn)無頭瀏覽器自動化吧2024-02-02
Python使用itchat模塊實現(xiàn)群聊轉(zhuǎn)發(fā),自動回復(fù)功能示例
這篇文章主要介紹了Python使用itchat模塊實現(xiàn)群聊轉(zhuǎn)發(fā),自動回復(fù)功能,結(jié)合實例形式分析了Python基于itchat模塊針對微信信息的發(fā)送、回復(fù)等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
Python利用裝飾器click處理解析命令行參數(shù)
這篇文章主要為大家詳細(xì)介紹了Python如何利用裝飾器click實現(xiàn)處理解析命令行參數(shù)功能,文中的示例代碼簡潔易懂,需要的小伙伴快跟隨小編一起了解一下2022-10-10

