環(huán)境準(zhǔn)備
安裝Tesseract:點(diǎn)這里參考本人博客
下載第三方庫
pip install Pytesseract
這個(gè)庫只自帶了一個(gè)英語的語言包,這個(gè)時(shí)候如果我們圖片中有對(duì)中文或者其他語言的識(shí)別需求,就需要去下載其他語言包
下載其他語言包
進(jìn)入官網(wǎng)以后進(jìn)入Traineddata Files
找到這個(gè)位置
tessdata_best適用于愿意以大量速度換取稍微好一點(diǎn)的準(zhǔn)確性的人。它也是 唯一可用于高級(jí)用戶的某些再培訓(xùn)方案的文件集。
tessdata 中的第三組是唯一支持舊識(shí)別器的集合。4 年 00 月的 2016.4 文件既有舊版 LSTM 模型,也有舊版 LSTM 模型。tessdata 中的當(dāng)前文件集具有舊模型和較新的 LSTM 模型(tessdata_best 中 00.00.<> alpha 模型的整數(shù)版本)。
點(diǎn)這里直接拿傳送的到github的語言包下載地址
下載完成后將traineddata文件拷貝到tesseract的安裝目錄下tessdata中(像這樣?。。?!)
小案例
輸出tesseract的版本號(hào)
import pytesseract
from PIL import Image
# 輸出版本號(hào)
print(pytesseract.get_tesseract_version())
結(jié)果:5.0.1.20220107
輸出能夠識(shí)別的語言列表
import pytesseract
from PIL import Image
# 輸出版本號(hào)
print(pytesseract.get_languages())
結(jié)果:['chi_sim', 'chi_sim_vert', 'eng', 'osd']
讀取中文
import pytesseract
from PIL import Image
case = pytesseract.image_to_string(Image.open('a.png'), lang='chi_sim')
print(case)
讀取英文
import pytesseract
from PIL import Image
case = pytesseract.image_to_string(Image.open('a.png'), lang='chi_sim')
print(case)
4、獲取圖片中文字的詳細(xì)信息
image_to_data()用來獲取識(shí)別出來的文字的詳細(xì)信息,包含識(shí)別到的文本內(nèi)容,可信度,位置等:
import pytesseract
from PIL import Image
im = Image.open('1.jpg')
獲取圖片中文字的詳細(xì)信息
print(pytesseract.image_to_data(im, lang='chi_sim'))
5、識(shí)別圖片中的文字和位置
image_to_boxes()用來獲取識(shí)別出來的文字和位置信息:
import pytesseract
from PIL import Image
im = Image.open('1.jpg')
print(pytesseract.image_to_boxes(im, lang='chi_sim'))
識(shí)別osd信息
image_to_osd()返回識(shí)別到的osd信息:
import pytesseract
from PIL import Image
im = Image.open('c.png')
print(pytesseract.image_to_osd(im, lang='chi_sim'))
7、識(shí)別并生成xml文件
image_to_pdf_or_hocr()可以將識(shí)別的文字信息轉(zhuǎn)為xml格式字節(jié)流,從而可以寫入到xml文件中,其中入?yún)xtension設(shè)置為’hocr’:文章來源:http://www.zghlxwxcb.cn/news/detail-527290.html
import pytesseract
from PIL import Image
im = Image.open('c.png')
hocr = pytesseract.image_to_pdf_or_hocr(im, lang='chi_sim', extension='hocr')
with open('test.xml',"w+b") as f:
f.write(hocr)
print(type(hocr))
文章來源地址http://www.zghlxwxcb.cn/news/detail-527290.html
到了這里,關(guān)于提取圖像中的文本信息(Tesseract OCR 和 pytesseract)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!