python利用百度AI實現(xiàn)文字識別功能
本文為大家分享了python實現(xiàn)文字識別功能大全,供大家參考,具體內容如下
1.通用文字識別
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "test3.png"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
# 定義參數(shù)變量
options = {
'detect_direction': 'true',
'language_type': 'CHN_ENG',
}
# 調用通用文字識別接口
result = aipOcr.basicGeneral(get_file_content(filePath), options)
print(result)
words_result=result['words_result']
for i in range(len(words_result)):
print(words_result[i]['words'])

2.網(wǎng)絡圖片文字識別
識別一些網(wǎng)絡上背景復雜,特殊字體的文字。
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "2-5.jpg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options={}
options["detect_direction"] = "true" #檢測朝向
options["detect_language"] = "true" #檢測語言
result= aipOcr.webImage(get_file_content(filePath),options)
print(result)
for i in range(len(result['words_result'])):
print(result['words_result'][i]['words'])

3.身份證識別
身份證識別包括正面和背面。
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath1 = "2-6-2.jpg" #正面
filePath2 = "2-6-1.jpg" #背面
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options={}
options["detect_direction"] = "true" #檢測朝向
options["detect_risk"] = "true"
#是否開啟身份證風險類型(身份證復印件、臨時身份證、身份證翻拍、修改過的身份證)功能,默認不開啟
result1= aipOcr.idcard(get_file_content(filePath1),'front',options)
result2= aipOcr.idcard(get_file_content(filePath2),'back',options)
print(result1)
print(result2)
for key in result1['words_result'].keys():
print(key+':'+result1['words_result'][key]['words'])
for key in result2['words_result'].keys():
print(key+':'+result2['words_result'][key]['words'])

4.銀行卡識別
識別銀行卡并返回卡號和發(fā)卡行。
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "2-7.jpeg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options={}
result=aipOcr.bankcard(get_file_content(filePath),options)
print(result)
#bank_card_type 銀行卡類型,0:不能識別; 1: 借記卡; 2: 信用卡
for key in result['result']:
print(key+':'+str(result['result'][key]))

5.駕駛證識別
對機動車駕駛證所有關鍵字段進行識別。
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "2-8.jpg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options={}
result=aipOcr.drivingLicense(get_file_content(filePath),options)
print(result)
for key in result['words_result']:
print(key+':'+str(result['words_result'][key]['words']))

6.行駛證識別
對機動車行駛證正本所有關鍵字段進行識別。
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "2-9.jpg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options={}
result=aipOcr.vehicleLicense(get_file_content(filePath),options)
print(result)
for key in result['words_result']:
print(key+':'+str(result['words_result'][key]['words']))

7.車牌識別
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "2-3.png"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options={}
options["multi_detect"] = "true"
#是否檢測多張車牌,默認為false,當置為true的時候可以對一張圖片內的多張車牌進行識別
result= aipOcr.licensePlate(get_file_content(filePath),options)
for i in range(len(result['words_result'])):
print(result['words_result'][i]['color']+' '+result['words_result'][i]['number'])

8.營業(yè)執(zhí)照識別
識別營業(yè)執(zhí)照,并返回關鍵字段的值,包括單位名稱、法人、地址、有效期、證件編號、社會信用代碼等。
# -*- coding: UTF-8 -*-
from aip import AipOcr
# 定義常量
APP_ID = '11352343'
API_KEY = 'Nd5Z1NkGoLDvHwBnD2bFLpCE'
SECRET_KEY = 'A9FsnnPj1Ys2Gof70SNgYo23hKOIK8Os'
# 初始化AipFace對象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# 讀取圖片
filePath = "2-10.jpg"
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options={}
result=aipOcr.businessLicense(get_file_content(filePath),options)
print(result)
for key in result['words_result']:
print(key+':'+str(result['words_result'][key]['words']))

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
matplotlib制作雷達圖報錯ValueError的實現(xiàn)
這篇文章主要介紹了matplotlib制作雷達圖報錯ValueError的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
flask后端request獲取參數(shù)的幾種方式整理
這篇文章主要為大家介紹了flask后端request獲取參數(shù)的幾種方式整理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Anaconda下Python中GDAL模塊的下載與安裝過程
這篇文章主要介紹了Anaconda下Python中GDAL模塊的下載與安裝方法,本文介紹在Anaconda環(huán)境下,安裝Python中柵格、矢量等地理數(shù)據(jù)處理庫GDAL的方法,需要的朋友可以參考下2023-04-04
利用pyinstaller或virtualenv將python程序打包詳解
這篇文章主要給大家介紹了利用pyinstaller將python程序打包的相關資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
python實現(xiàn)通過代理服務器訪問遠程url的方法
這篇文章主要介紹了python實現(xiàn)通過代理服務器訪問遠程url的方法,涉及Python使用urllib模塊操作URL的相關技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04

