摘要: 最近閒來無事, 使用Python寫了個文字識別的程序, 在這裏分享給大家
1. 準備工作
申請百度開發者平臺, 獲取圖片文字識別, 申請地址:
https://console.bce.baidu.com/ai/?_=1553338619822&fromai=1#/ai/ocr/overview/index
申請效果如下所述:
2. 配置依賴庫
pip install baidu-aip
3. 導入依賴庫
from aip import AipOcr
import os,sys
import PIL.Image
4. 設置ID API Key Secret Key
直接在百度雲平臺獲取就可以, 以下爲無效
API_ID = '1553XX'
API_KEY = 'LFIDSL3XXXXXXXXXXXFDSZXS'
SECRET_KEY = 'LFDSLFJIEJLXXXXXXXXXXJFKDL'
5. 打開圖片
(1)判斷打開文件是否爲圖片文件
def is_picture(filename):
cExt = ['.bmp','.png','jpg']
num = 0
for i in cExt:
if i in filename:
break
else:
num = num + 1
if num == 3:
return 0
else:
return 1
(2)由於目前該接口只適用於.png文件, 因此需要將其他類型的文件轉爲.png文件再讀取
def get_image_from_file(filename):
is_pic = is_picture(filename)
if is_pic == 1:
try:
png_filename = filename[:-4]+".png"
PIL.Image.open(filename).save(png_filename)
with open(png_filename,'rb') as fp:
content = fp.read()
#將轉化後的Png刪除
os.remove(png_filename)
return content
except:
return 'error'
#tkinter.messagebox.showinfo('提示','圖片已被其它工具打開,請關閉後重試!')
else:
return ''
6. 進行圖片識別
(1)獲取aip, 用來與百度雲數據傳輸
def get_apiOcr():
aipOcr = AipOcr(API_ID,API_KEY,SECRET_KEY)
return aipOcr
(2)進行識別
#獲取圖片內容
contents = get_image_from_file(filename)
#獲取aip
api = get_apiOcr()
def get_words(AipOcr,content):
#自動識別文字方向, 文字識別設置中\英文同時識別
options = { 'detect_direction':'true', 'language_type':'CHN_ENG'}
#獲取識別內容
result = AipOcr.webImage(content,options)
#print (result)
words = []
#提取內容
for i in result['words_result']:
#print (i['words'])
words.append(i['words'])
print(words)
7.識別測試
(1)輸入圖片
(2)識別結果
逆向工程在口腔醫學上的應用具有高度的學科交叉性,是機械、計算機生物材料學、醫學相融合的產物,
其應用領域和深度亦隨着各學科的發展與交而飛速的發展起來。本文圍繞逆向工程及其在口腔醫學上的
擴展應用,針對網格模型處理技術進行了以下幾個方面的研究與應用(1)建立了圖像濾波和網格光順之間
相應的橋樑,並對 Laplacian光順Taub光順、 debrun光順、雙邊濾波光順進行具體研究和對比分析,
得到了不同的光順效果,從而對比顯示出各種光順算法的特性。
8.以下爲借用python開發的GUI界面
關注下面的公衆號, 回覆[文字識別]獲取下載鏈接!
下載鏈接:https://fgk.pw/i/asJCUCX0237