目錄
1.介紹
2. 準(zhǔn)備工作
2.1 安裝Tesseract
2.2 安裝pytesseract
3.?Tesseract的基礎(chǔ)應(yīng)用
?3.1 翻譯圖像文字——image_to_string
?3.2 獲取單個字符的外框——image_to_boxes
?3.3? 輸出區(qū)域、置信度 文字內(nèi)容以及其他——image_to_data
3.4 設(shè)定配置實(shí)現(xiàn)過濾功能?
3.4.1? OEM
3.4.2? PEM
3.4.3 示例:只檢測數(shù)字
1.介紹
????????Tesseract 的開發(fā)始于 2006 年,由惠普公司的 Mike J. Bradbury 帶領(lǐng)團(tuán)隊(duì)進(jìn)行。當(dāng)時(shí),他們希望開發(fā)一款能夠識別印刷體和手寫體的 OCR(Optical Character Recognition,光學(xué)字符識別) 引擎。2009 年,Tesseract 被移植到 C++,并成為 Google 的開源項(xiàng)目。隨著時(shí)間的推移,Tesseract 逐漸成為最受歡迎的 OCR 引擎之一,被廣泛應(yīng)用于各種場景。它可以將圖像中的文字提取出來,并將其轉(zhuǎn)化為機(jī)器可讀的文本。Tesseract 不僅支持多種語言,還可以進(jìn)行多種格式的圖像處理,包括常見的 PDF、JPG、PNG 等。
2. 準(zhǔn)備工作
2.1 安裝Tesseract
Tesseract下載地址為:
Tesseract User Manual | tessdocTesseract documentationhttps://tesseract-ocr.github.io/tessdoc/
↓↓↓??按照如下步驟進(jìn)行安裝?
?
勾選下面的選項(xiàng)下載一些語言包,可以翻譯中文和一些其他語言?
下載不成功的朋友可以點(diǎn)擊下面的鏈接進(jìn)行下載
https://github.com/tesseract-ocr/tessdatahttps://github.com/tesseract-ocr/tessdata
選擇下載路徑?
點(diǎn)擊Finish下載完畢?
2.2 安裝pytesseract
執(zhí)行命令安裝pytesseract庫
pip install pytesseract
3.?Tesseract的基礎(chǔ)應(yīng)用
如果你想先了解一下pytesseract都有什么功能的話可以先Ctrl + 鼠標(biāo)左鍵 點(diǎn)進(jìn)去大致瀏覽一下
這里Franpper幫大家把pytesseract的功能先列出來
下面正式開始
首先進(jìn)行一些基礎(chǔ)操作:導(dǎo)入相關(guān)包、設(shè)置 Tesseract OCR 引擎的路徑、加載圖片等
# 導(dǎo)入一些需要的包
import cv2
import pytesseract
# 設(shè)置Tesseract OCR引擎路徑
pytesseract.pytesseract.tesseract_cmd = r'D:\Program Files\Tesseract-OCR\tesseract.exe'
# 加載一張圖片
img = cv2.imread(r'E:\csdn\tesseract\Snipaste.jpg')
?下面是Franpper讀入的圖片(就是本文的簡介)
?3.1 翻譯圖像文字——image_to_string
text = pytesseract.image_to_string(img, lang="chi_sim") # 指定語言
print(text)
輸出如下(其實(shí)看結(jié)果翻譯的情況并不好):
?3.2 獲取單個字符的外框——image_to_boxes
boxes = pytesseract.image_to_boxes(img, lang="chi_sim") # 使用
image_h, image_w, _ = img.shape
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
"""
:param img: 圖像
:param text: 文字內(nèi)容
:param left: 字體左邊開始位置
:param top: 字體上面開始位置
:param textColor: 字體顏色
:param textSize: 字體大小
:return: 繪制后的圖片
"""
import numpy as np
from PIL import Image, ImageDraw, ImageFont
if isinstance(img, np.ndarray): # 判斷是否OpenCV圖片類型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 創(chuàng)建一個可以在給定圖像上繪圖的對象
draw = ImageDraw.Draw(img)
# 字體的格式
fontStyle = ImageFont.truetype(
"STSONG.TTF", textSize, encoding="utf-8")
# 繪制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 轉(zhuǎn)換回OpenCV格式
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
for box in boxes.splitlines():
box = box.split(' ')
print(box)
"""
opencv中坐標(biāo)系是基于左上點(diǎn)的
但是tesseract檢測出的框是基于左下點(diǎn)的
輸出的數(shù)據(jù)分別是:字符, 左下角橫坐標(biāo), 左下角縱坐標(biāo), 右上角橫坐標(biāo), 右上角縱坐標(biāo)(均是基于左下點(diǎn)原點(diǎn))
所以在繪制的時(shí)候要進(jìn)行坐標(biāo)轉(zhuǎn)換
"""
x1, y1, x2, y2 = int(box[1]), int(box[2]), int(box[3]), int(box[4])
cv2.rectangle(img, (x1, image_h - y1), (x2, image_h - y2), (0, 255, 0), 1)
# cv2.putText函數(shù)無法添加中文字符,所以使用PIL庫(RGB)添加中文字符后轉(zhuǎn)為opencv格式(BGR)
img = cv2ImgAddText(img, box[0], x1, image_h - y1 - 30, (255, 0, 0), 15)
cv2.imshow("img", img)
cv2.waitKey(0)
輸出如下,可以獲取單個文字外框的左下角橫坐標(biāo)、左下角縱坐標(biāo)、右上角橫坐標(biāo)、右上角縱坐標(biāo)(基于左下點(diǎn)原點(diǎn)坐標(biāo)系)
?3.3? 輸出區(qū)域、置信度 文字內(nèi)容以及其他——image_to_data
data = pytesseract.image_to_data(img, output_type=pytesseract.Output.STRING, lang="chi_sim")
for level, infor in enumerate(data.splitlines()):
if level != 0:
infor = infor.split()
# 每一行的輸出為:level、page_num、block_num、par_num、line_num、word_num、left、top、width、height、conf、text
print(infor)
if len(infor) == 12:
x, y, w, h = int(infor[6]), int(infor[7]), int(infor[8]), int(infor[9]) # 這里的坐標(biāo)郵與opencv相同了,即左上加寬高
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)
img = cv2ImgAddText(img, infor[11], x, y - 20, (255, 0, 0), 15)
cv2.imshow("img", img)
cv2.waitKey(0)
執(zhí)行結(jié)果如下:可以看到把一些字母合并成單詞整體進(jìn)行輸出了?
3.4 設(shè)定配置實(shí)現(xiàn)過濾功能?
在編輯配置時(shí)有兩個重要的參數(shù),分別是OEM與PSM,下面Franpper給大家介紹一下
3.4.1? OEM
即 OCR?Engine Mode(引擎模式),共有四種,如下圖:
- 0 — Legacy engine only
- 1 — Neural nets LSTM engine only
- 2 — Legacy + LSTM engines
- 3 — Default, based on what is available
3.4.2? PEM
即Page Segmentation Mode(圖片分割模式),共有13種, 如下圖:
- 0 — Orientation and script detection (OSD) only. 方向及語言檢測(Orientation and script detection,OSD)
- 1 — Automatic page segmentation with OSD. 自動圖片分割
- 2 — Automatic page segmentation, but no OSD, or OCR. 自動圖片分割,沒有OSD和OCR
- 3 — Fully automatic page segmentation, but no OSD. (Default) 完全的自動圖片分割,沒有OSD
- 4 — Assume a single column of text of variable sizes. 假設(shè)有一列不同大小的文本
- 5 — Assume a single uniform block of vertically aligned text. 假設(shè)有一個垂直對齊的文本塊
- 6 — Assume a single uniform block of text. 假設(shè)有一個對齊的文本塊
- 7 — Treat the image as a single text line. 圖片為單行文本
- 8 — Treat the image as a single word. 圖片為單詞
- 9 — Treat the image as a single word in a circle. 圖片為圓形的單詞
- 10 — Treat the image as a single character. 圖片為單個字符
- 11 — Sparse text. Find as much text as possible in no particular order. 稀疏文本。查找盡可能多的文本,沒有特定的順序
- 12 — Sparse text with OSD. OSD稀疏文本
- 13 — Raw line. Treat the image as a single text line, bypassing hacks that are Tesseract-specific. 原始行。將圖像視為單個文本行
3.4.3 示例:只檢測數(shù)字
config = r'--oem 3 --psm 6 outputbase digits' # 添加配置
data = pytesseract.image_to_data(img, output_type=pytesseract.Output.STRING, lang="chi_sim", config=config)
for level, infor in enumerate(data.splitlines()):
if level != 0:
infor = infor.split()
# 每一行的輸出為:level、page_num、block_num、par_num、line_num、word_num、left、top、width、height、conf、text
print(infor)
if len(infor) == 12:
x, y, w, h = int(infor[6]), int(infor[7]), int(infor[8]), int(infor[9]) # 這里的坐標(biāo)郵與opencv相同了,即左上加寬高
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)
img = cv2ImgAddText(img, infor[11], x, y - 20, (255, 0, 0), 15)
cv2.imshow("img", img)
cv2.waitKey(0)
運(yùn)行結(jié)果如下,可以看到只有數(shù)字被提取了出來
文章來源:http://www.zghlxwxcb.cn/news/detail-859488.html
?
4. 結(jié)語
????????Tesseract不僅僅是一個OCR工具,它代表了人工智能和機(jī)器學(xué)習(xí)在文本識別領(lǐng)域的突破性技術(shù)。它不僅為我們提供了從圖像中提取文字的強(qiáng)大能力,而且通過持續(xù)的研發(fā)和優(yōu)化,Tesseract的能力還將進(jìn)一步提升。然而,Tesseract并非萬能的。雖然它對于一些常規(guī)的文本識別任務(wù)有著出色的表現(xiàn),但在處理一些復(fù)雜或特定的任務(wù)時(shí),我們可能還需要進(jìn)行更多的預(yù)處理或者后處理工作。盡管如此,Tesseract仍然是一個非常強(qiáng)大且靈活的工具,值得我們深入學(xué)習(xí)和探索。文章來源地址http://www.zghlxwxcb.cn/news/detail-859488.html
到了這里,關(guān)于OCR--基于Tesseract詳細(xì)教程(python)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!